2022-08-25 09:04:22 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-01-15 19:56:47 +01:00
|
|
|
"log"
|
2023-01-17 10:50:07 +01:00
|
|
|
"sort"
|
2022-08-25 09:04:22 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"git.milar.in/milarin/adverr"
|
2023-01-17 10:50:07 +01:00
|
|
|
"git.milar.in/milarin/slices"
|
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:56:47 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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{}
|
2023-01-15 20:21:46 +01:00
|
|
|
collectionPreferredTorrents := map[*model.ParsedTorrent]bool{}
|
2022-08-25 18:13:29 +02:00
|
|
|
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 {
|
2023-01-15 20:21:46 +01:00
|
|
|
collectionPreferred := props.Priority >= torrentPrio.Priority
|
|
|
|
collectionPreferredTorrents[torrentPrio.ParsedTorrent] = collectionPreferred // debug output
|
|
|
|
if collectionPreferred {
|
2022-08-25 23:25:48 +02:00
|
|
|
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,
|
2023-01-15 20:21:46 +01:00
|
|
|
collectionPreferredTorrents,
|
2022-08-25 18:13:29 +02:00
|
|
|
downloadedTorrents,
|
|
|
|
)
|
|
|
|
|
2023-01-17 10:50:07 +01:00
|
|
|
downloadedAnimeEpisodes := slices.OfMap(downloadedTorrents, func(animeEp model.AnimeEpisode, torrentPrio *TorrentPriority) model.AnimeEpisode {
|
|
|
|
return animeEp
|
|
|
|
})
|
|
|
|
sort.Slice(downloadedAnimeEpisodes, GetAnimeEpisodesSortFunc(downloadedAnimeEpisodes))
|
|
|
|
SendTelegramAnimeEpMessage(TelegramDownloadMessagePattern, downloadedAnimeEpisodes)
|
|
|
|
|
2023-01-15 19:56:47 +01:00
|
|
|
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
|
|
|
}
|