From 3ad75e54cb754855c30c97a20dc18993a1d9ccae Mon Sep 17 00:00:00 2001 From: foxxorcat <95907542+foxxorcat@users.noreply.github.com> Date: Sat, 16 Apr 2022 14:52:36 +0800 Subject: [PATCH] refactor(baidu): add a crack api of download MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 修复百度网盘API文件大于20M问题 * refactor: keep the official api Co-authored-by: Xhofe --- drivers/baidu/baidu.go | 19 +++++++------ drivers/baidu/driver.go | 41 ++++++++++++++++++++++++++++ drivers/baidu/types.go | 59 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 109 insertions(+), 10 deletions(-) diff --git a/drivers/baidu/baidu.go b/drivers/baidu/baidu.go index bb8d0c08..fe0b7ca1 100644 --- a/drivers/baidu/baidu.go +++ b/drivers/baidu/baidu.go @@ -48,8 +48,7 @@ func (driver Baidu) refreshToken(account *model.Account) error { return nil } -func (driver Baidu) Request(pathname string, method int, headers, query, form map[string]string, data interface{}, resp interface{}, account *model.Account) ([]byte, error) { - u := "https://pan.baidu.com/rest/2.0" + pathname +func (driver Baidu) Request(fullurl string, method int, headers, query, form map[string]string, data interface{}, resp interface{}, account *model.Account) ([]byte, error) { req := base.RestyClient.R() req.SetQueryParam("access_token", account.AccessToken) if headers != nil { @@ -71,15 +70,15 @@ func (driver Baidu) Request(pathname string, method int, headers, query, form ma var err error switch method { case base.Get: - res, err = req.Get(u) + res, err = req.Get(fullurl) case base.Post: - res, err = req.Post(u) + res, err = req.Post(fullurl) case base.Patch: - res, err = req.Patch(u) + res, err = req.Patch(fullurl) case base.Delete: - res, err = req.Delete(u) + res, err = req.Delete(fullurl) case base.Put: - res, err = req.Put(u) + res, err = req.Put(fullurl) default: return nil, base.ErrNotSupport } @@ -94,7 +93,7 @@ func (driver Baidu) Request(pathname string, method int, headers, query, form ma if err != nil { return nil, err } - return driver.Request(pathname, method, headers, query, form, data, resp, account) + return driver.Request(fullurl, method, headers, query, form, data, resp, account) } return nil, fmt.Errorf("errno: %d, refer to https://pan.baidu.com/union/doc/", errno) } @@ -102,11 +101,11 @@ func (driver Baidu) Request(pathname string, method int, headers, query, form ma } func (driver Baidu) Get(pathname string, params map[string]string, resp interface{}, account *model.Account) ([]byte, error) { - return driver.Request(pathname, base.Get, nil, params, nil, nil, resp, account) + return driver.Request("https://pan.baidu.com/rest/2.0"+pathname, base.Get, nil, params, nil, nil, resp, account) } func (driver Baidu) Post(pathname string, params map[string]string, data interface{}, resp interface{}, account *model.Account) ([]byte, error) { - return driver.Request(pathname, base.Post, nil, params, nil, data, resp, account) + return driver.Request("https://pan.baidu.com/rest/2.0"+pathname, base.Post, nil, params, nil, data, resp, account) } func (driver Baidu) manage(opera string, filelist interface{}, account *model.Account) ([]byte, error) { diff --git a/drivers/baidu/driver.go b/drivers/baidu/driver.go index e1a33f5a..1f17d21b 100644 --- a/drivers/baidu/driver.go +++ b/drivers/baidu/driver.go @@ -58,6 +58,14 @@ func (driver Baidu) Items() []base.Item { Default: "asc", Required: false, }, + { + Name: "internal_type", + Label: "download api", + Type: base.TypeSelect, + Required: true, + Values: "official,crack", + Default: "official", + }, { Name: "client_id", Label: "client id", @@ -125,6 +133,13 @@ func (driver Baidu) Files(path string, account *model.Account) ([]model.File, er } func (driver Baidu) Link(args base.Args, account *model.Account) (*base.Link, error) { + if account.InternalType == "crack" { + return driver.LinkCrack(args, account) + } + return driver.LinkOfficial(args, account) +} + +func (driver Baidu) LinkOfficial(args base.Args, account *model.Account) (*base.Link, error) { file, err := driver.File(args.Path, account) if err != nil { return nil, err @@ -157,6 +172,32 @@ func (driver Baidu) Link(args base.Args, account *model.Account) (*base.Link, er }}, nil } +func (driver Baidu) LinkCrack(args base.Args, account *model.Account) (*base.Link, error) { + file, err := driver.File(args.Path, account) + if err != nil { + return nil, err + } + if file.IsDir() { + return nil, base.ErrNotFile + } + var resp DownloadResp2 + param := map[string]string{ + "target": fmt.Sprintf("[\"%s\"]", utils.Join(account.RootFolder, args.Path)), + "dlink": "1", + "web": "5", + "origin": "dlna", + } + _, err = driver.Request("https://pan.baidu.com/api/filemetas", base.Get, nil, param, nil, nil, &resp, account) + if err != nil { + return nil, err + } + return &base.Link{ + Url: resp.Info[0].Dlink, + Headers: []base.Header{ + {Name: "User-Agent", Value: "pan.baidu.com"}, + }}, nil +} + func (driver Baidu) Path(path string, account *model.Account) (*model.File, []model.File, error) { file, err := driver.File(path, account) if err != nil { diff --git a/drivers/baidu/types.go b/drivers/baidu/types.go index 36a21053..5b4d2a71 100644 --- a/drivers/baidu/types.go +++ b/drivers/baidu/types.go @@ -74,6 +74,65 @@ type DownloadResp struct { RequestId string `json:"request_id"` } +type DownloadResp2 struct { + Errno int `json:"errno"` + Info []struct { + //ExtentTinyint4 int `json:"extent_tinyint4"` + //ExtentTinyint1 int `json:"extent_tinyint1"` + //Bitmap string `json:"bitmap"` + //Category int `json:"category"` + //Isdir int `json:"isdir"` + //Videotag int `json:"videotag"` + Dlink string `json:"dlink"` + //OperID int64 `json:"oper_id"` + //PathMd5 int `json:"path_md5"` + //Wpfile int `json:"wpfile"` + //LocalMtime int `json:"local_mtime"` + /*Thumbs struct { + Icon string `json:"icon"` + URL3 string `json:"url3"` + URL2 string `json:"url2"` + URL1 string `json:"url1"` + } `json:"thumbs"`*/ + //PlaySource int `json:"play_source"` + //Share int `json:"share"` + //FileKey string `json:"file_key"` + //Errno int `json:"errno"` + //LocalCtime int `json:"local_ctime"` + //Rotate int `json:"rotate"` + //Metadata time.Time `json:"metadata"` + //Height int `json:"height"` + //SampleRate int `json:"sample_rate"` + //Width int `json:"width"` + //OwnerType int `json:"owner_type"` + //Privacy int `json:"privacy"` + //ExtentInt3 int64 `json:"extent_int3"` + //RealCategory string `json:"real_category"` + //SrcLocation string `json:"src_location"` + //MetaInfo string `json:"meta_info"` + //ID string `json:"id"` + //Duration int `json:"duration"` + //FileSize string `json:"file_size"` + //Channels int `json:"channels"` + //UseSegment int `json:"use_segment"` + //ServerCtime int `json:"server_ctime"` + //Resolution string `json:"resolution"` + //OwnerID int `json:"owner_id"` + //ExtraInfo string `json:"extra_info"` + //Size int `json:"size"` + //FsID int64 `json:"fs_id"` + //ExtentTinyint3 int `json:"extent_tinyint3"` + //Md5 string `json:"md5"` + //Path string `json:"path"` + //FrameRate int `json:"frame_rate"` + //ExtentTinyint2 int `json:"extent_tinyint2"` + //ServerFilename string `json:"server_filename"` + //ServerMtime int `json:"server_mtime"` + //TkbindID int `json:"tkbind_id"` + } `json:"info"` + RequestID int64 `json:"request_id"` +} + type PrecreateResp struct { Path string `json:"path"` Uploadid string `json:"uploadid"`