downloader/torrent_sort.go
2022-08-21 21:14:44 +02:00

46 lines
1.3 KiB
Go

package main
import (
"sort"
"git.milar.in/milarin/slices"
"git.milar.in/nyaanime/model"
)
func SortTorrentsByPreferredProperties(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)
sort.Slice(torrentPrioList, func(i, j int) bool { return torrentPrioList[i].Priority > torrentPrioList[j].Priority })
torrentsWithPrio[animeEp] = torrentPrioList
}
return torrentsWithPrio
}
func DeterminePriority(torrent *model.ParsedTorrent) (priority int, preferredProperties map[string]int) {
preferredProperties = map[string]int{}
for _, lang := range torrent.Languages {
if langPriority, ok := PreferredLanguages[lang]; ok {
priority += langPriority
preferredProperties["lang/"+lang] = langPriority
}
}
for _, sub := range torrent.Subtitles {
if subPriority, ok := PreferredSubtitles[sub]; ok {
priority += subPriority
preferredProperties["sub/"+sub] = subPriority
}
}
if prefRes, ok := PreferredResolutions[torrent.Resolution]; ok {
priority += prefRes
preferredProperties["res/"+torrent.Resolution.String()] = prefRes
}
return
}