-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmagnets.go
239 lines (186 loc) · 6.67 KB
/
magnets.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
package alldebrid
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
)
//MagnetsUploadResponse is the response of the upload call
type MagnetsUploadResponse struct {
Status string `json:"status"`
Data magnetsUploadResponseData `json:"data,omitempty"`
Error alldebridError `json:"error,omitempty"`
}
type magnetsUploadResponseData struct {
Magnets []magnetData `json:"magnets"`
}
type magnetData struct {
Magnet string `json:"magnet"`
Hash string `json:"hash,omitempty"`
Name string `json:"name,omitempty"`
Size int `json:"size,omitempty"`
Ready bool `json:"ready,omitempty"`
ID int `json:"id,omitempty"`
Error alldebridError `json:"error,omitempty"`
}
//StatusMagnetResponse is the response of the status call
type StatusMagnetResponse struct {
Status string `json:"status"`
Data statusMagnetResponseData `json:"data,omitempty"`
Error alldebridError `json:"error,omitempty"`
}
type statusMagnetResponseData struct {
Magnets []statusMagnet `json:"magnets"`
Type string `json:"type"`
}
type statusMagnet struct {
ID int `json:"id"`
Filename string `json:"filename"`
Size int `json:"size"`
Hash string `json:"hash"`
Status string `json:"status"`
StatusCode int `json:"statusCode"`
Downloaded int `json:"downloaded"`
Uploaded int `json:"uploaded"`
Seeders int `json:"seeders"`
DownloadSpeed int `json:"downloadSpeed"`
UploadSpeed int `json:"uploadSpeed"`
UploadDate int `json:"uploadDate"`
Links []statusMagnetResponseLinks `json:"links"`
}
type statusMagnetResponseLinks struct {
Link string `json:"link"`
Filename string `json:"filename"`
Size int `json:"size"`
Files interface{} `json:"files"`
}
//DeleteMagnetResponse is the response of the delete call
type DeleteMagnetResponse struct {
Status string `json:"status"`
Data magnetResponseData `json:"data,omitempty"`
Error alldebridError `json:"error,omitempty"`
}
type magnetResponseData struct {
Message string `json:"message"`
}
//RestartMagnetResponse is the response of the restart call
type RestartMagnetResponse struct {
Status string `json:"status"`
Data magnetResponseData `json:"data,omitempty"`
Error alldebridError `json:"error,omitempty"`
}
//InstantAvailabilityResponse is the response of the instant availability call
type InstantAvailabilityResponse struct {
Status string `json:"status"`
Data instantAvailabilityResponseData `json:"data,omitempty"`
Error alldebridError `json:"error,omitempty"`
}
type instantAvailabilityResponseData struct {
Magnets []instantAvailabilityMagnet `json:"magnets"`
}
type instantAvailabilityMagnet struct {
Magnet string `json:"magnet"`
Hash string `json:"hash"`
Instant bool `json:"instant"`
}
// UploadMagnet sends magnet(s) to AllDebrid
func (c *Client) UploadMagnet(magnets []string) (MagnetsUploadResponse, error) {
client := &http.Client{}
ms := url.Values{}
for _, magnet := range magnets {
ms.Add("magnets[]", magnet)
}
resp, err := client.PostForm(fmt.Sprintf(magnetupload, getMagnetEndpoint(), c.ic.appName, c.ic.apikey), ms)
if err != nil {
return MagnetsUploadResponse{}, err
}
defer resp.Body.Close()
decoder := json.NewDecoder(resp.Body)
var uploadResponse MagnetsUploadResponse
err = decoder.Decode(&uploadResponse)
if err != nil {
return MagnetsUploadResponse{}, err
}
if uploadResponse.Status != "success" {
return MagnetsUploadResponse{}, errors.New(uploadResponse.Error.Message)
}
return uploadResponse, nil
}
//StatusMagnet returns the status of an Alldebrid download
func (c *Client) StatusMagnet(id string) (StatusMagnetResponse, error) {
resp, err := http.Get(fmt.Sprintf(magnetstatus, getMagnetEndpoint(), c.ic.appName, c.ic.apikey, id))
if err != nil {
return StatusMagnetResponse{}, err
}
defer resp.Body.Close()
decoder := json.NewDecoder(resp.Body)
var statusResponse StatusMagnetResponse
err = decoder.Decode(&statusResponse)
if err != nil {
return StatusMagnetResponse{}, err
}
if statusResponse.Status != "success" {
return StatusMagnetResponse{}, errors.New(statusResponse.Error.Message)
}
return statusResponse, nil
}
//DeleteMagnet removes a download from alldebrid
func (c *Client) DeleteMagnet(id string) (DeleteMagnetResponse, error) {
resp, err := http.Get(fmt.Sprintf(magnetdelete, getMagnetEndpoint(), c.ic.appName, c.ic.apikey, id))
if err != nil {
return DeleteMagnetResponse{}, err
}
defer resp.Body.Close()
decoder := json.NewDecoder(resp.Body)
var deleteResponse DeleteMagnetResponse
err = decoder.Decode(&deleteResponse)
if err != nil {
return DeleteMagnetResponse{}, err
}
if deleteResponse.Status != "success" {
return DeleteMagnetResponse{}, errors.New(deleteResponse.Error.Message)
}
return deleteResponse, nil
}
//RestartMagnet will restart a failed torrent
func (c *Client) RestartMagnet(id string) (RestartMagnetResponse, error) {
resp, err := http.Get(fmt.Sprintf(magnetrestart, getMagnetEndpoint(), c.ic.appName, c.ic.apikey, id))
if err != nil {
return RestartMagnetResponse{}, err
}
defer resp.Body.Close()
decoder := json.NewDecoder(resp.Body)
var restartResponse RestartMagnetResponse
err = decoder.Decode(&restartResponse)
if err != nil {
return RestartMagnetResponse{}, err
}
if restartResponse.Status != "success" {
return RestartMagnetResponse{}, errors.New(restartResponse.Error.Message)
}
return restartResponse, nil
}
//InstantAvailability sends magnet(s) to AllDebrid to know if thery are already available
func (c *Client) InstantAvailability(magnets []string) (InstantAvailabilityResponse, error) {
client := &http.Client{}
ms := url.Values{}
for _, magnet := range magnets {
ms.Add("magnets[]", magnet)
}
resp, err := client.PostForm(fmt.Sprintf(magnetinstant, getMagnetEndpoint(), c.ic.appName, c.ic.apikey), ms)
if err != nil {
return InstantAvailabilityResponse{}, err
}
defer resp.Body.Close()
decoder := json.NewDecoder(resp.Body)
var instantResponse InstantAvailabilityResponse
err = decoder.Decode(&instantResponse)
if err != nil {
return InstantAvailabilityResponse{}, err
}
if instantResponse.Status != "success" {
return InstantAvailabilityResponse{}, errors.New(instantResponse.Error.Message)
}
return instantResponse, nil
}