github.com/qjfoidnh/BaiduPCS-Go@v0.0.0-20231011165705-caa18a3765f3/internal/pcscommand/transfer.go (about) 1 package pcscommand 2 3 import ( 4 "encoding/base64" 5 "fmt" 6 "path" 7 "regexp" 8 "strconv" 9 "strings" 10 11 "github.com/qjfoidnh/BaiduPCS-Go/baidupcs" 12 ) 13 14 // RunShareTransfer 执行分享链接转存到网盘 15 func RunShareTransfer(params []string, opt *baidupcs.TransferOption) { 16 var link string 17 var extracode string 18 if len(params) == 1 { 19 link = params[0] 20 if strings.Contains(link, "bdlink=") || !strings.Contains(link, "pan.baidu.com/") { 21 RunRapidTransfer(link, opt.Rname) 22 //fmt.Printf("%s失败: %s\n", baidupcs.OperationShareFileSavetoLocal, "秒传已不再被支持") 23 return 24 } 25 extracode = "none" 26 if strings.Contains(link, "?pwd=") { 27 extracode = strings.Split(link, "?pwd=")[1] 28 link = strings.Split(link, "?pwd=")[0] 29 } 30 } else if len(params) == 2 { 31 link = params[0] 32 extracode = params[1] 33 } 34 if link[len(link)-1:] == "/" { 35 link = link[0 : len(link)-1] 36 } 37 featurestrs := strings.Split(link, "/") 38 featurestr := featurestrs[len(featurestrs)-1] 39 if strings.Contains(featurestr, "init?") { 40 featurestr = "1" + strings.Split(featurestr, "=")[1] 41 } 42 if len(featurestr) > 23 || featurestr[0:1] != "1" || len(extracode) != 4 { 43 fmt.Printf("%s失败: %s\n", baidupcs.OperationShareFileSavetoLocal, "链接地址或提取码非法") 44 return 45 } 46 pcs := GetBaiduPCS() 47 tokens := pcs.AccessSharePage(featurestr, true) 48 if tokens["ErrMsg"] != "0" { 49 fmt.Printf("%s失败: %s\n", baidupcs.OperationShareFileSavetoLocal, tokens["ErrMsg"]) 50 return 51 } 52 // pcs.UpdatePCSCookies(true) 53 var vefiryurl string 54 var randsk string 55 featuremap := make(map[string]string) 56 featuremap["bdstoken"] = tokens["bdstoken"] 57 featuremap["surl"] = featurestr[1:len(featurestr)] 58 if extracode != "none" { 59 60 vefiryurl = pcs.GenerateShareQueryURL("verify", featuremap).String() 61 res := pcs.PostShareQuery(vefiryurl, link, map[string]string{ 62 "pwd": extracode, 63 "vcode": "", 64 "vcode_str": "", 65 }) 66 if res["ErrMsg"] != "0" { 67 fmt.Printf("%s失败: %s\n", baidupcs.OperationShareFileSavetoLocal, res["ErrMsg"]) 68 return 69 } 70 randsk = res["randsk"] 71 } 72 pcs.UpdatePCSCookies(true) 73 74 tokens = pcs.AccessSharePage(featurestr, false) 75 tokens["randsk"] = randsk 76 if tokens["ErrMsg"] != "0" { 77 fmt.Printf("%s失败: %s\n", baidupcs.OperationShareFileSavetoLocal, tokens["ErrMsg"]) 78 return 79 } 80 metajsonstr := tokens["metajson"] 81 trans_metas := pcs.ExtractShareInfo(metajsonstr) 82 83 if trans_metas["ErrMsg"] != "0" { 84 fmt.Printf("%s失败: %s\n", baidupcs.OperationShareFileSavetoLocal, trans_metas["ErrMsg"]) 85 return 86 } 87 trans_metas["path"] = GetActiveUser().Workdir 88 if trans_metas["item_num"] != "1" && opt.Collect { 89 trans_metas["filename"] += "等文件" 90 trans_metas["path"] = path.Join(GetActiveUser().Workdir, trans_metas["filename"]) 91 pcs.Mkdir(trans_metas["path"]) 92 } 93 trans_metas["referer"] = "https://pan.baidu.com/s/" + featurestr 94 pcs.UpdatePCSCookies(true) 95 resp := pcs.GenerateRequestQuery("POST", trans_metas) 96 if resp["ErrNo"] != "0" { 97 fmt.Printf("%s失败: %s\n", baidupcs.OperationShareFileSavetoLocal, resp["ErrMsg"]) 98 if resp["ErrNo"] == "4" { 99 trans_metas["shorturl"] = featurestr 100 pcs.SuperTransfer(trans_metas, resp["limit"]) // 试验性功能, 当前未启用 101 } 102 return 103 } 104 if opt.Collect { 105 resp["filename"] = trans_metas["filename"] 106 } 107 fmt.Printf("%s成功, 保存了%s到当前目录\n", baidupcs.OperationShareFileSavetoLocal, resp["filename"]) 108 if opt.Download { 109 fmt.Println("即将开始下载") 110 paths := strings.Split(resp["filenames"], ",") 111 paths = paths[0 : len(paths)-1] 112 RunDownload(paths, nil) 113 } 114 } 115 116 // RunRapidTransfer 执行秒传链接解析及保存 117 func RunRapidTransfer(link string, rnameOpt ...bool) { 118 if strings.Contains(link, "bdlink=") || strings.Contains(link, "bdpan://") { 119 r, _ := regexp.Compile(`(bdlink=|bdpan://)([^\s]+)`) 120 link1 := r.FindStringSubmatch(link)[2] 121 decodeBytes, err := base64.StdEncoding.DecodeString(link1) 122 if err != nil { 123 fmt.Printf("%s失败: %s\n", baidupcs.OperationRapidLinkSavetoLocal, "秒传链接格式错误") 124 return 125 } 126 link = string(decodeBytes) 127 } 128 rname := false 129 if len(rnameOpt) > 0 { 130 rname = rnameOpt[0] 131 } 132 link = strings.TrimSpace(link) 133 substrs := strings.SplitN(link, "#", 4) 134 if len(substrs) == 4 { 135 md5, slicemd5 := substrs[0], substrs[1] 136 size, _ := strconv.ParseInt(substrs[2], 10, 64) 137 filename := path.Join(GetActiveUser().Workdir, randReplaceStr(substrs[3], rname)) 138 RunRapidUpload(filename, md5, slicemd5, size) 139 } else if len(substrs) == 3 { 140 md5 := substrs[0] 141 size, _ := strconv.ParseInt(substrs[1], 10, 64) 142 filename := path.Join(GetActiveUser().Workdir, randReplaceStr(substrs[2], rname)) 143 RunRapidUpload(filename, md5, "", size) 144 } else { 145 fmt.Printf("%s失败: %s\n", baidupcs.OperationRapidLinkSavetoLocal, "秒传链接格式错误") 146 } 147 return 148 }