/
catmap.go
68 lines (55 loc) · 1.53 KB
/
catmap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package indexer
import "github.com/cardigann/cardigann/torznab"
type categoryMap map[string]torznab.Category
func (mapping categoryMap) Categories() torznab.Categories {
cats := torznab.Categories{}
added := map[int]bool{}
for _, c := range mapping {
if _, exists := added[c.ID]; exists {
continue
}
cats = append(cats, c)
added[c.ID] = true
}
return cats
}
func (mapping categoryMap) Resolve(cat torznab.Category) []string {
var matched bool
var results = []string{}
// check for exact matches
for localID, mappedCat := range mapping {
if mappedCat.ID == cat.ID {
results = append(results, localID)
matched = true
}
}
// check for matches on the parent categories of the mapped categories
// e.g asked for Movies, but only had a more specific mapping for Movies/Blu-ray
if !matched {
for localID, mappedCat := range mapping {
if torznab.ParentCategory(mappedCat).ID == cat.ID {
results = append(results, localID)
matched = true
}
}
}
// finally check for matches on the parent category of the requested cat
// e.g. asked for Movies/Blu-ray but no mapping, so try Movies instead
if !matched {
parent := torznab.ParentCategory(cat)
for localID, mappedCat := range mapping {
if mappedCat.ID == parent.ID {
results = append(results, localID)
matched = true
}
}
}
return results
}
func (mapping categoryMap) ResolveAll(cats ...torznab.Category) []string {
results := []string{}
for _, cat := range cats {
results = append(results, mapping.Resolve(cat)...)
}
return results
}