-
Notifications
You must be signed in to change notification settings - Fork 1
/
search.go
120 lines (113 loc) · 2.96 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"time"
"github.com/labstack/gommon/log"
)
func RefreshList(model *SearchModel, search string) {
model.BeginResetModel()
model.SetRows([]*Search{NewSearch(nil)})
model.EndResetModel()
model.RemoveSearch(0)
searchList := SearchForHSnzbs(search)
//add Search
for i := 0; i < len(searchList.Channel.Item); i++ {
publishedDate, err := time.Parse("Mon, 02 Jan 2006 15:04:05 -0700", searchList.Channel.Item[i].PubDate)
if err != nil {
log.Error("Error parsing time/date stamp on item")
}
var p = NewSearch(nil)
p.SetDescription(searchList.Channel.Item[i].Title)
p.SetDate(publishedDate.Format("01/02/2006"))
p.SetId(searchList.Channel.Item[i].GUID[34:])
model.AddSearch(p)
}
}
type SearchResponse struct {
Attributes struct {
Version string `json:"version"`
} `json:"@attributes"`
Channel struct {
Title string `json:"title"`
Description string `json:"description"`
Link string `json:"link"`
Language string `json:"language"`
WebMaster string `json:"webMaster"`
Category struct {
} `json:"category"`
Image struct {
URL string `json:"url"`
Title string `json:"title"`
Link string `json:"link"`
Description string `json:"description"`
} `json:"image"`
Response struct {
Attributes struct {
Offset string `json:"offset"`
Total string `json:"total"`
} `json:"@attributes"`
} `json:"response"`
Item []struct {
Title string `json:"title"`
GUID string `json:"guid"`
Link string `json:"link"`
Comments string `json:"comments"`
PubDate string `json:"pubDate"`
Category string `json:"category"`
Description string `json:"description"`
Enclosure struct {
Attributes struct {
URL string `json:"url"`
Length string `json:"length"`
Type string `json:"type"`
} `json:"@attributes"`
} `json:"enclosure"`
Attr []struct {
Attributes struct {
Name string `json:"name"`
Value string `json:"value"`
} `json:"@attributes"`
} `json:"attr"`
} `json:"item"`
} `json:"channel"`
}
//SearchForHSnzbs is the initial athentication call
func SearchForHSnzbs(search string) SearchResponse {
var b SearchResponse
if s, ok := Settings["nzbsite"]; !ok || s == "" {
return b
}
if k, ok := Settings["nzbkey"]; !ok || k == "" {
return b
}
client := &http.Client{}
u, _ := url.ParseRequestURI(Settings["nzbsite"])
u.Path = "/api"
restpost := u.Query()
restpost.Add("apikey", Settings["nzbkey"])
restpost.Set("o", "json")
restpost.Add("q", search)
restpost.Set("t", "search")
u.RawQuery = restpost.Encode()
resturl := fmt.Sprintf("%v", u)
r, _ := http.NewRequest("GET", resturl, nil)
fmt.Println(r)
res, err := client.Do(r)
if err != nil {
panic(err)
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
panic(err)
}
err = json.Unmarshal(body, &b)
if err != nil {
panic(err)
}
return b
}