|
|
|
@ -1,7 +1,11 @@ |
|
|
|
|
import requests |
|
|
|
|
import urllib3 |
|
|
|
|
import random |
|
|
|
|
import re |
|
|
|
|
import time |
|
|
|
|
from retrying import retry |
|
|
|
|
|
|
|
|
|
requests.packages.urllib3.disable_warnings() |
|
|
|
|
urllib3.disable_warnings() |
|
|
|
|
|
|
|
|
|
# 静态变量 |
|
|
|
|
BASE_URL = 'https://pan.baidu.com' |
|
|
|
@ -54,7 +58,8 @@ class ReqAction: |
|
|
|
|
with open('cookie.txt', 'r', encoding='utf-8') as f: |
|
|
|
|
self.request_header['Cookie'] = f.readline() |
|
|
|
|
|
|
|
|
|
@retry(stop_max_attempt_number=3, wait_fixed=1000) |
|
|
|
|
# 获取bdstoken函数 |
|
|
|
|
@retry(stop_max_attempt_number=3, wait_fixed=random.randint(1000, 3000)) |
|
|
|
|
def get_bdstoken(self): |
|
|
|
|
url = f'{BASE_URL}/api/gettemplatevariable?clienttype=0&app_id=250528&web=1&fields=[%22bdstoken%22,%22token%22,%22uk%22,%22isdocuser%22,%22servertime%22]' |
|
|
|
|
response = self.session.get(url=url, headers=self.request_header, timeout=20, allow_redirects=True, |
|
|
|
@ -62,9 +67,96 @@ class ReqAction: |
|
|
|
|
print(response.text) |
|
|
|
|
return response.json()['errno'] if response.json()['errno'] != 0 else response.json()['result']['bdstoken'] |
|
|
|
|
|
|
|
|
|
# 获取目录列表函数 |
|
|
|
|
@retry(stop_max_attempt_number=5, wait_fixed=random.randint(1000, 3000)) |
|
|
|
|
def get_dir_list(self, dir_path): |
|
|
|
|
url = f'{BASE_URL}/api/list?order=time&desc=1&showempty=0&web=1&page=1&num=1000&dir={dir_path}&bdstoken={self.bdstoken}' |
|
|
|
|
response = self.session.get(url=url, headers=self.request_header, timeout=15, allow_redirects=False, |
|
|
|
|
verify=False) |
|
|
|
|
print(response.text) |
|
|
|
|
return response.json()['errno'] if response.json()['errno'] != 0 else response.json()['list'] |
|
|
|
|
|
|
|
|
|
# 新建目录函数 |
|
|
|
|
@retry(stop_max_attempt_number=5, wait_fixed=random.randint(1000, 3000)) |
|
|
|
|
def create_dir(self, target_directory_name): |
|
|
|
|
url = f'{BASE_URL}/api/create?a=commit&bdstoken={self.bdstoken}' |
|
|
|
|
post_data = {'path': target_directory_name, 'isdir': '1', 'block_list': '[]', } |
|
|
|
|
response = self.session.post(url=url, headers=self.request_header, data=post_data, timeout=15, |
|
|
|
|
allow_redirects=False, verify=False) |
|
|
|
|
print(response.text) |
|
|
|
|
return response.json()['errno'] |
|
|
|
|
|
|
|
|
|
# 更新 cookie 函数 |
|
|
|
|
def update_cookie(self, bdclnd): |
|
|
|
|
if 'BDCLND=' in self.request_header['Cookie']: |
|
|
|
|
self.request_header['Cookie'] = re.sub(r'BDCLND=(\S+);?', f'BDCLND={bdclnd};', |
|
|
|
|
self.request_header['Cookie']) |
|
|
|
|
else: |
|
|
|
|
self.request_header['Cookie'] += f';BDCLND={bdclnd}' |
|
|
|
|
|
|
|
|
|
# 验证提取码函数 |
|
|
|
|
@retry(stop_max_attempt_number=6, wait_fixed=1700) |
|
|
|
|
def verify_pass_code(self, link_url, pass_code): |
|
|
|
|
check_url = f'{BASE_URL}/share/verify?surl={link_url[25:48]}&bdstoken={self.bdstoken}&t={str(int(round(time.time() * 1000)))}&channel=chunlei&web=1&clienttype=0' |
|
|
|
|
post_data = {'pwd': pass_code, 'vcode': '', 'vcode_str': '', } |
|
|
|
|
response = self.session.post(url=check_url, headers=self.request_header, data=post_data, timeout=10, |
|
|
|
|
allow_redirects=False, verify=False) |
|
|
|
|
print(response.text) |
|
|
|
|
return response.json()['errno'] if response.json()['errno'] != 0 else response.json()['randsk'] |
|
|
|
|
|
|
|
|
|
# 验证链接函数 |
|
|
|
|
@retry(stop_max_attempt_number=12, wait_fixed=1700) |
|
|
|
|
def verify_links(self, link_url, pass_code): |
|
|
|
|
if pass_code: |
|
|
|
|
bdclnd = self.verify_pass_code(link_url, pass_code) |
|
|
|
|
if isinstance(bdclnd, int): |
|
|
|
|
return bdclnd |
|
|
|
|
self.update_cookie(bdclnd) |
|
|
|
|
|
|
|
|
|
response = self.session.get(url=link_url, headers=self.request_header, timeout=15, allow_redirects=True, |
|
|
|
|
verify=False).content.decode("utf-8") |
|
|
|
|
print(response) |
|
|
|
|
shareid_list = re.findall('"shareid":(\\d+?),"', response) |
|
|
|
|
user_id_list = re.findall('"share_uk":"(\\d+?)","', response) |
|
|
|
|
fs_id_list = re.findall('"fs_id":(\\d+?),"', response) |
|
|
|
|
info_title_list = re.findall('<title>(.+)</title>', response) |
|
|
|
|
|
|
|
|
|
if not shareid_list: |
|
|
|
|
return 1 |
|
|
|
|
elif not user_id_list: |
|
|
|
|
return 2 |
|
|
|
|
elif not fs_id_list: |
|
|
|
|
return info_title_list[0] if info_title_list else 3 |
|
|
|
|
else: |
|
|
|
|
return [shareid_list[0], user_id_list[0], fs_id_list] |
|
|
|
|
|
|
|
|
|
# 转存文件函数 |
|
|
|
|
@retry(stop_max_attempt_number=9, wait_fixed=random.randint(1000, 3000)) |
|
|
|
|
def transfer_files(self, verify_links_reason, target_directory_name): |
|
|
|
|
url = f'{BASE_URL}/share/transfer?shareid={verify_links_reason[0]}&from={verify_links_reason[1]}&bdstoken={self.bdstoken}&channel=chunlei&web=1&clienttype=0' |
|
|
|
|
post_data = {'fsidlist': f'[{",".join(i for i in verify_links_reason[2])}]', |
|
|
|
|
'path': f'/{target_directory_name}', } |
|
|
|
|
response = self.session.post(url=url, headers=self.request_header, data=post_data, timeout=15, |
|
|
|
|
allow_redirects=False, verify=False) |
|
|
|
|
print(response.text) |
|
|
|
|
return response.json()['errno'] |
|
|
|
|
|
|
|
|
|
def main(self): |
|
|
|
|
target_dir = '/Temp/leshe/1' |
|
|
|
|
dir_name = '2' |
|
|
|
|
link_list = [] |
|
|
|
|
self.get_cookies() |
|
|
|
|
self.bdstoken = self.get_bdstoken() |
|
|
|
|
dir_list = self.get_dir_list('/Temp/leshe') |
|
|
|
|
print([_dir['path'] for _dir in dir_list]) |
|
|
|
|
if target_dir in [_dir['path'] for _dir in dir_list]: |
|
|
|
|
print('找到了') |
|
|
|
|
# if dir_name and dir_name not in [_dir('path') for _dir in self.dir_list]: |
|
|
|
|
# self.create_dir(r'/Temp/leshe/1') |
|
|
|
|
# 执行转存 |
|
|
|
|
for link in link_list: |
|
|
|
|
verified_links = self.verify_links(link[0], link[1]) |
|
|
|
|
self.transfer_files(verified_links, target_dir) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
|
|
|