downloader/torrent_filter.go
2022-08-15 15:55:27 +02:00

55 lines
1.4 KiB
Go

package main
import (
"git.milar.in/animan/model"
"golang.org/x/exp/slices"
)
func FilterEssentialTorrents(parsedTorrentsByAnimeEp map[model.AnimeEpisode][]*model.ParsedTorrent) map[model.AnimeEpisode][]*model.ParsedTorrent {
filteredMap := map[model.AnimeEpisode][]*model.ParsedTorrent{}
for animeEpisode, parsedTorrents := range parsedTorrentsByAnimeEp {
for _, parsedTorrent := range parsedTorrents {
if HasEssentialProperties(parsedTorrent) {
filteredMap[animeEpisode] = append(filteredMap[animeEpisode], parsedTorrent)
}
}
}
return filteredMap
}
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
}