Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alist-org
GitHub Repository: alist-org/alist
Path: blob/main/drivers/mediatrack/util.go
1986 views
1
package mediatrack
2
3
import (
4
"errors"
5
"fmt"
6
"net/http"
7
"strconv"
8
9
"github.com/alist-org/alist/v3/drivers/base"
10
"github.com/alist-org/alist/v3/pkg/utils"
11
"github.com/go-resty/resty/v2"
12
log "github.com/sirupsen/logrus"
13
)
14
15
// do others that not defined in Driver interface
16
17
func (d *MediaTrack) request(url string, method string, callback base.ReqCallback, resp interface{}) ([]byte, error) {
18
req := base.RestyClient.R()
19
req.SetHeader("Authorization", "Bearer "+d.AccessToken)
20
if d.DeviceFingerprint != "" {
21
req.SetHeader("X-Device-Fingerprint", d.DeviceFingerprint)
22
}
23
if callback != nil {
24
callback(req)
25
}
26
var e BaseResp
27
req.SetResult(&e)
28
res, err := req.Execute(method, url)
29
if err != nil {
30
return nil, err
31
}
32
log.Debugln(res.String())
33
if e.Status != "SUCCESS" {
34
return nil, errors.New(e.Message)
35
}
36
if resp != nil {
37
err = utils.Json.Unmarshal(res.Body(), resp)
38
}
39
return res.Body(), err
40
}
41
42
func (d *MediaTrack) getFiles(parentId string) ([]File, error) {
43
files := make([]File, 0)
44
url := fmt.Sprintf("https://jayce.api.mediatrack.cn/v4/assets/%s/children", parentId)
45
sort := ""
46
if d.OrderBy != "" {
47
if d.OrderDesc {
48
sort = "-"
49
}
50
sort += d.OrderBy
51
}
52
page := 1
53
for {
54
var resp ChildrenResp
55
_, err := d.request(url, http.MethodGet, func(req *resty.Request) {
56
req.SetQueryParams(map[string]string{
57
"page": strconv.Itoa(page),
58
"size": "50",
59
"sort": sort,
60
})
61
}, &resp)
62
if err != nil {
63
return nil, err
64
}
65
if len(resp.Data.Assets) == 0 {
66
break
67
}
68
page++
69
files = append(files, resp.Data.Assets...)
70
}
71
return files, nil
72
}
73
74