downloader/check_torrents.go
Timon Ringwald 858c5806c4 polishing
2022-08-25 23:27:45 +02:00

84 lines
2.2 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{}{} // debug output
continue
}
props, inCollection := 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("check took %s. sleeping for %s\n", duration.Truncate(time.Millisecond), (PollRate - duration).Truncate(time.Millisecond))
}