2022-08-15 15:55:27 +02:00
|
|
|
package main
|
|
|
|
|
2022-08-21 21:14:44 +02:00
|
|
|
import (
|
|
|
|
"sort"
|
2022-08-15 15:55:27 +02:00
|
|
|
|
2022-08-21 21:14:44 +02:00
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
2022-08-22 14:14:14 +02:00
|
|
|
func DeterminePriority(props model.PropertyHolder) (priority int, preferredProperties map[string]int) {
|
2022-08-21 21:14:44 +02:00
|
|
|
preferredProperties = map[string]int{}
|
|
|
|
|
2022-08-22 14:14:14 +02:00
|
|
|
for _, lang := range props.GetLanguages() {
|
2022-08-21 21:14:44 +02:00
|
|
|
if langPriority, ok := PreferredLanguages[lang]; ok {
|
|
|
|
priority += langPriority
|
|
|
|
preferredProperties["lang/"+lang] = langPriority
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-22 14:14:14 +02:00
|
|
|
for _, sub := range props.GetSubtitles() {
|
2022-08-21 21:14:44 +02:00
|
|
|
if subPriority, ok := PreferredSubtitles[sub]; ok {
|
|
|
|
priority += subPriority
|
|
|
|
preferredProperties["sub/"+sub] = subPriority
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-22 14:14:14 +02:00
|
|
|
if prefRes, ok := PreferredResolutions[props.GetResolution()]; ok {
|
2022-08-21 21:14:44 +02:00
|
|
|
priority += prefRes
|
2022-08-22 14:14:14 +02:00
|
|
|
preferredProperties["res/"+props.GetResolution().String()] = prefRes
|
2022-08-21 21:14:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
2022-08-15 15:55:27 +02:00
|
|
|
}
|