/
main.go
355 lines (307 loc) · 10.6 KB
/
main.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package main
import "bytes"
import "encoding/json"
import "flag"
import "fmt"
import "io/ioutil"
import "log"
import "math/rand"
import "net/http"
import "os"
import "strconv"
import "strings"
import "time"
//import "github.com/davecgh/go-spew/spew"
import "github.com/kardianos/osext"
type sonarrMissingEpisodes struct {
Page int `json:"page"`
PageSize int `json:"pageSize"`
SortKey string `json:"sortKey"`
SortDirection string `json:"sortDirection"`
TotalRecords int `json:"totalRecords"`
Records []struct {
SeriesID int `json:"seriesId"`
EpisodeFileID int `json:"episodeFileId"`
SeasonNumber int `json:"seasonNumber"`
EpisodeNumber int `json:"episodeNumber"`
Title string `json:"title"`
AirDate string `json:"airDate"`
AirDateUtc time.Time `json:"airDateUtc"`
Overview string `json:"overview"`
HasFile bool `json:"hasFile"`
Monitored bool `json:"monitored"`
UnverifiedSceneNumbering bool `json:"unverifiedSceneNumbering"`
Series struct {
Title string `json:"title"`
SortTitle string `json:"sortTitle"`
SeasonCount int `json:"seasonCount"`
Status string `json:"status"`
Overview string `json:"overview"`
Network string `json:"network"`
AirTime string `json:"airTime"`
Images []struct {
CoverType string `json:"coverType"`
URL string `json:"url"`
} `json:"images"`
Seasons []struct {
SeasonNumber int `json:"seasonNumber"`
Monitored bool `json:"monitored"`
} `json:"seasons"`
Year int `json:"year"`
Path string `json:"path"`
ProfileID int `json:"profileId"`
SeasonFolder bool `json:"seasonFolder"`
Monitored bool `json:"monitored"`
UseSceneNumbering bool `json:"useSceneNumbering"`
Runtime int `json:"runtime"`
TvdbID int `json:"tvdbId"`
TvRageID int `json:"tvRageId"`
TvMazeID int `json:"tvMazeId"`
FirstAired time.Time `json:"firstAired"`
LastInfoSync time.Time `json:"lastInfoSync"`
SeriesType string `json:"seriesType"`
CleanTitle string `json:"cleanTitle"`
TitleSlug string `json:"titleSlug"`
Genres []string `json:"genres"`
Tags []interface{} `json:"tags"`
Added time.Time `json:"added"`
Ratings struct {
Votes int `json:"votes"`
Value float64 `json:"value"`
} `json:"ratings"`
QualityProfileID int `json:"qualityProfileId"`
ID int `json:"id"`
} `json:"series"`
ID int `json:"id"`
} `json:"records"`
}
type sonarrEpisode struct {
SeriesID int `json:"seriesId"`
EpisodeFileID int `json:"episodeFileId"`
SeasonNumber int `json:"seasonNumber"`
EpisodeNumber int `json:"episodeNumber"`
Title string `json:"title"`
AirDate string `json:"airDate"`
AirDateUtc time.Time `json:"airDateUtc"`
Overview string `json:"overview"`
HasFile bool `json:"hasFile"`
Monitored bool `json:"monitored"`
AbsoluteEpisodeNumber int `json:"absoluteEpisodeNumber"`
UnverifiedSceneNumbering bool `json:"unverifiedSceneNumbering"`
Series struct {
Title string `json:"title"`
SortTitle string `json:"sortTitle"`
SeasonCount int `json:"seasonCount"`
Status string `json:"status"`
Overview string `json:"overview"`
Network string `json:"network"`
Images []struct {
CoverType string `json:"coverType"`
URL string `json:"url"`
} `json:"images"`
Seasons []struct {
SeasonNumber int `json:"seasonNumber"`
Monitored bool `json:"monitored"`
} `json:"seasons"`
Year int `json:"year"`
Path string `json:"path"`
ProfileID int `json:"profileId"`
SeasonFolder bool `json:"seasonFolder"`
Monitored bool `json:"monitored"`
UseSceneNumbering bool `json:"useSceneNumbering"`
Runtime int `json:"runtime"`
TvdbID int `json:"tvdbId"`
TvRageID int `json:"tvRageId"`
TvMazeID int `json:"tvMazeId"`
FirstAired time.Time `json:"firstAired"`
LastInfoSync time.Time `json:"lastInfoSync"`
SeriesType string `json:"seriesType"`
CleanTitle string `json:"cleanTitle"`
TitleSlug string `json:"titleSlug"`
Certification string `json:"certification"`
Genres []string `json:"genres"`
Tags []interface{} `json:"tags"`
Added time.Time `json:"added"`
Ratings struct {
Votes int `json:"votes"`
Value float64 `json:"value"`
} `json:"ratings"`
QualityProfileID int `json:"qualityProfileId"`
ID int `json:"id"`
} `json:"series"`
ID int `json:"id"`
}
type sonarrSearchResult struct {
Name string `json:"name"`
Body struct {
EpisodeIds []int `json:"episodeIds"`
SendUpdatesToClient bool `json:"sendUpdatesToClient"`
UpdateScheduledTask bool `json:"updateScheduledTask"`
CompletionMessage string `json:"completionMessage"`
Name string `json:"name"`
Trigger string `json:"trigger"`
} `json:"body"`
Priority string `json:"priority"`
Status string `json:"status"`
Queued time.Time `json:"queued"`
Trigger string `json:"trigger"`
State string `json:"state"`
Manual bool `json:"manual"`
StartedOn time.Time `json:"startedOn"`
SendUpdatesToClient bool `json:"sendUpdatesToClient"`
UpdateScheduledTask bool `json:"updateScheduledTask"`
ID int `json:"id"`
}
type sonarrSearchEpisodeCommand struct {
Name string `json:"name"`
EpisodeIds []int `json:"episodeIds"`
}
type configuration struct {
LogLocation string `json:"loglocation"`
HostName string `json:"hostname"`
BaseURL string `json:"baseurl"`
HostPort int `json:"port"`
APIKey string `json:"apikey"`
}
var (
flagIndex = flag.Int("i", -1, "index to search for. (if used, skips the random index and uses defined)")
cfg configuration
)
func main() {
flag.Parse()
cfgpath, _ := osext.ExecutableFolder()
file, _ := os.Open(cfgpath + "/sre.json")
decoder := json.NewDecoder(file)
cfg = configuration{}
err := decoder.Decode(&cfg)
if err != nil {
fmt.Println("error:", err)
}
if cfg.HostName == "" {
cfg.HostName = "http://localhost"
}
if cfg.HostPort == 0 {
cfg.HostPort = 8989
}
if cfg.BaseURL == "" {
cfg.BaseURL = ""
}
if cfg.APIKey == "" {
writeToLog("ERROR: Invalid ApiKey")
return
}
urlRoot := cfg.HostName + ":" + strconv.Itoa(cfg.HostPort)
if cfg.BaseURL != "" {
if strings.Index(cfg.BaseURL, "/") == -1 {
urlRoot += "/"
}
urlRoot += cfg.BaseURL
}
urlRoot += "/api"
apiKey := cfg.APIKey
randomEpisode := getRandomSonarrEpisode(urlRoot, apiKey)
writeToLog(fmt.Sprintf("Random Episode ID: %d", randomEpisode))
fmt.Printf("Random Episode ID: %d\n", randomEpisode)
// Get Episode info
episode := getSonarrEpisodeInfo(urlRoot, apiKey, randomEpisode)
writeToLog(fmt.Sprintf("Searching: %s - S%dE%d - %s", episode.Series.Title, episode.SeasonNumber, episode.EpisodeNumber, episode.Title))
fmt.Printf("Searching: %s - S%dE%d - %s", episode.Series.Title, episode.SeasonNumber, episode.EpisodeNumber, episode.Title)
episodesURL := fmt.Sprintf("%s/command", urlRoot)
writeToLog(episodesURL)
b := new(bytes.Buffer)
json.NewEncoder(b).Encode(struct {
Name string `json:"name"`
EpisodeIds []int
}{
"episodeSearch",
[]int{randomEpisode},
})
writeToLog(b.String())
client := &http.Client{}
req, err := http.NewRequest("POST", episodesURL, b)
req.Header.Add("X-Api-Key", apiKey)
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
writeToLog(fmt.Sprintf("Resp Status: %s", resp.Status))
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
writeToLog(fmt.Sprintf("%s", err.Error()))
os.Exit(1)
}
writeToLog("Response:\n")
writeToLog(string(body))
writeToLog("\n___________________________\n")
}
func getSonarrTotalRecords(urlRoot, apiKey string) int {
page := 1
pageSize := 1
urlPath := fmt.Sprintf("%s/wanted/missing/?apikey=%s&page=%d&pageSize=%d&sortKey=airDateUtc&sortDir=asc", urlRoot, apiKey, page, pageSize)
res, err := http.Get(urlPath)
if err != nil {
panic(err.Error())
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
panic(err.Error())
}
var data sonarrMissingEpisodes
json.Unmarshal(body, &data)
writeToLog(fmt.Sprintf("Total Records: %d", data.TotalRecords))
return data.TotalRecords
}
func getRandomSonarrEpisode(urlRoot, apiKey string) int {
records := getSonarrTotalRecords(urlRoot, apiKey)
page := flagInt(flagIndex)
if flagInt(flagIndex) == -1 {
rand.Seed(time.Now().Unix())
page = rand.Intn(records-1) + 1
}
writeToLog(fmt.Sprintf("Rand Record: %d", page))
pageSize := 1
urlPath := fmt.Sprintf("%s/wanted/missing/?apikey=%s&page=%d&pageSize=%d&sortKey=airDateUtc&sortDir=asc", urlRoot, apiKey, page, pageSize)
res, err := http.Get(urlPath)
if err != nil {
panic(err.Error())
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
panic(err.Error())
}
var data sonarrMissingEpisodes
json.Unmarshal(body, &data)
return data.Records[0].ID
}
func getSonarrEpisodeInfo(urlRoot, apiKey string, episodeId int) (episode sonarrEpisode) {
urlPath := fmt.Sprintf("%s/episode/%d?apikey=%s", urlRoot, episodeId, apiKey)
res, err := http.Get(urlPath)
if err != nil {
panic(err.Error())
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
panic(err.Error())
}
var data sonarrEpisode
json.Unmarshal(body, &data)
return data
}
func flagInt(fs *int) int {
return *fs
}
func writeToLog(str string) {
filename := cfg.LogLocation
if cfg.LogLocation == "" {
filename, _ = osext.ExecutableFolder()
}
f, err := os.OpenFile(filename+"/sre.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatalf("error opening file: %v", err)
}
defer f.Close()
log.SetOutput(f)
log.Println(str)
}