/
caps.go
102 lines (89 loc) · 1.98 KB
/
caps.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package torznab
import (
"encoding/xml"
"net/http"
"sort"
"strings"
)
type Capabilities struct {
SearchModes []SearchMode
Categories Categories
}
func (c Capabilities) HasSearchMode(key string) (bool, []string) {
for _, m := range c.SearchModes {
if m.Key == key && m.Available {
return true, m.SupportedParams
}
}
return false, nil
}
func (c Capabilities) HasTVShows() bool {
for _, cat := range c.Categories {
if cat.ID >= 5000 && cat.ID < 6000 {
return true
}
}
return false
}
func (c Capabilities) HasMovies() bool {
for _, cat := range c.Categories {
if cat.ID >= 2000 && cat.ID < 3000 {
return true
}
}
return false
}
type SearchMode struct {
Key string
Available bool
SupportedParams []string
}
func (c Capabilities) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
var cx struct {
XMLName struct{} `xml:"caps"`
Searching struct {
Values []interface{}
} `xml:"searching"`
Categories struct {
Values []interface{}
} `xml:"categories"`
}
for _, mode := range c.SearchModes {
available := "no"
if mode.Available {
available = "yes"
}
cx.Searching.Values = append(cx.Searching.Values, struct {
XMLName xml.Name
Available string `xml:"available,attr"`
SupportedParams string `xml:"supportedParams,attr"`
}{
xml.Name{"", mode.Key},
available,
strings.Join(mode.SupportedParams, ","),
})
}
cats := c.Categories
sort.Sort(cats)
for _, cat := range cats {
cx.Categories.Values = append(cx.Categories.Values, struct {
XMLName struct{} `xml:"category"`
ID int `xml:"id,attr"`
Name string `xml:"name,attr"`
}{
ID: cat.ID,
Name: cat.Name,
})
}
e.Encode(cx)
return nil
}
func (c Capabilities) ServeHTTP(w http.ResponseWriter, r *http.Request) {
x, err := xml.MarshalIndent(c, "", " ")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/xml")
w.Write(x)
}