-
-
Notifications
You must be signed in to change notification settings - Fork 126
/
red.go
241 lines (200 loc) · 6.38 KB
/
red.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
// Copyright (c) 2021 - 2024, Ludvig Lundgren and the autobrr contributors.
// SPDX-License-Identifier: GPL-2.0-or-later
package red
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"time"
"github.com/autobrr/autobrr/internal/domain"
"github.com/autobrr/autobrr/pkg/errors"
"github.com/autobrr/autobrr/pkg/sharedhttp"
"golang.org/x/time/rate"
)
const DefaultURL = "https://redacted.ch/ajax.php"
type ApiClient interface {
GetTorrentByID(ctx context.Context, torrentID string) (*domain.TorrentBasic, error)
TestAPI(ctx context.Context) (bool, error)
}
type Client struct {
url string
client *http.Client
rateLimiter *rate.Limiter
APIKey string
}
type OptFunc func(*Client)
func WithUrl(url string) OptFunc {
return func(c *Client) {
c.url = url
}
}
func NewClient(apiKey string, opts ...OptFunc) ApiClient {
c := &Client{
url: DefaultURL,
client: &http.Client{
Timeout: time.Second * 30,
Transport: sharedhttp.Transport,
},
rateLimiter: rate.NewLimiter(rate.Every(10*time.Second), 10),
APIKey: apiKey,
}
for _, opt := range opts {
opt(c)
}
return c
}
type ErrorResponse struct {
Status string `json:"status"`
Error string `json:"error,omitempty"`
}
type TorrentDetailsResponse struct {
Status string `json:"status"`
Response struct {
Group Group `json:"group"`
Torrent Torrent `json:"torrent"`
} `json:"response"`
Error string `json:"error,omitempty"`
}
type Group struct {
//WikiBody string `json:"wikiBody"`
//WikiImage string `json:"wikiImage"`
Id int `json:"id"`
Name string `json:"name"`
Year int `json:"year"`
RecordLabel string `json:"recordLabel"`
CatalogueNumber string `json:"catalogueNumber"`
ReleaseType int `json:"releaseType"`
CategoryId int `json:"categoryId"`
CategoryName string `json:"categoryName"`
Time string `json:"time"`
VanityHouse bool `json:"vanityHouse"`
//MusicInfo struct {
// Composers []interface{} `json:"composers"`
// Dj []interface{} `json:"dj"`
// Artists []struct {
// Id int `json:"id"`
// Name string `json:"name"`
// } `json:"artists"`
// With []struct {
// Id int `json:"id"`
// Name string `json:"name"`
// } `json:"with"`
// Conductor []interface{} `json:"conductor"`
// RemixedBy []interface{} `json:"remixedBy"`
// Producer []interface{} `json:"producer"`
//} `json:"musicInfo"`
}
type Torrent struct {
Id int `json:"id"`
InfoHash string `json:"infoHash"`
Media string `json:"media"`
Format string `json:"format"`
Encoding string `json:"encoding"`
Remastered bool `json:"remastered"`
RemasterYear int `json:"remasterYear"`
RemasterTitle string `json:"remasterTitle"`
RemasterRecordLabel string `json:"remasterRecordLabel"`
RemasterCatalogueNumber string `json:"remasterCatalogueNumber"`
Scene bool `json:"scene"`
HasLog bool `json:"hasLog"`
HasCue bool `json:"hasCue"`
LogScore int `json:"logScore"`
FileCount int `json:"fileCount"`
Size int `json:"size"`
Seeders int `json:"seeders"`
Leechers int `json:"leechers"`
Snatched int `json:"snatched"`
FreeTorrent bool `json:"freeTorrent"`
IsNeutralleech bool `json:"isNeutralleech"`
IsFreeload bool `json:"isFreeload"`
Time string `json:"time"`
Description string `json:"description"`
FileList string `json:"fileList"`
FilePath string `json:"filePath"`
UserId int `json:"userId"`
Username string `json:"username"`
}
func (c *Client) Do(req *http.Request) (*http.Response, error) {
//ctx := context.Background()
err := c.rateLimiter.Wait(req.Context()) // This is a blocking call. Honors the rate limit
if err != nil {
return nil, err
}
resp, err := c.client.Do(req)
if err != nil {
return resp, err
}
return resp, nil
}
func (c *Client) get(ctx context.Context, url string) (*http.Response, error) {
if c.APIKey == "" {
return nil, errors.New("RED client missing API key!")
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, http.NoBody)
if err != nil {
return nil, errors.Wrap(err, "could not build request")
}
req.Header.Add("Authorization", c.APIKey)
req.Header.Set("User-Agent", "autobrr")
res, err := c.Do(req)
if err != nil {
return res, errors.Wrap(err, "could not make request: %+v", req)
}
// return early if not OK
if res.StatusCode != http.StatusOK {
var r ErrorResponse
body, readErr := io.ReadAll(res.Body)
if readErr != nil {
return res, errors.Wrap(readErr, "could not read body")
}
if err = json.Unmarshal(body, &r); err != nil {
return res, errors.Wrap(readErr, "could not unmarshal body")
}
res.Body.Close()
return res, errors.New("status code: %d status: %s error: %s", res.StatusCode, r.Status, r.Error)
}
return res, nil
}
func (c *Client) GetTorrentByID(ctx context.Context, torrentID string) (*domain.TorrentBasic, error) {
if torrentID == "" {
return nil, errors.New("red client: must have torrentID")
}
var r TorrentDetailsResponse
v := url.Values{}
v.Add("id", torrentID)
params := v.Encode()
reqUrl := fmt.Sprintf("%s?action=torrent&%s", c.url, params)
resp, err := c.get(ctx, reqUrl)
if err != nil {
return nil, errors.Wrap(err, "could not get torrent by id: %v", torrentID)
}
defer resp.Body.Close()
body, readErr := io.ReadAll(resp.Body)
if readErr != nil {
return nil, errors.Wrap(readErr, "could not read body")
}
if err := json.Unmarshal(body, &r); err != nil {
return nil, errors.Wrap(readErr, "could not unmarshal body")
}
return &domain.TorrentBasic{
Id: strconv.Itoa(r.Response.Torrent.Id),
InfoHash: r.Response.Torrent.InfoHash,
Size: strconv.Itoa(r.Response.Torrent.Size),
}, nil
}
// TestAPI try api access against torrents page
func (c *Client) TestAPI(ctx context.Context) (bool, error) {
resp, err := c.get(ctx, c.url+"?action=index")
if err != nil {
return false, errors.Wrap(err, "test api error")
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return false, nil
}
return true, nil
}