downloader/check_torrents.go

94 lines
2.6 KiB
Go
Raw Normal View History

2022-08-25 09:04:22 +02:00
package main
import (
"fmt"
"log"
"sort"
2022-08-25 09:04:22 +02:00
"time"
"git.milar.in/milarin/adverr"
"git.milar.in/milarin/slices"
"git.milar.in/nyaanime/logic"
2022-08-25 18:13:29 +02:00
"git.milar.in/nyaanime/model"
2022-08-25 09:04:22 +02:00
)
func CheckTorrents() {
log.Println("checking torrents")
2022-08-25 09:04:22 +02:00
start := time.Now()
torrents, err := GetTorrents()
if err != nil {
fmt.Println(adverr.Wrap("retrieving torrents failed", err))
return
}
animeList, err := logic.GetAnimeListByAnimeID()
2022-08-25 09:04:22 +02:00
if err != nil {
fmt.Println(adverr.Wrap("retrieving anime list failed", err))
return
}
// parse torrents
parsedTorrentsByAnimeEp := ParseTorrentsByAnimeEp(torrents)
// filter not on anime list
animeListTorrents := FilterTorrentsByAnimeList(parsedTorrentsByAnimeEp, animeList)
// filter essential properties
essentialTorrents := FilterEssentialTorrents(animeListTorrents)
// filter preferred properties
preferredTorrents := GetTorrentsWithMaxPrioByAnimeEp(essentialTorrents)
2022-08-25 18:13:29 +02:00
// information gathered for logging purposes
downloadingEpisodes := map[model.AnimeEpisode]struct{}{}
inCollectionEpisodes := map[model.AnimeEpisode]bool{}
2023-01-15 20:21:46 +01:00
collectionPreferredTorrents := map[*model.ParsedTorrent]bool{}
2022-08-25 18:13:29 +02:00
downloadedTorrents := map[model.AnimeEpisode]*TorrentPriority{}
2022-08-25 09:04:22 +02:00
for animeEp, torrentPrio := range preferredTorrents {
if IsCurrentlyDownloading(animeEp) {
2022-08-25 23:25:48 +02:00
downloadingEpisodes[animeEp] = struct{}{} // debug output
2022-08-25 09:04:22 +02:00
continue
}
props, inCollection := logic.GetAnimeEpProps(animeEp)
2022-08-25 18:13:29 +02:00
2022-08-25 23:25:48 +02:00
inCollectionEpisodes[animeEp] = inCollection // debug output
if inCollection {
2023-01-15 20:21:46 +01:00
collectionPreferred := props.Priority >= torrentPrio.Priority
collectionPreferredTorrents[torrentPrio.ParsedTorrent] = collectionPreferred // debug output
if collectionPreferred {
2022-08-25 23:25:48 +02:00
continue
}
2022-08-25 09:04:22 +02:00
}
if err := DownloadTorrent(animeEp, torrentPrio.ParsedTorrent); err != nil {
2022-08-25 18:13:29 +02:00
fmt.Println(fmt.Sprintf("could not download torrent %s", torrentPrio.ParsedTorrent.Torrent.ID), err)
2022-08-25 09:04:22 +02:00
}
2022-08-25 18:13:29 +02:00
2022-08-25 23:25:48 +02:00
downloadedTorrents[animeEp] = torrentPrio // debug output
2022-08-25 09:04:22 +02:00
}
2022-08-25 23:25:48 +02:00
duration := time.Since(start)
2022-08-25 18:13:29 +02:00
ShowDebugInfo(
parsedTorrentsByAnimeEp,
animeListTorrents,
essentialTorrents,
preferredTorrents,
downloadingEpisodes,
inCollectionEpisodes,
2023-01-15 20:21:46 +01:00
collectionPreferredTorrents,
2022-08-25 18:13:29 +02:00
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))
2022-08-25 09:04:22 +02:00
}