From e4db93e9112683d492f08615cd2fccd5cccd0073 Mon Sep 17 00:00:00 2001 From: milarin Date: Mon, 30 Jan 2023 21:35:49 +0100 Subject: [PATCH] remove double entries in telegram report message --- telegram.go | 2 ++ utils.go | 15 +++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/telegram.go b/telegram.go index 507e51d..1540d3e 100644 --- a/telegram.go +++ b/telegram.go @@ -102,6 +102,8 @@ func sendTelegramAnimeEpMessage(animeEpisodes []model.AnimeEpisode) { return } + animeEpisodes = RemoveDoubleEntries(animeEpisodes) + b := new(strings.Builder) if err := TelegramOrganizeMessagePattern.Execute(b, animeEpisodes); err != nil { adverr.Println(adverr.Wrap("could not send telegram message", err)) diff --git a/utils.go b/utils.go index 8c8778e..df08335 100644 --- a/utils.go +++ b/utils.go @@ -6,8 +6,11 @@ import ( "os" "time" + "git.milar.in/milarin/anilist" "git.milar.in/milarin/channel" "git.milar.in/milarin/gmath" + "git.milar.in/milarin/slices" + "git.milar.in/nyaanime/model" ) func InitializeRunner() channel.Runner { @@ -63,3 +66,15 @@ func WaitForNextTelegramSendCycle() { time.Sleep(time.Until(nextCycle)) } } + +func RemoveDoubleEntries(animeEpisodes []model.AnimeEpisode) []model.AnimeEpisode { + mapperFunc := func(animeEp model.AnimeEpisode) (anilist.MediaID, model.AnimeEpisode) { + return animeEp.Anime.ID, animeEp + } + + unmapperFunc := func(id anilist.MediaID, animeEp model.AnimeEpisode) model.AnimeEpisode { + return animeEp + } + + return slices.OfMap(slices.ToMap(animeEpisodes, mapperFunc), unmapperFunc) +}