[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)