downloader/check_torrents.go
2022-08-25 18:13:29 +02:00

81 lines
2.1 KiB
Go

package main
import (
"fmt"
"time"
"git.milar.in/milarin/adverr"
"git.milar.in/nyaanime/model"
)
func checkTorrents() {
fmt.Println("checking torrents")
start := time.Now()
torrents, err := GetTorrents()
if err != nil {
fmt.Println(adverr.Wrap("retrieving torrents failed", err))
return
}
animeList, err := 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{}{}
continue
}
props, inCollection := GetAnimeEpProps(animeEp)
lowerPrio := props.Priority > torrentPrio.Priority
inCollectionEpisodes[animeEp] = inCollection
lowerPrioTorrents[torrentPrio.ParsedTorrent] = lowerPrio
if inCollection && 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
}
ShowDebugInfo(
parsedTorrentsByAnimeEp,
animeListTorrents,
essentialTorrents,
preferredTorrents,
downloadingEpisodes,
inCollectionEpisodes,
lowerPrioTorrents,
downloadedTorrents,
)
duration := time.Since(start)
fmt.Printf("check took %s. sleeping for %s\n", duration.Truncate(time.Millisecond), (PollRate - duration).Truncate(time.Millisecond))
}