-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgonarr.go
201 lines (176 loc) · 4.85 KB
/
gonarr.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package gonarr
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
)
// Gonarr ...
type Gonarr struct {
Hostname string `json:"hostname"`
Port int `json:"port"`
ApiPrefix string `json:"api_prefix"`
Username string `json:"username"`
Password string `json:"password"`
APIKey string `json:"api_key"`
Opts GonarrOptions `json:"-"`
}
func (g Gonarr) String() string {
j, err := json.MarshalIndent(g, "", " ")
if err != nil {
return err.Error()
}
return string(j)
}
// NewGonarrFromConfigFile creates and returns a pointer to a new
// Gonarr object, from a persisted JSON configuration file at the
// specified path.
func NewGonarrFromOptions(opts GonarrOptions) *Gonarr {
path := opts.Config
fh, err := os.Open(path)
if err != nil {
log.Fatal(err)
}
defer fh.Close()
config, err := ioutil.ReadAll(fh)
if err != nil {
log.Fatal(err)
}
g := &Gonarr{}
json.Unmarshal(config, g)
g.Opts = opts
return g
}
func (g *Gonarr) makeGetRequest(command string) string {
return g.makeRequest("GET", command, nil)
}
func (g *Gonarr) makePostRequest(command string, payload io.Reader) string {
return g.makeRequest("POST", command, payload)
}
func (g *Gonarr) makePutRequest(command string, payload io.Reader) string {
return g.makeRequest("PUT", command, payload)
}
func (g *Gonarr) makeRequest(method string, command string, payload io.Reader) string {
url := fmt.Sprintf("http://%s:%d/%s%s",
g.Hostname, g.Port, g.ApiPrefix, command)
fmt.Println(url)
req, err := http.NewRequest(method, url, payload)
req.Header.Add("X-Api-Key", g.APIKey)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
return string(body)
}
func (g *Gonarr) UpdateOneSeries(cmd MySeries) string {
url := fmt.Sprintf("series")
r := g.toJSONB(cmd)
return g.makePutRequest(url, r)
}
// RefreshSeries invokes the RefreshSeries API command
func (g *Gonarr) RefreshSeries(seriesID int) string {
url := fmt.Sprintf("command/RefreshSeries?seriesId=%d", seriesID)
return g.makePostRequest(url, nil)
}
// RescanSeries invokes the RescanSeries API command
func (g *Gonarr) RescanSeries(seriesID int) string {
url := fmt.Sprintf("command/RescanSeries?seriesId=%d", seriesID)
return g.makePostRequest(url, nil)
}
// SeasonSearch invokes the SeasonSearch API command
func (g *Gonarr) SonarrCommand(commandName string, seriesID int, seasonNumber int) string {
cmd := NewSonarrCommand(commandName, seriesID, seasonNumber)
r := g.toJSONB(cmd)
return g.makePostRequest("command", r)
}
// SeriesSearch invokes the SeasonSearch API command
func (g *Gonarr) SeriesSearch(seriesID int) string {
url := fmt.Sprintf("command/SeriesSearch?seriesId=%d",
seriesID)
return g.makePostRequest(url, nil)
}
// ListCommands ...
func (g *Gonarr) ListCommands() string {
// return g.makeGetRequest("commands")
return g.makeGetRequest("command")
}
func (g *Gonarr) ListCommand(commandId int) string {
url := fmt.Sprintf("command/%d", commandId)
return g.makeGetRequest(url)
}
// ListCalendar ...
func (g *Gonarr) ListCalendar() string {
return g.makeGetRequest("calendar")
}
// GetAllSeries lists all series in your collection
func (g *Gonarr) GetAllSeries() MySeriesList {
b := g.makeGetRequest("series")
var mySeriesList MySeriesList
json.Unmarshal([]byte(b), &mySeriesList)
return mySeriesList
}
func (g *Gonarr) GetOneSeries(seriesID int) MySeries {
url := fmt.Sprintf("series/%d", seriesID)
b := g.makeGetRequest(url)
var mySeries MySeries
json.Unmarshal([]byte(b), &mySeries)
return mySeries
}
func (g *Gonarr) GetSystemStatus() string {
return g.makeGetRequest("system/status")
}
// SearchSeries searches for series matching the search term
func (g *Gonarr) SearchSeries(searchTerm string) SeriesInformationList {
// FIXME: Escape things like spaces, etc.
url := fmt.Sprintf("series/lookup?term=%s", searchTerm)
b := g.makeGetRequest(url)
var seriesInformationList SeriesInformationList
json.Unmarshal([]byte(b), &seriesInformationList)
return seriesInformationList
}
func (g *Gonarr) PrintJSON(i interface{}) {
fmt.Println(toJSON(i))
}
func (g *Gonarr) Filter(i interface{}, keys ...string) interface{} {
mkeys := make(map[string]bool)
for _, k := range keys {
mkeys[k] = true
}
sl, ok := i.([]interface{})
if ok {
var o []interface{}
for _, e := range sl {
o = append(o, g.Filter(e, keys...))
}
return o
}
m, ok := i.(map[string]interface{})
if ok {
o := make(map[string]interface{})
for k, v := range m {
if _, haskey := mkeys[k]; haskey {
o[k] = g.Filter(v, keys...)
}
}
return o
}
return i
}
func (g *Gonarr) toJSONB(i interface{}) io.Reader {
jsonb := toJSONB(i)
if g.Opts.DebugOut {
fmt.Println(string(jsonb))
}
r := bytes.NewBuffer(jsonb)
return r
}