DeterminePriority implemented
This commit is contained in:
commit
4b71c945a9
28
determine_priority.go
Normal file
28
determine_priority.go
Normal file
@ -0,0 +1,28 @@
|
||||
package logic
|
||||
|
||||
import "git.milar.in/nyaanime/model"
|
||||
|
||||
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
|
||||
}
|
13
envvars.go
Normal file
13
envvars.go
Normal file
@ -0,0 +1,13 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"git.milar.in/milarin/envvars/v2"
|
||||
"git.milar.in/nyaanime/model"
|
||||
)
|
||||
|
||||
var (
|
||||
// preferred torrent properties
|
||||
PreferredLanguages = ParsePreferredStringProps(envvars.StringSlice("PREFERRED_LANGUAGES", "|", []string{}))
|
||||
PreferredSubtitles = ParsePreferredStringProps(envvars.StringSlice("PREFERRED_SUBTITLES", "|", []string{}))
|
||||
PreferredResolutions = ParsePreferredProps(envvars.StringSlice("PREFERRED_RESOLUTIONS", "|", []string{}), model.ParseResolution)
|
||||
)
|
12
go.mod
Normal file
12
go.mod
Normal file
@ -0,0 +1,12 @@
|
||||
module git.milar.in/nyaanime/logic
|
||||
|
||||
go 1.19
|
||||
|
||||
require (
|
||||
git.milar.in/milarin/envvars/v2 v2.0.0
|
||||
git.milar.in/milarin/gmath v0.0.3
|
||||
git.milar.in/milarin/slices v0.0.6
|
||||
git.milar.in/nyaanime/model v0.0.0-20221008104642-466e1111ddea
|
||||
)
|
||||
|
||||
require git.milar.in/milarin/anilist v1.5.0 // indirect
|
10
go.sum
Normal file
10
go.sum
Normal file
@ -0,0 +1,10 @@
|
||||
git.milar.in/milarin/anilist v1.5.0 h1:fSiAXY/topNk4ISEp2QtcG9HHKLJfMc8w05iqc+Paf0=
|
||||
git.milar.in/milarin/anilist v1.5.0/go.mod h1:8PTHXFMA45JpfRFIpcdrKwDHue8fbT/wwV1BuHFn6c0=
|
||||
git.milar.in/milarin/envvars/v2 v2.0.0 h1:DWRQCWaHqzDD8NGpSgv5tYLuF9A/dVFPAtTvz3oiIqE=
|
||||
git.milar.in/milarin/envvars/v2 v2.0.0/go.mod h1:HkdEi+gG2lJSmVq547bTlQV4qQ0hO333bE8IrE0B9yY=
|
||||
git.milar.in/milarin/gmath v0.0.3 h1:ii6rKNItS55O/wtIFhD1cTN2BMwDZjTBmiOocKURvxM=
|
||||
git.milar.in/milarin/gmath v0.0.3/go.mod h1:HDLftG5RLpiNGKiIWh+O2G1PYkNzyLDADO8Cd/1abiE=
|
||||
git.milar.in/milarin/slices v0.0.6 h1:AQoSarZ58WHYol9c6woWJSe8wFpPC2RC4cvIlZpfg9s=
|
||||
git.milar.in/milarin/slices v0.0.6/go.mod h1:NOr53AOeur/qscu/FBj3lsFR262PNYBccLYSTCAXRk4=
|
||||
git.milar.in/nyaanime/model v0.0.0-20221008104642-466e1111ddea h1:iBwxI3vZ+Hix/5HHB3k9f9/R0nkwR0EvVJ3o5RJEKP4=
|
||||
git.milar.in/nyaanime/model v0.0.0-20221008104642-466e1111ddea/go.mod h1:kPWLDvFrhc1Uf77gxsBOxNeJ5JTVF2HhVs1IdVcw0tg=
|
53
preferred_props.go
Normal file
53
preferred_props.go
Normal file
@ -0,0 +1,53 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"git.milar.in/milarin/gmath"
|
||||
"git.milar.in/milarin/slices"
|
||||
)
|
||||
|
||||
// ParsePreferredProps parses properties and its corresponding priority.
|
||||
// priorities are distributed exponentially in reverse order.
|
||||
//
|
||||
// That means the last entry will have priority 1, the second last 2, then 4, 8 and so on.
|
||||
//
|
||||
// Properties with name "_" will be ignored and function as a placeholder to increase the priority
|
||||
// of the properties which comes before them.
|
||||
//
|
||||
// Properties separated by comma will have the same priorities.
|
||||
//
|
||||
// str usually is the return value of a call to strings.Split(str, "|")
|
||||
//
|
||||
// Examples:
|
||||
// str = "a|b|c" -> c:1 b:2 a:4
|
||||
// str = "a|b|_|c" -> c:1 b:4 a:8
|
||||
// str = "a,b|c" -> c:1 b:4 a:4
|
||||
// str = "d|_|a,b|c" -> c:1 b:4 a:4 d:16
|
||||
//
|
||||
// Additionally, properties can be converted to a generic type with the converter function
|
||||
func ParsePreferredProps[T comparable](str []string, converter func(string) (T, error)) map[T]int {
|
||||
props := map[T]int{}
|
||||
|
||||
for i, subProps := range slices.Reverse(str) {
|
||||
if subProps == "_" {
|
||||
continue
|
||||
}
|
||||
|
||||
propPriority := gmath.Pow(2, i)
|
||||
for _, subProp := range strings.Split(subProps, ",") {
|
||||
subPropT, err := converter(subProp)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
props[subPropT] = propPriority
|
||||
}
|
||||
}
|
||||
|
||||
return props
|
||||
}
|
||||
|
||||
func ParsePreferredStringProps(str []string) map[string]int {
|
||||
return ParsePreferredProps(str, func(s string) (string, error) { return s, nil })
|
||||
}
|
Loading…
Reference in New Issue
Block a user