downloader/torrent_filter.go

71 lines
1.9 KiB
Go
Raw Permalink Normal View History

2022-08-15 15:55:27 +02:00
package main
import (
2022-08-23 13:24:03 +02:00
"git.milar.in/milarin/anilist"
2022-08-21 21:14:44 +02:00
"git.milar.in/milarin/slices"
"git.milar.in/nyaanime/logic"
2022-08-15 16:45:52 +02:00
"git.milar.in/nyaanime/model"
2022-08-15 15:55:27 +02:00
)
2022-08-23 13:24:03 +02:00
func FilterTorrentsByAnimeList(allTorrents map[model.AnimeEpisode][]*model.ParsedTorrent, animeList map[anilist.MediaID]*anilist.MediaList) map[model.AnimeEpisode][]*model.ParsedTorrent {
2022-08-25 23:25:48 +02:00
if DownloadAll {
return allTorrents
}
2022-08-23 13:24:03 +02:00
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 {
2022-08-15 15:55:27 +02:00
for _, parsedTorrent := range parsedTorrents {
if HasEssentialProperties(parsedTorrent) {
2022-08-23 13:24:03 +02:00
filtered[animeEpisode] = append(filtered[animeEpisode], parsedTorrent)
2022-08-15 15:55:27 +02:00
}
}
}
2022-08-23 13:24:03 +02:00
return filtered
2022-08-15 15:55:27 +02:00
}
func HasEssentialProperties(torrent *model.ParsedTorrent) bool {
if torrent.Resolution < logic.MinResolution || torrent.Resolution > logic.MaxResolution {
2022-08-15 15:55:27 +02:00
return false
}
if torrent.Torrent.Seeders < logic.MinSeeders || torrent.Torrent.Seeders > logic.MaxSeeders {
2022-08-15 15:55:27 +02:00
return false
}
if torrent.Torrent.Leechers < logic.MinLeechers || torrent.Torrent.Leechers > logic.MaxLeechers {
2022-08-15 15:55:27 +02:00
return false
}
if torrent.Torrent.Downloads < logic.MinDownloads || torrent.Torrent.Downloads > logic.MaxDownloads {
2022-08-15 15:55:27 +02:00
return false
}
if logic.TrustedOnly && !torrent.Torrent.Trusted {
2022-08-15 15:55:27 +02:00
return false
}
for _, essentialLanguage := range logic.EssentialLanguages {
2022-08-15 15:55:27 +02:00
if !slices.Contains(torrent.Languages, essentialLanguage) {
return false
}
}
for _, essentialSubtitle := range logic.EssentialSubtitles {
2022-08-15 15:55:27 +02:00
if !slices.Contains(torrent.Subtitles, essentialSubtitle) {
return false
}
}
return true
}