1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
| from tkinter import * from retrying import retry import urllib3,time,sys,re,requests
headers = { "Cookie": r'', "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36" }
request_header = { 'Host': 'pan.baidu.com', 'Connection': 'keep-alive', 'Upgrade-Insecure-Requests': '1', 'Sec-Fetch-Dest': 'document', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9', 'Sec-Fetch-Site': 'same-site', 'Sec-Fetch-Mode': 'navigate', 'Referer': 'https://pan.baidu.com', 'Accept-Encoding': 'gzip, deflate, br', 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-US;q=0.7,en-GB;q=0.6,ru;q=0.5', }
urllib3.disable_warnings() s = requests.session() s.trust_env = False
@retry(stop_max_attempt_number=5, wait_fixed=1000) def get_bdstoken(): url = 'https://pan.baidu.com/api/gettemplatevariable?clienttype=0&app_id=250528&web=1&fields=[%22bdstoken%22,%22token%22,%22uk%22,%22isdocuser%22,%22servertime%22]' response = s.get(url=url, headers=request_header, timeout=20, allow_redirects=True, verify=False) return response.json()['errno'] if response.json()['errno'] != 0 else response.json()['result']['bdstoken']
@retry(stop_max_attempt_number=5, wait_fixed=1000) def get_dir_list(bdstoken): url = 'https://pan.baidu.com/api/list?order=time&desc=1&showempty=0&web=1&page=1&num=1000&dir=%2Fupload&bdstoken=' + bdstoken response = s.get(url=url, headers=request_header, timeout=15, allow_redirects=False, verify=False) return response.json()['errno'] if response.json()['errno'] != 0 else response.json()['list']
@retry(stop_max_attempt_number=5, wait_fixed=1000) def create_dir(dir_name, bdstoken): url = 'https://pan.baidu.com/api/create?a=commit&bdstoken=' + bdstoken post_data = {'path': "/"+dir_name, 'isdir': '1', 'block_list': '[]', } response = s.post(url=url, headers=request_header, data=post_data, timeout=15, allow_redirects=False, verify=False) return response.json()['errno']
@retry(stop_max_attempt_number=6, wait_fixed=2000) def check_links(link_url, pass_code, bdstoken): if pass_code: t_str = str(int(round(time.time() * 1000))) check_url = 'https://pan.baidu.com/share/verify?surl=' + link_url[25:48] + '&bdstoken=' + bdstoken + '&t=' + t_str + '&channel=chunlei&web=1&clienttype=0' post_data = {'pwd': pass_code, 'vcode': '', 'vcode_str': '', } response_post = s.post(url=check_url, headers=request_header, data=post_data, timeout=10, allow_redirects=False, verify=False) if response_post.json()['errno'] == 0: bdclnd = response_post.json()['randsk'] else: return response_post.json()['errno'] if bool(re.search('BDCLND=', request_header['Cookie'], re.IGNORECASE)): request_header['Cookie'] = re.sub(r'BDCLND=(\S+);?', r'BDCLND=' + bdclnd + ';', request_header['Cookie']) else: request_header['Cookie'] += ';BDCLND=' + bdclnd response = s.get(url=link_url, headers=request_header, timeout=15, allow_redirects=True, verify=False).content.decode("utf-8") 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, info_title_list[0].replace("_免费高速下载|百度网盘-分享无限制","")]
@retry(stop_max_attempt_number=20, wait_fixed=2000) def transfer_files(check_links_reason, dir_name, bdstoken): url = 'https://pan.baidu.com/share/transfer?shareid=' + check_links_reason[0] + '&from=' + check_links_reason[1] + '&bdstoken=' + bdstoken + '&channel=chunlei&web=1&clienttype=0' fs_id = ','.join(i for i in check_links_reason[2]) post_data = {'fsidlist': '[' + fs_id + ']', 'path': '/' + dir_name, } response = s.post(url=url, headers=request_header, data=post_data, timeout=15, allow_redirects=False, verify=False) return response.json()['errno']
def savebp(dir_name,url): cookie = headers['Cookie'] user_agent = headers['User-Agent'] request_header['Cookie'] = cookie request_header['User-Agent'] = user_agent try: if any([ord(word) not in range(256) for word in cookie]) or cookie.find('BAIDUID=') == -1: sys.exit() bdstoken = get_bdstoken() if isinstance(bdstoken, int): sys.exit() dir_list_json = get_dir_list(bdstoken) if type(dir_list_json) != list: sys.exit() dir_list = [dir_json['server_filename'] for dir_json in dir_list_json] if dir_name and dir_name not in dir_list: create_dir_reason = create_dir(dir_name, bdstoken) if create_dir_reason != 0: sys.exit() url_code = url.replace("?pwd=", " ") link_url_org, pass_code_org = re.sub(r'提取码*[::](.*)', r'\1', url_code.lstrip()).split(' ', maxsplit=1) [link_url, pass_code] = [link_url_org.strip()[:47], pass_code_org.strip()[:4]] check_links_reason = check_links(link_url, pass_code, bdstoken) if isinstance(check_links_reason, list): transfer_files_reason = transfer_files(check_links_reason, dir_name, bdstoken) if transfer_files_reason == 0: filepath = "/"+dir_name+"/"+check_links_reason[3] print(f'转存成功:{filepath} {url_code}') return filepath except Exception as e: print(e) return None
url = "https://pan.baidu.com/s/*******************?pwd=****" savebp("test",url)
urls = [ "https://pan.baidu.com/s/*******************?pwd=****", "https://pan.baidu.com/s/*******************?pwd=****", "https://pan.baidu.com/s/*******************?pwd=****" ] for url in urls: savebp("test",url)
|