downloader/check_torrents.go

81 lines
2.1 KiB
Go
Raw Normal View History

2022-08-25 09:04:22 +02:00
package main
import (
"fmt"
"time"
"git.milar.in/milarin/adverr"
2022-08-25 18:13:29 +02:00
"git.milar.in/nyaanime/model"
2022-08-25 09:04:22 +02:00
)
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)
2022-08-25 18:13:29 +02:00
// 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{}
2022-08-25 09:04:22 +02:00
for animeEp, torrentPrio := range preferredTorrents {
if IsCurrentlyDownloading(animeEp) {
2022-08-25 18:13:29 +02:00
downloadingEpisodes[animeEp] = struct{}{}
2022-08-25 09:04:22 +02:00
continue
}
2022-08-25 18:13:29 +02:00
props, inCollection := GetAnimeEpProps(animeEp)
lowerPrio := props.Priority > torrentPrio.Priority
inCollectionEpisodes[animeEp] = inCollection
lowerPrioTorrents[torrentPrio.ParsedTorrent] = lowerPrio
if inCollection && lowerPrio {
2022-08-25 09:04:22 +02:00
continue
}
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
downloadedTorrents[animeEp] = torrentPrio
2022-08-25 09:04:22 +02:00
}
2022-08-25 18:13:29 +02:00
ShowDebugInfo(
parsedTorrentsByAnimeEp,
animeListTorrents,
essentialTorrents,
preferredTorrents,
downloadingEpisodes,
inCollectionEpisodes,
lowerPrioTorrents,
downloadedTorrents,
)
2022-08-25 09:04:22 +02:00
duration := time.Since(start)
2022-08-25 18:13:29 +02:00
fmt.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
}