-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.go
84 lines (65 loc) · 1.34 KB
/
search.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
package main
import (
"encoding/xml"
"fmt"
"strings"
nzb "astuart.co/go-nzb"
apis "astuart.co/goapis"
)
type SearchOptions struct {
Type string
Query string
Filter []string
}
//Search returns items for a type, query tuple.
func Search(opt SearchOptions) ([]Item, error) {
qy := query{T: opt.Type, Q: opt.Query}
is := []Item{}
if cached, ok := localCache.Queries[qy]; ok && !*nc {
is = cached
} else {
o := 0
for len(is) < *num {
res, err := geek.Get("api", apis.Query{
"t": opt.Type,
"q": opt.Query,
"offset": fmt.Sprint(o),
})
if err != nil {
return is, err
}
dec := xml.NewDecoder(res.Body)
m := RespEnv{}
err = dec.Decode(&m)
if err != nil {
return is, err
}
itemFilterLoop:
for _, item := range m.Item {
for _, filter := range opt.Filter {
if strings.Contains(item.Title, filter) {
continue itemFilterLoop
}
}
is = append(is, item)
}
o += len(is)
}
localCache.Queries[qy] = is
}
for i := range is {
localCache.ItemsByLink[is[i].Link] = &is[i]
}
return is, nil
}
//GetNZB encapsulates the cache lookup and retrieval for an NZB
func GetNZB(i Item) (*nzb.NZB, error) {
if n, ok := localCache.Nzbs[i.GUID]; ok {
return &n, nil
}
nz, err := i.GetNzb()
if nz != nil {
localCache.Nzbs[i.GUID] = *nz
}
return nz, err
}