downloader/check_torrents.go
2023-01-15 19:49:35 +01:00

89 lines
2.3 KiB
Go

package main
import (
"fmt"
"time"
"git.milar.in/milarin/adverr"
"git.milar.in/nyaanime/logic"
"git.milar.in/nyaanime/model"
)
func CheckTorrents() {
fmt.Printf("%schecking torrents\n", time.Now().In(time.Local).Format("2006-01-02 15:04:05"))
start := time.Now()
torrents, err := GetTorrents()
if err != nil {
fmt.Println(adverr.Wrap("retrieving torrents failed", err))
return
}
animeList, err := logic.GetAnimeListByAnimeID()
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)
// information gathered for logging purposes
downloadingEpisodes := map[model.AnimeEpisode]struct{}{}
inCollectionEpisodes := map[model.AnimeEpisode]bool{}
lowerPrioTorrents := map[*model.ParsedTorrent]bool{}
downloadedTorrents := map[model.AnimeEpisode]*TorrentPriority{}
for animeEp, torrentPrio := range preferredTorrents {
if IsCurrentlyDownloading(animeEp) {
downloadingEpisodes[animeEp] = struct{}{} // debug output
continue
}
props, inCollection := logic.GetAnimeEpProps(animeEp)
inCollectionEpisodes[animeEp] = inCollection // debug output
if inCollection {
lowerPrio := props.Priority > torrentPrio.Priority
lowerPrioTorrents[torrentPrio.ParsedTorrent] = lowerPrio // debug output
if lowerPrio {
continue
}
}
if err := DownloadTorrent(animeEp, torrentPrio.ParsedTorrent); err != nil {
fmt.Println(fmt.Sprintf("could not download torrent %s", torrentPrio.ParsedTorrent.Torrent.ID), err)
}
downloadedTorrents[animeEp] = torrentPrio // debug output
}
duration := time.Since(start)
ShowDebugInfo(
parsedTorrentsByAnimeEp,
animeListTorrents,
essentialTorrents,
preferredTorrents,
downloadingEpisodes,
inCollectionEpisodes,
lowerPrioTorrents,
downloadedTorrents,
)
fmt.Printf("%s check took %s. sleeping for %s\n",
time.Now().In(time.Local).Format("2006-01-02 15:04:05"),
duration.Truncate(time.Millisecond),
(PollRate - duration).Truncate(time.Millisecond),
)
}