2022-09-01 14:13:37 +00:00
|
|
|
package _123
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2023-02-16 14:09:33 +00:00
|
|
|
"strconv"
|
2022-09-01 14:13:37 +00:00
|
|
|
|
|
|
|
"github.com/alist-org/alist/v3/drivers/base"
|
2022-11-10 01:34:48 +00:00
|
|
|
"github.com/alist-org/alist/v3/pkg/utils"
|
2022-09-01 14:13:37 +00:00
|
|
|
"github.com/go-resty/resty/v2"
|
|
|
|
jsoniter "github.com/json-iterator/go"
|
|
|
|
)
|
|
|
|
|
|
|
|
// do others that not defined in Driver interface
|
|
|
|
|
|
|
|
func (d *Pan123) login() error {
|
2022-11-10 01:34:48 +00:00
|
|
|
var body base.Json
|
2022-11-14 09:58:49 +00:00
|
|
|
url := "https://www.123pan.com/a/api/user/sign_in"
|
2022-11-10 01:34:48 +00:00
|
|
|
if utils.IsEmailFormat(d.Username) {
|
|
|
|
body = base.Json{
|
|
|
|
"mail": d.Username,
|
|
|
|
"password": d.Password,
|
|
|
|
"type": 2,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
body = base.Json{
|
|
|
|
"passport": d.Username,
|
|
|
|
"password": d.Password,
|
|
|
|
}
|
|
|
|
}
|
2022-09-01 14:13:37 +00:00
|
|
|
var resp TokenResp
|
2022-11-19 04:24:25 +00:00
|
|
|
res, err := base.RestyClient.R().
|
2022-09-01 14:13:37 +00:00
|
|
|
SetResult(&resp).
|
2022-11-10 01:34:48 +00:00
|
|
|
SetBody(body).Post(url)
|
2022-09-01 14:13:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-11-19 04:24:25 +00:00
|
|
|
if utils.Json.Get(res.Body(), "code").ToInt() != 200 {
|
|
|
|
err = fmt.Errorf(utils.Json.Get(res.Body(), "message").ToString())
|
2022-09-01 14:13:37 +00:00
|
|
|
} else {
|
|
|
|
d.AccessToken = resp.Data.Token
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-09-02 14:46:31 +00:00
|
|
|
func (d *Pan123) request(url string, method string, callback base.ReqCallback, resp interface{}) ([]byte, error) {
|
2022-09-01 14:13:37 +00:00
|
|
|
req := base.RestyClient.R()
|
2022-11-14 09:58:49 +00:00
|
|
|
req.SetHeaders(map[string]string{
|
|
|
|
"origin": "https://www.123pan.com",
|
|
|
|
"authorization": "Bearer " + d.AccessToken,
|
|
|
|
"platform": "web",
|
|
|
|
"app-version": "1.2",
|
|
|
|
})
|
2022-09-01 14:13:37 +00:00
|
|
|
if callback != nil {
|
|
|
|
callback(req)
|
|
|
|
}
|
|
|
|
if resp != nil {
|
|
|
|
req.SetResult(resp)
|
|
|
|
}
|
|
|
|
res, err := req.Execute(method, url)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
body := res.Body()
|
2022-11-19 04:24:25 +00:00
|
|
|
code := utils.Json.Get(body, "code").ToInt()
|
2022-09-01 14:13:37 +00:00
|
|
|
if code != 0 {
|
|
|
|
if code == 401 {
|
|
|
|
err := d.login()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return d.request(url, method, callback, resp)
|
|
|
|
}
|
|
|
|
return nil, errors.New(jsoniter.Get(body, "message").ToString())
|
|
|
|
}
|
|
|
|
return body, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Pan123) getFiles(parentId string) ([]File, error) {
|
2023-02-16 14:09:33 +00:00
|
|
|
page := 1
|
2022-09-01 14:13:37 +00:00
|
|
|
res := make([]File, 0)
|
2023-02-16 14:09:33 +00:00
|
|
|
for {
|
2022-09-01 14:13:37 +00:00
|
|
|
var resp Files
|
|
|
|
query := map[string]string{
|
|
|
|
"driveId": "0",
|
|
|
|
"limit": "100",
|
2023-02-16 14:09:33 +00:00
|
|
|
"next": "0",
|
2022-09-01 14:13:37 +00:00
|
|
|
"orderBy": d.OrderBy,
|
|
|
|
"orderDirection": d.OrderDirection,
|
|
|
|
"parentFileId": parentId,
|
|
|
|
"trashed": "false",
|
2023-02-16 14:09:33 +00:00
|
|
|
"Page": strconv.Itoa(page),
|
2022-09-01 14:13:37 +00:00
|
|
|
}
|
|
|
|
_, err := d.request("https://www.123pan.com/api/file/list/new", http.MethodGet, func(req *resty.Request) {
|
|
|
|
req.SetQueryParams(query)
|
|
|
|
}, &resp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-02-16 14:09:33 +00:00
|
|
|
page++
|
2022-09-01 14:13:37 +00:00
|
|
|
res = append(res, resp.Data.InfoList...)
|
2023-02-16 14:09:33 +00:00
|
|
|
if len(resp.Data.InfoList) == 0 || resp.Data.Next == "-1" {
|
|
|
|
break
|
|
|
|
}
|
2022-09-01 14:13:37 +00:00
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|