From f361203c73ba61adfa4e6e7d0fc1c3d38053b611 Mon Sep 17 00:00:00 2001 From: milarin Date: Tue, 17 Jan 2023 10:50:07 +0100 Subject: [PATCH] send a single telegram message for each torrent check instead of for individual torrent files --- check_torrents.go | 8 ++++++++ download_torrent_file.go | 2 -- envvars.go | 2 +- go.mod | 2 +- go.sum | 10 ++-------- telegram.go | 9 +++++++-- utils.go | 12 ++++++++++++ 7 files changed, 31 insertions(+), 14 deletions(-) diff --git a/check_torrents.go b/check_torrents.go index 07576e8..f79e725 100644 --- a/check_torrents.go +++ b/check_torrents.go @@ -3,9 +3,11 @@ package main import ( "fmt" "log" + "sort" "time" "git.milar.in/milarin/adverr" + "git.milar.in/milarin/slices" "git.milar.in/nyaanime/logic" "git.milar.in/nyaanime/model" ) @@ -81,5 +83,11 @@ func CheckTorrents() { downloadedTorrents, ) + downloadedAnimeEpisodes := slices.OfMap(downloadedTorrents, func(animeEp model.AnimeEpisode, torrentPrio *TorrentPriority) model.AnimeEpisode { + return animeEp + }) + sort.Slice(downloadedAnimeEpisodes, GetAnimeEpisodesSortFunc(downloadedAnimeEpisodes)) + SendTelegramAnimeEpMessage(TelegramDownloadMessagePattern, downloadedAnimeEpisodes) + log.Printf("check took %s. sleeping for %s\n", duration.Truncate(time.Millisecond), (PollRate - duration).Truncate(time.Millisecond)) } diff --git a/download_torrent_file.go b/download_torrent_file.go index a95498e..96c8eca 100644 --- a/download_torrent_file.go +++ b/download_torrent_file.go @@ -11,8 +11,6 @@ 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 bc62545..f819b45 100644 --- a/envvars.go +++ b/envvars.go @@ -18,7 +18,7 @@ var ( TelegramBotToken = envvars.String("TELEGRAM_API_TOKEN", "") TelegramChatID = envvars.Int64("TELEGRAM_CHAT_ID", 0) - TelegramDownloadMessagePatternStr = logic.EscSeqReplacer.Replace(envvars.String("TELEGRAM_DOWNLOAD_MESSAGE_PATTERN", `Download started\n\n{{.Title.UserPreferred}} episode {{.Episode}}`)) + TelegramDownloadMessagePatternStr = logic.EscSeqReplacer.Replace(envvars.String("TELEGRAM_DOWNLOAD_MESSAGE_PATTERN", `Download started{{range .}}\n{{.Anime.Title.UserPreferred}} episode {{.Episode}}{{end}}`)) TelegramDownloadMessagePattern = template.Must(template.New("TELEGRAM_DOWNLOAD_MESSAGE_PATTERN").Parse(TelegramDownloadMessagePatternStr)) DownloadAll = envvars.Bool("DOWNLOAD_ALL_ANIMES", false) diff --git a/go.mod b/go.mod index 1503b3a..08a3e7e 100644 --- a/go.mod +++ b/go.mod @@ -10,7 +10,7 @@ require ( git.milar.in/milarin/tprint v0.0.7 git.milar.in/nyaanime/logic v0.0.0-20230115215841-53bbded00fe6 git.milar.in/nyaanime/model v0.0.0-20230117081612-d3fc649dad67 - git.milar.in/nyaanime/parsers v0.0.0-20230115232356-54a23b868d71 + git.milar.in/nyaanime/parsers v0.0.0-20230117093342-b57ecf49e1f5 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 diff --git a/go.sum b/go.sum index b4f5ed2..d9a944f 100644 --- a/go.sum +++ b/go.sum @@ -14,16 +14,10 @@ git.milar.in/milarin/tprint v0.0.7 h1:dvm4l4BhXOie4vtnRMZii0WTLTz2wju8wyUPB/lNeV git.milar.in/milarin/tprint v0.0.7/go.mod h1:UwW/B+0cTCbN5hi0bQBE9rIRgLkq+x4V751rrS2KVoI= git.milar.in/nyaanime/logic v0.0.0-20230115215841-53bbded00fe6 h1:6GmcKt0TaHxATGbSpTgDExdRHiON5OwZlHVJCYU0gbA= git.milar.in/nyaanime/logic v0.0.0-20230115215841-53bbded00fe6/go.mod h1:cLuIBDTtpXdzprOIsz73r6ZCE5s0k+o6HA7fH9OuW/M= -git.milar.in/nyaanime/model v0.0.0-20230115221845-51c282652aef h1:Bp1cMHgGnz5SAVWSL1hk1w7F75LGAkfwHqoTZnNQNbY= -git.milar.in/nyaanime/model v0.0.0-20230115221845-51c282652aef/go.mod h1:kPWLDvFrhc1Uf77gxsBOxNeJ5JTVF2HhVs1IdVcw0tg= -git.milar.in/nyaanime/model v0.0.0-20230117081410-7701a09a0de0 h1:70QTQYoF3OYkJur6gCygb9D+2awYv2gNjWdWMCl/8FU= -git.milar.in/nyaanime/model v0.0.0-20230117081410-7701a09a0de0/go.mod h1:kPWLDvFrhc1Uf77gxsBOxNeJ5JTVF2HhVs1IdVcw0tg= git.milar.in/nyaanime/model v0.0.0-20230117081612-d3fc649dad67 h1:p69LqaWzN/2zsgw45tR1XbFxyayu9ffVlikiKE2ibRw= git.milar.in/nyaanime/model v0.0.0-20230117081612-d3fc649dad67/go.mod h1:kPWLDvFrhc1Uf77gxsBOxNeJ5JTVF2HhVs1IdVcw0tg= -git.milar.in/nyaanime/parsers v0.0.0-20230115231942-89b759a7829f h1:xJiFxbLRKyb+43Yfv3UaZO1GiRGAsakebz6mHKUaRdI= -git.milar.in/nyaanime/parsers v0.0.0-20230115231942-89b759a7829f/go.mod h1:fqiJqSWxh1YVJkKhvQql/21HmQzv4elhC3SpH/2TybI= -git.milar.in/nyaanime/parsers v0.0.0-20230115232356-54a23b868d71 h1:BwWlk7UuQ/Ii97/XUVijujH4nkYJwZ0dHpghQ04hQeo= -git.milar.in/nyaanime/parsers v0.0.0-20230115232356-54a23b868d71/go.mod h1:fqiJqSWxh1YVJkKhvQql/21HmQzv4elhC3SpH/2TybI= +git.milar.in/nyaanime/parsers v0.0.0-20230117093342-b57ecf49e1f5 h1:/CdnetuL/vNwo8VsPX2n4IQt81ozYCLDScgmkPser8c= +git.milar.in/nyaanime/parsers v0.0.0-20230117093342-b57ecf49e1f5/go.mod h1:fqiJqSWxh1YVJkKhvQql/21HmQzv4elhC3SpH/2TybI= 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= diff --git a/telegram.go b/telegram.go index 23a9f79..b543ce9 100644 --- a/telegram.go +++ b/telegram.go @@ -34,10 +34,15 @@ func SendTelegramMessage(text string) { } } -func SendTelegramAnimeEpMessage(messagePattern *template.Template, animeEp model.AnimeEpisode) { +func SendTelegramAnimeEpMessage(messagePattern *template.Template, animeEps []model.AnimeEpisode) { + if len(animeEps) == 0 { + return + } + b := new(strings.Builder) - if err := messagePattern.Execute(b, animeEp); err != nil { + if err := messagePattern.Execute(b, animeEps); err != nil { adverr.Println(adverr.Wrap("could not send telegram message", err)) } + SendTelegramMessage(b.String()) } diff --git a/utils.go b/utils.go index b81f97f..e97590b 100644 --- a/utils.go +++ b/utils.go @@ -22,3 +22,15 @@ func Map2Table[K comparable](title string, m map[K]int) string { return table.String() } + +func GetAnimeEpisodesSortFunc(s []model.AnimeEpisode) func(i, j int) bool { + return func(i, j int) bool { + if s[i].Anime.ID < s[j].Anime.ID { + return true + } else if s[i].Anime.ID > s[j].Anime.ID { + return false + } else { + return s[i].Episode <= s[j].Episode + } + } +}