2023-01-15 21:09:26 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-01-18 10:59:48 +01:00
|
|
|
"context"
|
2023-01-15 21:09:26 +01:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"git.milar.in/milarin/adverr"
|
2023-01-18 10:59:48 +01:00
|
|
|
"git.milar.in/milarin/anilist"
|
|
|
|
"git.milar.in/milarin/channel"
|
|
|
|
"git.milar.in/milarin/slices"
|
|
|
|
"git.milar.in/nyaanime/logic"
|
2023-01-15 21:09:26 +01:00
|
|
|
"git.milar.in/nyaanime/model"
|
|
|
|
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
|
|
|
|
)
|
|
|
|
|
|
|
|
var TelegramBot *tgbotapi.BotAPI
|
2023-01-18 11:33:00 +01:00
|
|
|
var AnimeEpisodeMessageChannel = make(chan model.AnimeEpisode, 1000)
|
2023-01-15 21:09:26 +01:00
|
|
|
|
|
|
|
func InitTelegramBot() error {
|
|
|
|
if TelegramBotToken != "" && TelegramChatID != 0 {
|
|
|
|
bot, err := tgbotapi.NewBotAPI(TelegramBotToken)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
TelegramBot = bot
|
|
|
|
}
|
2023-01-19 12:05:46 +01:00
|
|
|
|
|
|
|
// close channel on app shutdown
|
|
|
|
go func() {
|
|
|
|
<-AppCtx.Done()
|
|
|
|
close(AnimeEpisodeMessageChannel)
|
|
|
|
}()
|
|
|
|
|
|
|
|
AppExitWg.Add(1)
|
2023-01-18 11:33:00 +01:00
|
|
|
go SendMessagePeriodically()
|
2023-01-19 12:05:46 +01:00
|
|
|
|
2023-01-15 21:09:26 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-18 11:33:00 +01:00
|
|
|
func SendMessagePeriodically() {
|
2023-01-19 12:05:46 +01:00
|
|
|
defer AppExitWg.Done()
|
|
|
|
|
2023-01-18 11:33:00 +01:00
|
|
|
var messagesPerInterval <-chan []model.AnimeEpisode
|
|
|
|
|
|
|
|
if TelegramOrganizeMessageSendInterval > 0 {
|
2023-01-19 10:16:43 +01:00
|
|
|
WaitForNextTelegramSendCycle()
|
2023-01-18 11:33:00 +01:00
|
|
|
sendAllQueuedAnimeEpisodes()
|
|
|
|
|
|
|
|
grouperFunc := func(current []model.AnimeEpisode, value model.AnimeEpisode) []model.AnimeEpisode {
|
|
|
|
return append(current, value)
|
|
|
|
}
|
|
|
|
messagesPerInterval = channel.GroupByTime(AnimeEpisodeMessageChannel, TelegramOrganizeMessageSendInterval, grouperFunc)
|
|
|
|
} else {
|
|
|
|
mapperFunc := func(value model.AnimeEpisode) []model.AnimeEpisode {
|
|
|
|
return []model.AnimeEpisode{value}
|
|
|
|
}
|
|
|
|
messagesPerInterval = channel.MapSuccessive(AnimeEpisodeMessageChannel, mapperFunc)
|
|
|
|
}
|
|
|
|
|
|
|
|
for animeEpisodes := range messagesPerInterval {
|
|
|
|
sendTelegramAnimeEpMessage(animeEpisodes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func sendAllQueuedAnimeEpisodes() {
|
|
|
|
queuedEpisodeAmount := len(AnimeEpisodeMessageChannel)
|
|
|
|
animeEpisodes := make([]model.AnimeEpisode, 0, queuedEpisodeAmount)
|
|
|
|
for i := 0; i < queuedEpisodeAmount; i++ {
|
|
|
|
animeEpisodes = append(animeEpisodes, <-AnimeEpisodeMessageChannel)
|
|
|
|
}
|
|
|
|
sendTelegramAnimeEpMessage(animeEpisodes)
|
|
|
|
}
|
|
|
|
|
2023-01-15 21:09:26 +01:00
|
|
|
func SendTelegramMessage(text string) {
|
2023-01-22 13:40:42 +01:00
|
|
|
if TelegramBot == nil || strings.TrimSpace(text) == "" {
|
2023-01-15 21:09:26 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := tgbotapi.NewMessage(TelegramChatID, text)
|
2023-01-22 13:40:42 +01:00
|
|
|
msg.ParseMode = "html"
|
2023-01-15 21:09:26 +01:00
|
|
|
_, err := TelegramBot.Send(msg)
|
|
|
|
if err != nil {
|
|
|
|
adverr.Println(adverr.Wrap("could not send telegram message", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-18 11:33:00 +01:00
|
|
|
func PrepareTelegramAnimeEpMessage(animeEp model.AnimeEpisode) {
|
2023-01-18 10:59:48 +01:00
|
|
|
shouldSendMessage, err := CheckSendConditions(animeEp)
|
|
|
|
if err != nil {
|
|
|
|
adverr.Println(adverr.Wrap("could not check telegram message send conditions", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
if !shouldSendMessage {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-18 11:33:00 +01:00
|
|
|
AnimeEpisodeMessageChannel <- animeEp
|
|
|
|
}
|
|
|
|
|
|
|
|
func sendTelegramAnimeEpMessage(animeEpisodes []model.AnimeEpisode) {
|
|
|
|
if len(animeEpisodes) == 0 {
|
|
|
|
return
|
|
|
|
}
|
2023-01-18 10:59:48 +01:00
|
|
|
|
2023-02-02 12:36:23 +01:00
|
|
|
animeEpisodes = RemoveDuplicates(animeEpisodes)
|
|
|
|
|
2023-01-15 21:09:26 +01:00
|
|
|
b := new(strings.Builder)
|
2023-01-18 11:33:00 +01:00
|
|
|
if err := TelegramOrganizeMessagePattern.Execute(b, animeEpisodes); err != nil {
|
2023-01-15 21:09:26 +01:00
|
|
|
adverr.Println(adverr.Wrap("could not send telegram message", err))
|
|
|
|
}
|
2023-01-18 10:59:48 +01:00
|
|
|
|
2023-01-15 21:09:26 +01:00
|
|
|
SendTelegramMessage(b.String())
|
|
|
|
}
|
2023-01-18 10:59:48 +01:00
|
|
|
|
2023-02-02 12:36:23 +01:00
|
|
|
func RemoveDuplicates(animeEpisodes []model.AnimeEpisode) []model.AnimeEpisode {
|
|
|
|
mapperFunc := func(animeEp model.AnimeEpisode) (model.Pair[anilist.MediaID, int], model.AnimeEpisode) {
|
|
|
|
return model.Pair[anilist.MediaID, int]{First: animeEp.Anime.ID, Second: animeEp.Episode}, animeEp
|
|
|
|
}
|
|
|
|
|
|
|
|
unmapperFunc := func(key model.Pair[anilist.MediaID, int], value model.AnimeEpisode) model.AnimeEpisode {
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
|
|
|
return slices.OfMap(slices.ToMap(animeEpisodes, mapperFunc), unmapperFunc)
|
|
|
|
}
|
|
|
|
|
2023-01-18 10:59:48 +01:00
|
|
|
func CheckSendConditions(animeEp model.AnimeEpisode) (bool, error) {
|
|
|
|
listEntry, err := GetListEntry(animeEp.Anime)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, sendCondition := range AllSendConditions {
|
|
|
|
// check if user configured current sendCondition
|
|
|
|
if !slices.Contains(TelegramOrganizeMessageSendCondition, sendCondition) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if current sendCondition applies for given anime episode
|
|
|
|
if sendCondition.ShouldSend(animeEp, listEntry) {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetListEntry(anime *anilist.Media) (*anilist.MediaList, error) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
listEntries, err := logic.GetCurrentlyWatchingAnimesContext(ctx, logic.AllMediaListStatuses...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
filteredListEntries := channel.Filter(listEntries, func(a *anilist.MediaList) bool { return a.MediaID == anime.ID })
|
|
|
|
listEntry := channel.FindFirstAndCancelFlush(filteredListEntries, cancel) // TODO flush working properly?
|
|
|
|
|
|
|
|
if listEntry == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return *listEntry, nil
|
|
|
|
}
|