downloader/torrent_filter.go

71 lines
1.9 KiB
Go

package main
import (
"git.milar.in/milarin/anilist"
"git.milar.in/milarin/slices"
"git.milar.in/nyaanime/logic"
"git.milar.in/nyaanime/model"
)
func FilterTorrentsByAnimeList(allTorrents map[model.AnimeEpisode][]*model.ParsedTorrent, animeList map[anilist.MediaID]*anilist.MediaList) map[model.AnimeEpisode][]*model.ParsedTorrent {
if DownloadAll {
return allTorrents
}
filtered := map[model.AnimeEpisode][]*model.ParsedTorrent{}
for animeEp, torrents := range allTorrents {
if _, ok := animeList[animeEp.Anime.ID]; ok {
filtered[animeEp] = torrents
}
}
return filtered
}
func FilterEssentialTorrents(allTorrents map[model.AnimeEpisode][]*model.ParsedTorrent) map[model.AnimeEpisode][]*model.ParsedTorrent {
filtered := map[model.AnimeEpisode][]*model.ParsedTorrent{}
for animeEpisode, parsedTorrents := range allTorrents {
for _, parsedTorrent := range parsedTorrents {
if HasEssentialProperties(parsedTorrent) {
filtered[animeEpisode] = append(filtered[animeEpisode], parsedTorrent)
}
}
}
return filtered
}
func HasEssentialProperties(torrent *model.ParsedTorrent) bool {
if torrent.Resolution < logic.MinResolution || torrent.Resolution > logic.MaxResolution {
return false
}
if torrent.Torrent.Seeders < logic.MinSeeders || torrent.Torrent.Seeders > logic.MaxSeeders {
return false
}
if torrent.Torrent.Leechers < logic.MinLeechers || torrent.Torrent.Leechers > logic.MaxLeechers {
return false
}
if torrent.Torrent.Downloads < logic.MinDownloads || torrent.Torrent.Downloads > logic.MaxDownloads {
return false
}
if logic.TrustedOnly && !torrent.Torrent.Trusted {
return false
}
for _, essentialLanguage := range logic.EssentialLanguages {
if !slices.Contains(torrent.Languages, essentialLanguage) {
return false
}
}
for _, essentialSubtitle := range logic.EssentialSubtitles {
if !slices.Contains(torrent.Subtitles, essentialSubtitle) {
return false
}
}
return true
}