downloader/torrent_filter.go
2022-08-23 13:24:03 +02:00

66 lines
1.8 KiB
Go

package main
import (
"git.milar.in/milarin/anilist"
"git.milar.in/milarin/slices"
"git.milar.in/nyaanime/model"
)
func FilterTorrentsByAnimeList(allTorrents map[model.AnimeEpisode][]*model.ParsedTorrent, animeList map[anilist.MediaID]*anilist.MediaList) map[model.AnimeEpisode][]*model.ParsedTorrent {
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 < MinResolution || torrent.Resolution > MaxResolution {
return false
}
if torrent.Torrent.Seeders < MinSeeders || torrent.Torrent.Seeders > MaxSeeders {
return false
}
if torrent.Torrent.Leechers < MinLeechers || torrent.Torrent.Leechers > MaxLeechers {
return false
}
if torrent.Torrent.Downloads < MinDownloads || torrent.Torrent.Downloads > MaxDownloads {
return false
}
if TrustedOnly && !torrent.Torrent.Trusted {
return false
}
for _, essentialLanguage := range EssentialLanguages {
if !slices.Contains(torrent.Languages, essentialLanguage) {
return false
}
}
for _, essentialSubtitle := range EssentialSubtitles {
if !slices.Contains(torrent.Subtitles, essentialSubtitle) {
return false
}
}
return true
}