-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhistory.go
146 lines (124 loc) · 3.72 KB
/
history.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
package radarr
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"time"
)
// History return your Radarr history
type History struct {
Page int `json:"page"`
PageSize int `json:"pageSize"`
SortKey string `json:"sortKey"`
SortDirection string `json:"sortDirection"`
TotalRecords int `json:"totalRecords"`
Records []Record `json:"records"`
}
// Revision movie revisions
type Revision struct {
Version int `json:"version"`
Real int `json:"real"`
IsRepack bool `json:"isRepack"`
}
// Images movie images
type Images struct {
CoverType string `json:"coverType"`
URL string `json:"url"`
}
// Ratings your movies ratings
type Ratings struct {
Votes int `json:"votes"`
Value float64 `json:"value"`
}
// Data contains arbitrary data
type Data struct {
Reason string `json:"reason,omitempty"`
DroppedPath string `json:"droppedPath,omitempty"`
ImportedPath string `json:"importedPath,omitempty"`
Indexer string `json:"indexer,omitempty"`
NzbInfoURL string `json:"nzbInfoUrl,omitempty"`
ReleaseGroup string `json:"releaseGroup,omitempty"`
Age string `json:"age,omitempty"`
AgeHours string `json:"ageHours,omitempty"`
AgeMinutes string `json:"ageMinutes,omitempty"`
PublishedDate time.Time `json:"publishedDate,omitempty"`
DownloadClient string `json:"downloadClient,omitempty"`
Size string `json:"size,omitempty"`
DownloadURL string `json:"downloadUrl,omitempty"`
GUID string `json:"guid,omitempty"`
TvdbID string `json:"tvdbId,omitempty"`
TvRageID string `json:"tvRageId,omitempty"`
Protocol string `json:"protocol,omitempty"`
IndexerFlags string `json:"indexerFlags,omitempty"`
IndexerID string `json:"indexerId,omitempty"`
TorrentInfoHash string `json:"torrentInfoHash,omitempty"`
}
// Record history item
type Record struct {
MovieID int `json:"movieId"`
SourceTitle string `json:"sourceTitle"`
Quality Quality `json:"quality"`
QualityCutoffNotMet bool `json:"qualityCutoffNotMet"`
Date time.Time `json:"date"`
EventType string `json:"eventType"`
Movie Movie `json:"movie"`
ID int `json:"id"`
DownloadID string `json:"downloadId,omitempty"`
Data Data `json:"data,omitempty"`
}
// Records is a set of Record
type Records = []Record
// HistoryService perform actions on your Radarr history
type HistoryService struct {
s *Service
}
func newHistoryService(s *Service) *HistoryService {
return &HistoryService{s: s}
}
// Get return all history
func (s *HistoryService) Get() (*Records, error) {
// First call
var page int = 1
history, err := s.paginate(page)
if err != nil {
return nil, err
}
for {
if len(history.Records) == history.TotalRecords {
break
}
page++
s, err := s.paginate(page)
if err != nil {
break
}
history.Records = append(history.Records, s.Records...)
}
return &history.Records, nil
}
func (s *HistoryService) paginate(page int) (*History, error) {
request, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/api%s", s.s.url, historyURI), nil)
if err != nil {
return nil, err
}
q := request.URL.Query()
q.Add("page", strconv.Itoa(page))
q.Add("pageSize", "50")
request.URL.RawQuery = q.Encode()
resp, err := s.s.client.Do(request)
if err != nil {
return nil, err
}
defer resp.Body.Close()
err = parseRadarrResponse(resp)
if err != nil {
return nil, err
}
var history History
err = json.NewDecoder(resp.Body).Decode(&history)
if err != nil {
return nil, err
}
return &history, nil
}