package main import ( "fmt" "log" "sort" "time" "git.milar.in/milarin/adverr" "git.milar.in/milarin/slices" "git.milar.in/nyaanime/logic" "git.milar.in/nyaanime/model" ) func CheckTorrents() { log.Println("checking torrents") start := time.Now() torrents, err := GetTorrents() if err != nil { fmt.Println(adverr.Wrap("retrieving torrents failed", err)) return } animeList, err := logic.GetAnimeListByAnimeID(logic.AnimeStatuses) 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{} collectionPreferredTorrents := 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 := logic.GetAnimeEpProps(animeEp) inCollectionEpisodes[animeEp] = inCollection // debug output if inCollection { collectionPreferred := props.Priority >= torrentPrio.Priority collectionPreferredTorrents[torrentPrio.ParsedTorrent] = collectionPreferred // debug output if collectionPreferred { 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, collectionPreferredTorrents, downloadedTorrents, ) downloadedAnimeEpisodes := slices.OfMap(downloadedTorrents, func(animeEp model.AnimeEpisode, torrentPrio *TorrentPriority) model.AnimeEpisode { return animeEp }) sort.Slice(downloadedAnimeEpisodes, GetAnimeEpisodesSortFunc(downloadedAnimeEpisodes)) SendTelegramAnimeEpMessage(TelegramDownloadMessagePattern, downloadedAnimeEpisodes) log.Printf("check took %s. sleeping for %s\n", duration.Truncate(time.Millisecond), (PollRate - duration).Truncate(time.Millisecond)) }