downloader/check_torrents.go

86 lines
2.2 KiB
Go
Raw Normal View History

2022-08-25 09:04:22 +02:00
package main
import (
"fmt"
"log"
2022-08-25 09:04:22 +02:00
"time"
"git.milar.in/milarin/adverr"
"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{}
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 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 {
lowerPrio := props.Priority > torrentPrio.Priority
lowerPrioTorrents[torrentPrio.ParsedTorrent] = lowerPrio // debug output
if lowerPrio {
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,
lowerPrioTorrents,
downloadedTorrents,
)
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
}