Skip to content

Commit

Permalink
fix transfer error
Browse files Browse the repository at this point in the history
  • Loading branch information
qjfoidnh committed Dec 13, 2024
1 parent ea4b9c4 commit 467529a
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 87 deletions.
107 changes: 54 additions & 53 deletions baidupcs/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import (
"path"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
)

type (
Expand All @@ -29,8 +31,8 @@ func (pcs *BaiduPCS) GenerateShareQueryURL(subPath string, params map[string]str
Path: "/share/" + subPath,
}
uv := shareURL.Query()
//uv.Set("time", strconv.Itoa(int(time.Now().UnixMilli())))
//uv.Set("clienttype", "1")
uv.Set("time", strconv.Itoa(int(time.Now().UnixMilli())))
uv.Set("clienttype", "1")
for key, value := range params {
uv.Set(key, value)
}
Expand All @@ -39,7 +41,7 @@ func (pcs *BaiduPCS) GenerateShareQueryURL(subPath string, params map[string]str
return shareURL
}

func (pcs *BaiduPCS) ExtractShareInfo(shareURL string) (res map[string]string) {
func (pcs *BaiduPCS) ExtractShareInfo(shareURL, shardID, shareUK, bdstoken string) (res map[string]string) {
res = make(map[string]string)
dataReadCloser, panError := pcs.sendReqReturnReadCloser(reqTypePan, OperationShareFileSavetoLocal, http.MethodGet, shareURL, nil, map[string]string{
"User-Agent": requester.UserAgent,
Expand All @@ -50,43 +52,43 @@ func (pcs *BaiduPCS) ExtractShareInfo(shareURL string) (res map[string]string) {
return
}
defer dataReadCloser.Close()
//if !strings.Contains(metajsonstr, "server_filename") {
// res["ErrMsg"] = "获取分享文件详情失败"
// return
//}
//errno := gjson.Get(metajsonstr, `file_list.errno`).Int()
//if errno != 0 {
// res["ErrMsg"] = fmt.Sprintf("未知错误, 错误码%d", errno)
// return
//}
//res["filename"] = gjson.Get(metajsonstr, `file_list.0.server_filename`).String()
//fsid_list := gjson.Get(metajsonstr, `file_list.#.fs_id`).Array()
//var fids_str string = "["
//for _, sid := range fsid_list {
// fids_str += sid.String() + ","
//}
//
//res["shareid"] = gjson.Get(metajsonstr, `shareid`).String()
//res["from"] = gjson.Get(metajsonstr, `share_uk`).String()
//res["bdstoken"] = gjson.Get(metajsonstr, `bdstoken`).String()
//shareUrl := &url.URL{
// Scheme: GetHTTPScheme(true),
// Host: PanBaiduCom,
// Path: "/share/transfer",
//}
//uv := shareUrl.Query()
//uv.Set("app_id", PanAppID)
//uv.Set("channel", "chunlei")
//uv.Set("clienttype", "0")
//uv.Set("web", "1")
//for key, value := range res {
// uv.Set(key, value)
//}
//res["item_num"] = strconv.Itoa(len(fsid_list))
//res["ErrMsg"] = "0"
//res["fs_id"] = fids_str[:len(fids_str)-1] + "]"
//shareUrl.RawQuery = uv.Encode()
//res["shareUrl"] = shareUrl.String()
body, _ := ioutil.ReadAll(dataReadCloser)
errno := gjson.Get(string(body), `errno`).Int()
if errno != 0 {
res["ErrMsg"] = fmt.Sprintf("未知错误, 错误码%d", errno)
if errno == 8001 {
res["ErrMsg"] = "已触发验证, 请稍后再试"
}
return
}
res["filename"] = gjson.Get(string(body), `list.0.server_filename`).String()
fsidList := gjson.Get(string(body), `list.#.fs_id`).Array()
var fidsStr string = "["
for _, sid := range fsidList {
fidsStr += sid.String() + ","
}

res["shareid"] = shardID
res["from"] = shareUK
res["bdstoken"] = bdstoken
shareUrl := &url.URL{
Scheme: GetHTTPScheme(true),
Host: PanBaiduCom,
Path: "/share/transfer",
}
uv := shareUrl.Query()
uv.Set("app_id", PanAppID)
uv.Set("channel", "chunlei")
uv.Set("clienttype", "0")
uv.Set("web", "1")
for key, value := range res {
uv.Set(key, value)
}
res["item_num"] = strconv.Itoa(len(fsidList))
res["ErrMsg"] = "success"
res["fs_id"] = fidsStr[:len(fidsStr)-1] + "]"
shareUrl.RawQuery = uv.Encode()
res["shareUrl"] = shareUrl.String()
return
}

Expand Down Expand Up @@ -135,13 +137,13 @@ func (pcs *BaiduPCS) AccessSharePage(featurestr string, first bool) (tokens map[
}
defer dataReadCloser.Close()
body, _ := ioutil.ReadAll(dataReadCloser)
not_found_flag := strings.Contains(string(body), "platform-non-found")
error_page_title := strings.Contains(string(body), "error-404")
if error_page_title {
notFoundFlag := strings.Contains(string(body), "platform-non-found")
errorPageTitle := strings.Contains(string(body), "error-404")
if errorPageTitle {
tokens["ErrMsg"] = "页面不存在"
return
}
if not_found_flag {
if notFoundFlag {
tokens["ErrMsg"] = "分享链接已失效"
return
} else {
Expand All @@ -151,7 +153,6 @@ func (pcs *BaiduPCS) AccessSharePage(featurestr string, first bool) (tokens map[
tokens["ErrMsg"] = "请确认登录参数中已经包含了网盘STOKEN"
return
}
//tokens["metajson"] = string(sub[1])
tokens["bdstoken"] = gjson.Get(string(sub[1]), `bdstoken`).String()
tokens["uk"] = gjson.Get(string(sub[1]), `uk`).String()
tokens["share_uk"] = gjson.Get(string(sub[1]), `share_uk`).String()
Expand Down Expand Up @@ -200,12 +201,12 @@ func (pcs *BaiduPCS) GenerateRequestQuery(mode string, params map[string]string)
path := gjson.Get(string(body), `info.0.path`).String()
_, file := filepath.Split(path) // Should be path.Split here, but never mind~
_errno := gjson.Get(string(body), `info.0.errno`).Int()
target_file_nums := gjson.Get(string(body), `target_file_nums`).Int()
target_file_nums_limit := gjson.Get(string(body), `target_file_nums_limit`).Int()
if target_file_nums > target_file_nums_limit {
targetFileNums := gjson.Get(string(body), `target_file_nums`).Int()
targetFileNumsLimit := gjson.Get(string(body), `target_file_nums_limit`).Int()
if targetFileNums > targetFileNumsLimit {
res["ErrNo"] = "4"
res["ErrMsg"] = fmt.Sprintf("转存文件数%d超过当前用户上限, 当前用户单次最大转存数%d", target_file_nums, target_file_nums_limit)
res["limit"] = fmt.Sprintf("%d", target_file_nums_limit)
res["ErrMsg"] = fmt.Sprintf("转存文件数%d超过当前用户上限, 当前用户单次最大转存数%d", targetFileNums, targetFileNumsLimit)
res["limit"] = fmt.Sprintf("%d", targetFileNumsLimit)
} else if _errno == -30 {
res["ErrNo"] = "9"
res["ErrMsg"] = fmt.Sprintf("当前目录下已有%s同名文件/文件夹", file)
Expand All @@ -219,11 +220,11 @@ func (pcs *BaiduPCS) GenerateRequestQuery(mode string, params map[string]string)
}
_, res["filename"] = filepath.Split(gjson.Get(string(body), `info.0.path`).String())
filenames := gjson.Get(string(body), `info.#.path`).Array()
filenames_str := ""
filenamesStr := ""
for _, _path := range filenames {
filenames_str += "," + path.Base(_path.String())
filenamesStr += "," + path.Base(_path.String())
}
res["filenames"] = filenames_str[1:]
res["filenames"] = filenamesStr[1:]
if len(gjson.Get(string(body), `info.#.fsid`).Array()) > 1 {
res["filename"] += "等多个文件/文件夹"
}
Expand Down
57 changes: 23 additions & 34 deletions internal/pcscommand/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,25 @@ func RunShareTransfer(params []string, opt *baidupcs.TransferOption) {
if link[len(link)-1:] == "/" {
link = link[0 : len(link)-1]
}
featurestrs := strings.Split(link, "/")
featurestr := featurestrs[len(featurestrs)-1]
if strings.Contains(featurestr, "init?") {
featurestr = "1" + strings.Split(featurestr, "=")[1]
featureStrs := strings.Split(link, "/")
featureStr := featureStrs[len(featureStrs)-1]
if strings.Contains(featureStr, "init?") {
featureStr = "1" + strings.Split(featureStr, "=")[1]
}
if len(featurestr) > 23 || featurestr[0:1] != "1" || len(extracode) != 4 {
if len(featureStr) > 23 || featureStr[0:1] != "1" || len(extracode) != 4 {
fmt.Printf("%s失败: %s\n", baidupcs.OperationShareFileSavetoLocal, "链接地址或提取码非法")
return
}
pcs := GetBaiduPCS()
tokens := pcs.AccessSharePage(featurestr, true)
tokens := pcs.AccessSharePage(featureStr, true)
if tokens["ErrMsg"] != "0" {
fmt.Printf("%s失败: %s\n", baidupcs.OperationShareFileSavetoLocal, tokens["ErrMsg"])
return
}
// pcs.UpdatePCSCookies(true)
var vefiryurl string
var randsk string
featuremap := make(map[string]string)
featuremap["shareid"] = tokens["shareid"]
featuremap["uk"] = tokens["share_uk"]
if extracode != "none" {

vefiryurl = pcs.GenerateShareQueryURL("verify", featuremap).String()
res := pcs.PostShareQuery(vefiryurl, link, map[string]string{
"pwd": extracode,
Expand All @@ -67,54 +63,47 @@ func RunShareTransfer(params []string, opt *baidupcs.TransferOption) {
fmt.Printf("%s失败: %s\n", baidupcs.OperationShareFileSavetoLocal, res["ErrMsg"])
return
}
randsk = res["randsk"]
}
pcs.UpdatePCSCookies(true)

tokens = pcs.AccessSharePage(featurestr, false)
tokens["randsk"] = randsk
tokens = pcs.AccessSharePage(featureStr, false)
if tokens["ErrMsg"] != "0" {
fmt.Printf("%s失败: %s\n", baidupcs.OperationShareFileSavetoLocal, tokens["ErrMsg"])
return
}

//sekey, _ := url.QueryUnescape(randsk)
//featuremap["sekey"] = sekey
featuremap["bdstoken"] = tokens["bdstoken"]
featuremap["root"] = "1"
featuremap["web"] = "5"
featuremap["app_id"] = "250528"
featuremap["shorturl"] = featurestr[1:]
//featuremap["devuid"] = baidupcs.
//featuremap["timestamp"] = strconv.Itoa(int(time.Now().Unix()))
featuremap["shorturl"] = featureStr[1:]
featuremap["channel"] = "chunlei"
queryShareInfoUrl := pcs.GenerateShareQueryURL("list", featuremap).String()
//metajsonstr := tokens["metajson"]
trans_metas := pcs.ExtractShareInfo(queryShareInfoUrl)
transMetas := pcs.ExtractShareInfo(queryShareInfoUrl, tokens["shareid"], tokens["share_uk"], tokens["bdstoken"])

if trans_metas["ErrMsg"] != "0" {
fmt.Printf("%s失败: %s\n", baidupcs.OperationShareFileSavetoLocal, trans_metas["ErrMsg"])
if transMetas["ErrMsg"] != "success" {
fmt.Printf("%s失败: %s\n", baidupcs.OperationShareFileSavetoLocal, transMetas["ErrMsg"])
return
}
trans_metas["path"] = GetActiveUser().Workdir
if trans_metas["item_num"] != "1" && opt.Collect {
trans_metas["filename"] += "等文件"
trans_metas["path"] = path.Join(GetActiveUser().Workdir, trans_metas["filename"])
pcs.Mkdir(trans_metas["path"])
transMetas["path"] = GetActiveUser().Workdir
if transMetas["item_num"] != "1" && opt.Collect {
transMetas["filename"] += "等文件"
transMetas["path"] = path.Join(GetActiveUser().Workdir, transMetas["filename"])
pcs.Mkdir(transMetas["path"])
}
trans_metas["referer"] = "https://pan.baidu.com/s/" + featurestr
transMetas["referer"] = "https://pan.baidu.com/s/" + featureStr
pcs.UpdatePCSCookies(true)
resp := pcs.GenerateRequestQuery("POST", trans_metas)
resp := pcs.GenerateRequestQuery("POST", transMetas)
if resp["ErrNo"] != "0" {
fmt.Printf("%s失败: %s\n", baidupcs.OperationShareFileSavetoLocal, resp["ErrMsg"])
if resp["ErrNo"] == "4" {
trans_metas["shorturl"] = featurestr
pcs.SuperTransfer(trans_metas, resp["limit"]) // 试验性功能, 当前未启用
}
//if resp["ErrNo"] == "4" {
// transMetas["shorturl"] = featureStr
// pcs.SuperTransfer(transMetas, resp["limit"]) // 试验性功能, 当前未启用
//}
return
}
if opt.Collect {
resp["filename"] = trans_metas["filename"]
resp["filename"] = transMetas["filename"]
}
fmt.Printf("%s成功, 保存了%s到当前目录\n", baidupcs.OperationShareFileSavetoLocal, resp["filename"])
if opt.Download {
Expand Down

0 comments on commit 467529a

Please sign in to comment.