Baidu shared links are transferred to Baidu network disk in batches

@百度Share link bulk transfer to Baidu network disk

Baidu shared links are transferred to Baidu network disk in batches

Recently, I got a batch of links shared by Baidu, and wanted to save them in batches to my own network disk, so I wrote a piece of code to realize it automatically, and share it with you here.
I released the code dumped by Baidu, and the steps to get the link can be realized by myself. There are many ways to do this, and it is not difficult.
Time: December 22, 2019
Language: Python3

#!/usr/local/Cellar/python/3.7.1/bin
# -*- coding: UTF-8 -*-
import requests, re, time
#获取当前时间戳
def get_timestamp():
    return int(round(time.time() * 1000)/1e3)

#百度分享文件转存
def bdsave(furl,verify,savepath,BDUSS,STOKEN,bdstoken):
    #Get 打开分享链接网址
    s = requests.Session()
    s.cookies['BDUSS'] = BDUSS
    s.cookies['STOKEN'] = STOKEN
    surl = furl.split('/')[-1][1:len(furl.split('/')[-1])]
    headers = {
    
    
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36',
        'Referer': 'https://pan.baidu.com/share/init?surl=' + surl
    }
    req = s.get(furl, headers=headers)
    req.encoding = 'utf-8'
    #判断分享链接是否正常
    if ('侵权、色情、反动、低俗' in req.text) or ('已过期' in req.text):
        print('此链接分享内容可能因为涉及侵权、色情、反动、低俗等信息,无法访问!||啊哦,来晚了,该分享文件已过期!')
        return ('此链接分享内容可能因为涉及侵权、色情、反动、低俗等信息,无法访问!||啊哦,来晚了,该分享文件已过期!')
    else:
        # Post user信息
        purl = 'https://pan.baidu.com/api/report/user?channel=chunlei&web=1&app_id=250528&bdstoken=' + bdstoken + \
               '&logid=MTU3NjY0ODUzNTI1MjAuNjkzMzEyMTcyODA2ODk2&clienttype=0'
        data = {
    
    'timestamp': get_timestamp(), 'action': 'web_home'}
        req = s.post(purl, data=data, headers=headers)

        # Post 链接和提取码信息,获取分享的内容
        purl = 'https://pan.baidu.com/share/verify?surl=' + surl + '&t=' + str(get_timestamp()) \
               + '&channel=chunlei&web=1&app_id=250528&bdstoken=' + bdstoken + '&logid=MTU3NjY1MDc0NDMyMzAuMjIxNjQwMTA0OTQ3NjA0Njc=&clienttype=0'
        data = {
    
    'pwd': verify, 'vcode': '', 'vcode_str': ''}
        req = s.post(purl, data=data, headers=headers)
        rinfo = re.findall('"errno":[-]{0,1}\d+', req.text)[0].replace('"errno":', '')
        if rinfo in '-12,-9':
            return ('提取码错误。')
        else:
            # Get 获取保存分享内容需要使用的相关参数信息,这些写信息存在与分享链接网页里
            req = s.get(furl, headers=headers)
            req.encoding = 'utf-8'
            shareid = re.findall('"shareid":\d+', req.text)[0].replace('"shareid":', '')
            uk = re.findall('uk=\d+', req.text)[0].replace('uk=', '')
            fsidlist = re.findall('"fs_id":\d+', req.text)[0].replace('"fs_id":', '')
            app_id = re.findall('"app_id":"\d+"', req.text)[0].replace('"app_id":', '').replace('"', '')
            # print('shareid:'+shareid +'\nuk:'+uk+'\nfsidlist:'+fsidlist+'\napp_id:'+app_id)

            # Post 保存分享的内容
            purl = 'https://pan.baidu.com/share/transfer?shareid=' + shareid + '&from=' + uk + '&channel=chunlei&web=1&app_id=' + app_id + '&bdstoken=' + bdstoken + '&logid=MTU3NjY1MTc1NTcwNTAuNjg1NDU2NDk2ODIxNjYx&clienttype=0'
            data = {
    
    
                'fsidlist': '[' + fsidlist + ']',
                'path': '/' + savepath
            }
            req = s.post(purl, data=data, headers=headers)
            req.encoding = 'utf-8'

            #检查最后返回的页面信息,查看是否成功保存
            try:
                zcinfo = re.findall('\[\{"errno":[-]{0,1}\d+', req.text)[0].replace('[{"errno":', '')
            except:
                return 'Cookies失效,请更新BDUSS、STOKEN、bdstoken后再试!'
            else:
                info = {
    
    
                    "0": "转存成功。",
                    "-1": "由于您分享了违反相关法律法规的文件,分享功能已被禁用,之前分享出去的文件不受影响。",
                    "-2": "用户不存在,请刷新页面后重试。",
                    "-3": "文件不存在,请刷新页面后重试。",
                    "-4": "登录信息有误,请重新登录试试。",
                    "-5": "host_key和user_key无效。",
                    "-6": "请重新登录。",
                    "-7": "该分享已删除或已取消。",
                    "-8": "该分享已经过期。",
                    "-9": "访问密码错误。",
                    "-10": "分享外链已经达到最大上限100000条,不能再次分享。",
                    "-11": "验证cookie无效。",
                    "-14": "对不起,短信分享每天限制20条,你今天已经分享完,请明天再来分享吧!",
                    "-15": "对不起,邮件分享每天限制20封,你今天已经分享完,请明天再来分享吧!",
                    "-16": "对不起,该文件已经限制分享!",
                    "-17": "文件分享超过限制。",
                    "-21": "预置文件无法进行相关操作。",
                    "-30": "文件已存在。",
                    "-31": "文件保存失败。",
                    "-33": "一次支持操作999个,减点试试吧。",
                    "-32": "未知结果。",
                    "-70": "你分享的文件中包含病毒或疑似病毒,为了你和他人的数据安全,换个文件分享吧。",
                    "2": "参数错误。",
                    "3": "未登录或帐号无效。",
                    "4": "存储好像出问题了,请稍候再试。",
                    "108": "文件名有敏感词,优化一下吧。",
                    "110": "分享次数超出限制,可以到“我的分享”中查看已分享的文件链接。",
                    "114": "当前任务不存在,保存失败。",
                    "115": "该文件禁止分享。",
                    "112": '页面已过期,请刷新后重试。',
                    "9100": '你的帐号存在违规行为,已被冻结。',
                    "9200": '你的帐号存在违规行为,已被冻结。',
                    "9300": '你的帐号存在违规行为,该功能暂被冻结。',
                    "9400": '你的帐号异常,需验证后才能使用该功能。',
                    "9500": '你的帐号存在安全风险,已进入保护模式,请修改密码后使用。'}
                return (info[zcinfo])

def main():
    # 百度网盘转存
    #   furl = 分享链接
    #   verify = 提取码
    #   savepath = 转存到百度网盘指定目录
    #   BDUSS = 从百度COOKICES中提取
    #   STOKEN = 从百度COOKICES中提取
    #   bdstoken = 从百度COOKICES中提取
    furl = ''
    verify = ''
    savepath = '/临时文件/pic图片'
    BDUSS = ''
    STOKEN = ''
    bdstoken = ''
    #调用百度分享转存函数,info是返回的信息,如果是'转存成功。'说明保存成功了
    info = bdsave(furl, verify, savepath, BDUSS, STOKEN, bdstoken)

if __name__ == '__main__':
    main()

Reference: https://www.52pojie.cn/thread-1005198-1-1.html

Guess you like

Origin blog.csdn.net/lkhk2008/article/details/103655533