2022-08-25 09:04:22 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"git.milar.in/milarin/adverr"
|
2023-01-13 13:38:51 +01:00
|
|
|
"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
|
|
|
)
|
|
|
|
|
2023-01-13 13:38:51 +01:00
|
|
|
func CheckTorrents() {
|
2023-01-15 19:49:35 +01:00
|
|
|
fmt.Printf("%schecking torrents\n", time.Now().In(time.Local).Format("2006-01-02 15:04:05"))
|
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
|
|
|
|
}
|
|
|
|
|
2023-01-13 13:38:51 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-01-13 13:38:51 +01:00
|
|
|
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,
|
|
|
|
)
|
|
|
|
|
2023-01-15 19:49:35 +01:00
|
|
|
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),
|
|
|
|
)
|
2022-08-25 09:04:22 +02:00
|
|
|
}
|