/
client.go
179 lines (153 loc) · 4.84 KB
/
client.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
package metube
import (
"fmt"
"log"
"time"
"github.com/imroc/req/v3"
)
type Client struct {
URL string `json:"url" xml:"url"`
Debug bool
Client *req.Client
}
func New(url string, debug bool) *Client {
r := req.C().
SetBaseURL(url).
SetTimeout(5 * time.Second)
if debug {
r.DevMode()
}
return &Client{
URL: url,
Debug: debug,
Client: r,
}
}
type Response struct {
Status string `json:"status"`
Message string `json:"msg"`
}
type AddRequest struct {
URL string `json:"url"`
AutoStart bool `json:"auto_start"`
Quality string `json:"quality"`
Format string `json:"format"`
Folder string `json:"folder"`
Name string `json:"custom_name_prefix"`
}
func (m *Client) Add(name, url string, autoStart bool) error {
result := &Response{}
resp, err := m.Client.R().
SetHeader("Accept", "application/json"). // Chainable request settings.
SetSuccessResult(result). // Unmarshal response body into userInfo automatically if status code is between 200 and 299.
SetBody(&AddRequest{url, autoStart, "best", "any", "", name}).
Post("add")
if err != nil { // Error handling.
log.Println("error:", err)
log.Println("raw content:")
log.Println(resp.Dump()) // Record raw content when error occurs.
return fmt.Errorf("failed: %w", err)
}
if resp.IsErrorState() { // Status code >= 400.
return fmt.Errorf("failed: %d: %s: %s", resp.StatusCode, resp.Status, resp.String())
}
if resp.IsSuccessState() { // Status code is between 200 and 299.
if result.Status != "ok" {
return fmt.Errorf("failed: %s", result.Message)
}
}
return nil
}
type DeleteRequest struct {
IDs []string `json:"ids"`
Where string `json:"where"`
}
func (m *Client) Delete(ids []string, where string) error {
result := &Response{}
resp, err := m.Client.R().
SetHeader("Accept", "application/json"). // Chainable request settings.
SetSuccessResult(result). // Unmarshal response body into userInfo automatically if status code is between 200 and 299.
SetBody(&DeleteRequest{ids, where}).
Post("delete")
if err != nil { // Error handling.
log.Println("error:", err)
log.Println("raw content:")
log.Println(resp.Dump()) // Record raw content when error occurs.
return fmt.Errorf("failed: %w", err)
}
if resp.IsErrorState() { // Status code >= 400.
return fmt.Errorf("failed: %d: %s: %s", resp.StatusCode, resp.Status, resp.String())
}
if resp.IsSuccessState() { // Status code is between 200 and 299.
if result.Status != "ok" {
return fmt.Errorf("failed: %s", result.Message)
}
}
return nil
}
type StartRequest struct {
IDs []string `json:"ids"`
}
func (m *Client) Start(ids []string) error {
result := &Response{}
resp, err := m.Client.R().
SetHeader("Accept", "application/json"). // Chainable request settings.
SetSuccessResult(result). // Unmarshal response body into userInfo automatically if status code is between 200 and 299.
SetBody(&StartRequest{ids}).
Post("delete")
if err != nil { // Error handling.
log.Println("error:", err)
log.Println("raw content:")
log.Println(resp.Dump()) // Record raw content when error occurs.
return fmt.Errorf("failed: %w", err)
}
if resp.IsErrorState() { // Status code >= 400.
return fmt.Errorf("failed: %d: %s: %s", resp.StatusCode, resp.Status, resp.String())
}
if resp.IsSuccessState() { // Status code is between 200 and 299.
if result.Status != "ok" {
return fmt.Errorf("failed: %s", result.Message)
}
}
return nil
}
type HistoryResponse struct {
Done []*Download `json:"done"`
Queue []*Download `json:"queue"`
}
type Download struct {
ID string `json:"id"`
Title string `json:"title"`
URL string `json:"url"`
Quality string `json:"quality"`
Format string `json:"format"`
Folder string `json:"folder"`
CustomNamePrefix string `json:"custom_name_prefix"`
Msg string `json:"msg"`
Percent float64 `json:"percent"`
Speed float64 `json:"speed"`
Eta float64 `json:"eta"`
Status string `json:"status"`
Size int64 `json:"size"`
Timestamp int64 `json:"timestamp"`
Error string `json:"error"`
Filename string `json:"filename"`
}
func (m *Client) History() (*HistoryResponse, error) {
result := &HistoryResponse{}
resp, err := m.Client.R().
SetHeader("Accept", "application/json").
SetContentType("application/json").
SetSuccessResult(result).
Get("history")
if err != nil { // Error handling.
log.Println("error:", err)
log.Println("raw content:")
log.Println(resp.Dump()) // Record raw content when error occurs.
return nil, fmt.Errorf("failed: %w", err)
}
if resp.IsErrorState() { // Status code >= 400.
return nil, fmt.Errorf("failed: %d: %s: %s", resp.StatusCode, resp.Status, resp.String())
}
return result, nil
}