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

54 lines
1.3 KiB
Go

package main
import (
"git.milar.in/milarin/slices"
"git.milar.in/nyaanime/model"
)
func GetTorrentsWithMaxPrioByAnimeEp(torrents map[model.AnimeEpisode][]*model.ParsedTorrent) map[model.AnimeEpisode]*TorrentPriority {
torrentsWithPrio := map[model.AnimeEpisode]*TorrentPriority{}
for animeEp, torrentList := range torrents {
torrentPrioList := slices.Map(torrentList, NewTorrentPriority)
var maxPrio *TorrentPriority
for _, torrentPrio := range torrentPrioList {
if maxPrio == nil || torrentPrio.Priority > maxPrio.Priority {
maxPrio = torrentPrio
}
}
if maxPrio != nil {
torrentsWithPrio[animeEp] = maxPrio
}
}
return torrentsWithPrio
}
func DeterminePriority(props model.PropertyHolder) (priority int, preferredProperties map[string]int) {
preferredProperties = map[string]int{}
for _, lang := range props.GetLanguages() {
if langPriority, ok := PreferredLanguages[lang]; ok {
priority += langPriority
preferredProperties["lang/"+lang] = langPriority
}
}
for _, sub := range props.GetSubtitles() {
if subPriority, ok := PreferredSubtitles[sub]; ok {
priority += subPriority
preferredProperties["sub/"+sub] = subPriority
}
}
if prefRes, ok := PreferredResolutions[props.GetResolution()]; ok {
priority += prefRes
preferredProperties["res/"+props.GetResolution().String()] = prefRes
}
return
}