吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 4710|回复: 11
收起左侧

[Python 转载] 百度网盘批量保存

[复制链接]
Arcticlyc 发表于 2022-8-24 11:58
本帖最后由 Arcticlyc 于 2023-6-12 12:15 编辑

前言
前段时间Minecraft的买一赠一活动已经出来了,我也用自己基岩版的账号白嫖了java版Minecraft,这两天想起来很久以前看的一个实况解说,就去网上找了他分享的存档和资源包,但是下载下来发现是很多个网盘链接存在一个txt文件中,我保存几个之后就不想点了,一是太无聊,而是怕自己看花眼了。所以写了个批量保存百度网盘资源的脚本。


脚本简介
通过解析百度网盘保存链接,只需要要保存的网盘资源链接,用户的cookie和需要保存到的路径,文件名只是便于控制台查看结果,并无实际作用。
上传模式分为单个文件和多个文件(txt文件),批量保存可选择多文件保存。
屏幕截图 2022-08-23 234040.jpg


脚本资源
链接: https://pan.baidu.com/s/19fEGBA4quuT2R7CCuULY5w?pwd=84up 提取码: 84up 复制这段内容后打开百度网盘手机App,操作更方便哦


代码部分
[Python] 纯文本查看 复制代码
# -*- encoding: utf-8 -*-
'''
@file:   BaiduPan.py
@Time:   2022/08/24 11:54:57
@AuThor:   Arctic
@version:   1.0
@Contact:   [email]jc2150828@gmail.com[/email]
@desc:   
'''


import math
import random
import re
import requests
import base64
from urllib.parse import unquote

class BaiduPan:
    def __init__(self, cookie) -> None:
        self.headers = {
            'Host': 'pan.baidu.com',
            'Cookie': cookie,
            'User-Agent': 'Mozilla/5.0'
        }

    def get_info(self, url):
        res = requests.get(url, headers=self.headers)

        share_uk = re.findall(r'share_uk:"(\d+)"', res.text)[0]
        shareid = re.findall(r'shareid:"(\d+)"', res.text)[0]
        app_id = re.findall(r'app_id":"(\d+)"', res.text)[0]
        fs_id = re.findall(r'fs_id":(\d+),', res.text)[0]
        bdstoken = re.findall(r"bdstoken:'(\w+)'", res.text)[0]
        return (share_uk, shareid, app_id, fs_id, bdstoken)

    def getDpLogId(self, uk=None):
        def getRandomInt(num):
            return str(math.floor((random.random() * 9 + 1) * math.pow(10, num - 1)))
        def validateUk(uk):
            return len(uk + '') == 10
        def prefixInteger(num, length):
            return ''.join(['0' for _ in range(length - len(num))]) + num
        def getCountId(countid=30):
            if countid < 9999:
                countid += 1
            else:
                countid = 0
            return prefixInteger(str(countid), 4)

        client = ''

        userid = '00' + getRandomInt(8)
        sessionid = getRandomInt(6)

        if uk and validateUk(uk):
            userid = uk
        return client + sessionid + userid + getCountId()

    def upload(self, uri, path='/我的资源'):
        url = 'https://pan.baidu.com/share/transfer'

        headers = self.headers.copy()
        headers['Referer'] = uri

        share_uk, shareid, app_id, fs_id, bdstoken = self.get_info(uri)
        sekey = unquote(re.findall(r'BDCLND=(.+?);', headers['Cookie'])[0])
        logid = base64.b64encode(re.findall(r'BAIDUID=(.+?);', headers['Cookie'])[0].encode()).decode()
        dplogid = self.getDpLogId()
        params = {
            'shareid': shareid,
            'from': share_uk,
            'sekey': sekey,
            'channel': 'chunlei',
            'web': '1',
            'app_id': app_id,
            'bdstoken': bdstoken,
            'logid': logid,
            'clienttype': '0',
            'dp-logid': dplogid,
        }

        data = {
            'fsidlist': f'[{fs_id}]',
            'path': path
        }

        resp = requests.post(url, headers=headers, params=params, data=data).json()
        # pprint.pp(resp)
        if resp['errno'] == 0:
            return '保存网盘成功。'
        elif resp['errno'] == 4:
            return '已经保存过了。'
        elif resp['errno'] == 2:
            return '保存时出现未知错误。'
        else:
            return resp['show_msg']

# 单个文件保存
def sing_upload(name, url, cookie, path='/我的资源'):
    pan = BaiduPan(cookie)
    print(name + pan.upload(url, path))

# 多个文件保存
def txt_upload(file, cookie, path='/我的资源'):
    with open(file, 'r', encoding='utf8') as f:
        datas = [data.strip().split() for data in f.readlines() if data.strip()]

    pan = BaiduPan(cookie)

    for data in datas:
        name, url = data
        print(name + pan.upload(url, path))

if __name__ == '__main__':
    print('***百度网盘批量保存***')
    print('\n请将百度账号cookie复制到cookie.txt文件中')
    with open('cookie.txt', 'r', encoding='utf8') as f:
        cookie = f.read()

    mode = input('\n1. 单个文件保存\n2. 多个文件保存(txt文件)\n选择模式:')
    if mode == '1':
        name = input('输入文件名称:')
        url = input('输入网盘链接:')
        path = input('输入保存路径(不输入默认保存至 /我的资源):')
        if not path: 
            path = '/我的资源'
        sing_upload(name, url, cookie, path)
    elif mode == '2':
        file = input('输入txt文件路径:')
        path = input('输入保存路径(不输入默认保存至 /我的资源):')
        if not path: 
            path = '/我的资源'
        txt_upload(file, cookie, path)

免费评分

参与人数 3吾爱币 +4 热心值 +2 收起 理由
三滑稽甲苯 + 2 + 1 用心讨论,共获提升!
89684828 + 1 + 1 鼓励转贴优秀软件安全工具和文档!
小坏丶 + 1 热心回复!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

Lambor_G 发表于 2023-1-18 09:43
Arcticlyc 发表于 2023-1-17 19:06
名称 链接

如上格式,每行一个链接

File "G:\python\BaiduPan\BaiduPan.py", line 135, in <module>
    txt_upload(file, cookie, path)
会报错
File "G:\python\BaiduPan\BaiduPan.py", line 114, in txt_upload
    print(name + pan.upload(url, path))
  File "G:\python\BaiduPan\BaiduPan.py", line 65, in upload
    print(self.get_info(uri))
  File "G:\python\BaiduPan\BaiduPan.py", line 32, in get_info
    app_id = re.findall(r'app_id":"(\d+)"', res.text)[0]
IndexError: list index out of range
 楼主| Arcticlyc 发表于 2023-1-18 10:05
Lambor_G 发表于 2023-1-18 09:43
File "G:\python\BaiduPan\BaiduPan.py", line 135, in
    txt_upload(file, cookie, path)
会报错
...

可能规则更新了
89684828 发表于 2022-8-24 17:32
FM107.9 发表于 2022-8-29 18:28
怎么的呀 老哥
 楼主| Arcticlyc 发表于 2022-8-29 22:54

什么怎么的?
小飞龙 发表于 2022-10-1 22:31
怎么使用呢?
virsnow 发表于 2022-10-9 19:13
大佬牛啊??先收藏了再说
276148226 发表于 2022-11-29 14:03
没啥用  感谢分享
laibao888 发表于 2022-12-18 22:56
怎么用啊,不会用。
Lambor_G 发表于 2023-1-17 18:14
想问一下txt文档的格式是怎样的
 楼主| Arcticlyc 发表于 2023-1-17 19:06
Lambor_G 发表于 2023-1-17 18:14
想问一下txt文档的格式是怎样的

名称 链接

如上格式,每行一个链接

名称无实际意义,随意
链接为百度网盘的分享链接
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-29 00:13

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表