diff --git a/check_torrents.go b/check_torrents.go index 07576e8..b0cf665 100644 --- a/check_torrents.go +++ b/check_torrents.go @@ -57,6 +57,7 @@ func CheckTorrents() { collectionPreferred := props.Priority >= torrentPrio.Priority collectionPreferredTorrents[torrentPrio.ParsedTorrent] = collectionPreferred // debug output if collectionPreferred { + SendTelegramAnimeEpMessage(TelegramDownloadMessagePattern, animeEp) continue } } diff --git a/download_torrent_file.go b/download_torrent_file.go index 96c8eca..a95498e 100644 --- a/download_torrent_file.go +++ b/download_torrent_file.go @@ -11,6 +11,8 @@ import ( ) func DownloadTorrent(animeEp model.AnimeEpisode, torrent *model.ParsedTorrent) error { + SendTelegramAnimeEpMessage(TelegramDownloadMessagePattern, animeEp) + if err := SetCurrentlyDownloading(animeEp); err != nil { return ErrLockFileCreationFailed.Wrap(err, animeEp.Anime.Title.Romaji, animeEp.Episode) } diff --git a/envvars.go b/envvars.go index dccc192..d207d2d 100644 --- a/envvars.go +++ b/envvars.go @@ -1,10 +1,11 @@ package main import ( - "html/template" + "text/template" "time" "git.milar.in/milarin/envvars/v2" + "git.milar.in/nyaanime/logic" ) var ( @@ -13,10 +14,16 @@ var ( TorrentPath = envvars.String("TORRENT_PATH", "") DebugAnimeEpisodePattern = envvars.Object( - "DEBUG_ANIME_LANGUAGE_PATTERN", + "DEBUG_ANIME_EPISODE_PATTERN", template.Must(template.New("anime-episode-filepath-pattern").Parse(`{{.Title.UserPreferred}} episode {{.Episode}}`)), - template.New("anime-episode-pattern").Parse, + template.New("anime-episode-filepath-pattern").Parse, ) + TelegramDownloadMessagePatternStr = logic.EscSeqReplacer.Replace(envvars.String("TELEGRAM_DOWNLOAD_MESSAGE_PATTERN", `Download started\n{{.Anime.Title.UserPreferred}}第{{.Episode}}話`)) + TelegramDownloadMessagePattern = template.Must(template.New("telegram-download-message-pattern").Parse(TelegramDownloadMessagePatternStr)) + DownloadAll = envvars.Bool("DOWNLOAD_ALL_ANIMES", false) + + TelegramBotToken = envvars.String("TELEGRAM_API_TOKEN", "") + TelegramChatID = envvars.Int64("TELEGRAM_CHAT_ID", 0) ) diff --git a/go.mod b/go.mod index 1328eef..1ca5769 100644 --- a/go.mod +++ b/go.mod @@ -8,11 +8,12 @@ require ( git.milar.in/milarin/envvars/v2 v2.0.0 git.milar.in/milarin/slices v0.0.6 git.milar.in/milarin/tprint v0.0.7 - git.milar.in/nyaanime/logic v0.0.0-20230114105336-6bfb7dce349f + git.milar.in/nyaanime/logic v0.0.0-20230115195351-8ed3e407ee72 git.milar.in/nyaanime/model v0.0.0-20230113095840-5eb2822653c3 - git.milar.in/nyaanime/parsers v0.0.0-20230113101942-2c9bc6925201 + git.milar.in/nyaanime/parsers v0.0.0-20230115135225-d80026a240a2 github.com/PuerkitoBio/goquery v1.8.0 github.com/fatih/color v1.13.0 + github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible ) require ( @@ -23,6 +24,7 @@ require ( github.com/mattn/go-isatty v0.0.16 // indirect github.com/mattn/go-runewidth v0.0.14 // indirect github.com/rivo/uniseg v0.4.3 // indirect + github.com/technoweenie/multipartstreamer v1.0.1 // indirect golang.org/x/net v0.0.0-20220812174116-3211cb980234 // indirect golang.org/x/sys v0.0.0-20220823224334-20c2bfdbfe24 // indirect gopkg.in/vansante/go-ffprobe.v2 v2.1.1 // indirect diff --git a/go.sum b/go.sum index 2ee2ef5..8271249 100644 --- a/go.sum +++ b/go.sum @@ -12,18 +12,20 @@ git.milar.in/milarin/slices v0.0.6 h1:AQoSarZ58WHYol9c6woWJSe8wFpPC2RC4cvIlZpfg9 git.milar.in/milarin/slices v0.0.6/go.mod h1:NOr53AOeur/qscu/FBj3lsFR262PNYBccLYSTCAXRk4= git.milar.in/milarin/tprint v0.0.7 h1:dvm4l4BhXOie4vtnRMZii0WTLTz2wju8wyUPB/lNeVo= git.milar.in/milarin/tprint v0.0.7/go.mod h1:UwW/B+0cTCbN5hi0bQBE9rIRgLkq+x4V751rrS2KVoI= -git.milar.in/nyaanime/logic v0.0.0-20230114105336-6bfb7dce349f h1:tc8hQpZprt8/yedVSPmkftKOI2+P7twFc7BEov5RTBc= -git.milar.in/nyaanime/logic v0.0.0-20230114105336-6bfb7dce349f/go.mod h1:cLuIBDTtpXdzprOIsz73r6ZCE5s0k+o6HA7fH9OuW/M= +git.milar.in/nyaanime/logic v0.0.0-20230115195351-8ed3e407ee72 h1:/kSY7P/NNaUmkxCJMKmB5YbhIA9owX9pErPxREuBAog= +git.milar.in/nyaanime/logic v0.0.0-20230115195351-8ed3e407ee72/go.mod h1:cLuIBDTtpXdzprOIsz73r6ZCE5s0k+o6HA7fH9OuW/M= git.milar.in/nyaanime/model v0.0.0-20230113095840-5eb2822653c3 h1:mXcEA47FQQzeSDXE3UvhNfIt4fBfpDSq1/f0r+jbHpY= git.milar.in/nyaanime/model v0.0.0-20230113095840-5eb2822653c3/go.mod h1:kPWLDvFrhc1Uf77gxsBOxNeJ5JTVF2HhVs1IdVcw0tg= -git.milar.in/nyaanime/parsers v0.0.0-20230113101942-2c9bc6925201 h1:glTG4IeuIvD4mVwJyCF5SYMawCRcZZ01pz4AUyWTEP8= -git.milar.in/nyaanime/parsers v0.0.0-20230113101942-2c9bc6925201/go.mod h1:GG4vtUIfxopZc/+Y8OAa//vWJw/m6aeoGN7fw6SLiEM= +git.milar.in/nyaanime/parsers v0.0.0-20230115135225-d80026a240a2 h1:Q95JBR9mXENAjRhvzPAsFjPfxY0ljUiLVlhfAO4q6UY= +git.milar.in/nyaanime/parsers v0.0.0-20230115135225-d80026a240a2/go.mod h1:GG4vtUIfxopZc/+Y8OAa//vWJw/m6aeoGN7fw6SLiEM= github.com/PuerkitoBio/goquery v1.8.0 h1:PJTF7AmFCFKk1N6V6jmKfrNH9tV5pNE6lZMkG0gta/U= github.com/PuerkitoBio/goquery v1.8.0/go.mod h1:ypIiRMtY7COPGk+I/YbZLbxsxn9g5ejnI2HSMtkjZvI= github.com/andybalholm/cascadia v1.3.1 h1:nhxRkql1kdYCc8Snf7D5/D3spOX+dBgjA6u8x004T2c= github.com/andybalholm/cascadia v1.3.1/go.mod h1:R4bJ1UQfqADjvDa4P6HZHLh/3OxWWEqc0Sk8XGwHqvA= github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w= github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk= +github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible h1:2cauKuaELYAEARXRkq2LrJ0yDDv1rW7+wrTEdVL3uaU= +github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible/go.mod h1:qf9acutJ8cwBUhm1bqgz6Bei9/C/c93FPDljKWwsOgM= github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= @@ -36,6 +38,8 @@ github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rivo/uniseg v0.4.3 h1:utMvzDsuh3suAEnhH0RdHmoPbU648o6CvXxTx4SBMOw= github.com/rivo/uniseg v0.4.3/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= +github.com/technoweenie/multipartstreamer v1.0.1 h1:XRztA5MXiR1TIRHxH2uNxXxaIkKQDeX7m2XsSOlQEnM= +github.com/technoweenie/multipartstreamer v1.0.1/go.mod h1:jNVxdtShOxzAsukZwTSw6MDx5eUJoiEBsSvzDU9uzog= golang.org/x/net v0.0.0-20210916014120-12bc252f5db8/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20220812174116-3211cb980234 h1:RDqmgfe7SvlMWoqC3xwQ2blLO3fcWcxMa3eBLRdRW7E= golang.org/x/net v0.0.0-20220812174116-3211cb980234/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk= diff --git a/local_file_check.go b/local_file_check.go index 1028768..32160ae 100644 --- a/local_file_check.go +++ b/local_file_check.go @@ -5,17 +5,10 @@ import ( "os" "path/filepath" - "git.milar.in/milarin/anilist" "git.milar.in/nyaanime/logic" "git.milar.in/nyaanime/model" ) -type AnimePathPatternData struct { - Title anilist.MediaTitle - Episode int - Ext string -} - func AnimeEpExistsLocally(animeEp model.AnimeEpisode) bool { animeEpPath := logic.GetAnimeEpFilepath(animeEp, "*") diff --git a/main.go b/main.go index 3f7ef9a..d2af85d 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,6 @@ package main import ( - "fmt" "os/exec" "time" @@ -16,14 +15,14 @@ func main() { // get access token once at startup to be sure that an access token is obtainable at all if _, err := logic.GetAnilistAccessToken(); err != nil { - panic(err) + panic(err) // TODO error handling } - fmt.Println("generated priority values:") - fmt.Print(Map2Table("language", logic.PreferredLanguages)) - fmt.Print(Map2Table("subtitle", logic.PreferredSubtitles)) - fmt.Print(Map2Table("resolution", logic.PreferredResolutions)) - fmt.Println() + if err := InitTelegramBot(); err != nil { + panic(err) // TODO error handling + } + + logic.PrintPriorityTables() ticker := time.NewTicker(PollRate) defer ticker.Stop() diff --git a/show_debug_info.go b/show_debug_info.go index eae38c1..1b027c0 100644 --- a/show_debug_info.go +++ b/show_debug_info.go @@ -63,7 +63,7 @@ func ShowDebugInfo( } b := new(strings.Builder) - DebugAnimeEpisodePattern.Execute(b, AnimePathPatternData{Title: animeEp.Anime.Title, Episode: animeEp.Episode}) + DebugAnimeEpisodePattern.Execute(b, animeEp) header := BoldText.Sprintf("%s (%s)", color.MagentaString(b.String()), epState) fmt.Println(tprint.FormatHeaderTable(header, table)) } diff --git a/telegram.go b/telegram.go new file mode 100644 index 0000000..23a9f79 --- /dev/null +++ b/telegram.go @@ -0,0 +1,43 @@ +package main + +import ( + "strings" + "text/template" + + "git.milar.in/milarin/adverr" + "git.milar.in/nyaanime/model" + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" +) + +var TelegramBot *tgbotapi.BotAPI + +func InitTelegramBot() error { + if TelegramBotToken != "" && TelegramChatID != 0 { + bot, err := tgbotapi.NewBotAPI(TelegramBotToken) + if err != nil { + return err + } + TelegramBot = bot + } + return nil +} + +func SendTelegramMessage(text string) { + if TelegramBot == nil { + return + } + + msg := tgbotapi.NewMessage(TelegramChatID, text) + _, err := TelegramBot.Send(msg) + if err != nil { + adverr.Println(adverr.Wrap("could not send telegram message", err)) + } +} + +func SendTelegramAnimeEpMessage(messagePattern *template.Template, animeEp model.AnimeEpisode) { + b := new(strings.Builder) + if err := messagePattern.Execute(b, animeEp); err != nil { + adverr.Println(adverr.Wrap("could not send telegram message", err)) + } + SendTelegramMessage(b.String()) +}