diff --git a/bcloud/SharePage.py b/bcloud/SharePage.py index 1300a79..8121525 100644 --- a/bcloud/SharePage.py +++ b/bcloud/SharePage.py @@ -226,7 +226,8 @@ def on_get_share_uk(info, error=None): else: return pwd_dialog.destroy() - gutil.async_call(pcs.verify_share_password, self.uk, + gutil.async_call(pcs.verify_share_password, + self.app.cookie, self.uk, self.shareid, pwd, callback=on_verify_password) else: diff --git a/bcloud/net.py b/bcloud/net.py index 2305a48..49d28ab 100644 --- a/bcloud/net.py +++ b/bcloud/net.py @@ -30,7 +30,7 @@ 'Cache-control': 'no-cache', } -def urloption(url, headers={}, retries=RETRIES): +def urloption(url, headers={}, retries=RETRIES, timeout=TIMEOUT): '''发送OPTION 请求''' headers_merged = default_headers.copy() for key in headers.keys(): @@ -38,13 +38,15 @@ def urloption(url, headers={}, retries=RETRIES): schema = urllib.parse.urlparse(url) for i in range(retries): try: - conn = http.client.HTTPConnection(schema.netloc) + conn = http.client.HTTPConnection(schema.netloc, timeout=timeout) conn.request('OPTIONS', url, headers=headers_merged) resp = conn.getresponse() return resp except OSError: logger.error(traceback.format_exc()) #return None + except KeyboardInterrupt: + raise except: logger.error(traceback.format_exc()) #return None @@ -66,10 +68,10 @@ def urlopen_simple(url, retries=RETRIES, timeout=TIMEOUT): return urllib.request.urlopen(url, timeout=timeout) except OSError: logger.error(traceback.format_exc()) - + except : logger.error(traceback.format_exc()) - + return None def urlopen(url, headers={}, data=None, retries=RETRIES, timeout=TIMEOUT): @@ -101,13 +103,15 @@ def urlopen(url, headers={}, data=None, retries=RETRIES, timeout=TIMEOUT): return req except OSError: logger.error(traceback.format_exc()) - + except KeyboardInterrupt: + raise except: logger.error(traceback.format_exc()) - + return None -def urlopen_without_redirect(url, headers={}, data=None, retries=RETRIES): +def urlopen_without_redirect(url, headers={}, data=None, retries=RETRIES, + timeout=TIMEOUT): '''请求一个URL, 并返回一个Response对象. 不处理重定向. 使用这个函数可以返回URL重定向(Error 301/302)后的地址, 也可以重到URL中请 @@ -120,7 +124,8 @@ def urlopen_without_redirect(url, headers={}, data=None, retries=RETRIES): parse_result = urllib.parse.urlparse(url) for i in range(retries): try: - conn = http.client.HTTPConnection(parse_result.netloc) + conn = http.client.HTTPConnection(parse_result.netloc, + timeout=TIMEOUT) if data: conn.request('POST', url, body=data, headers=headers_merged) else: @@ -128,12 +133,15 @@ def urlopen_without_redirect(url, headers={}, data=None, retries=RETRIES): return conn.getresponse() except OSError: logger.error(traceback.format_exc()) + except KeyboardInterrupt: + raise except: logger.error(traceback.format_exc()) #return None return None -def post_multipart(url, headers, fields, files, retries=RETRIES): +def post_multipart(url, headers, fields, files, retries=RETRIES, + timeout=TIMEOUT): content_type, body = encode_multipart_formdata(fields, files) schema = urllib.parse.urlparse(url) @@ -145,7 +153,7 @@ def post_multipart(url, headers, fields, files, retries=RETRIES): for i in range(retries): try: - h = http.client.HTTPConnection(schema.netloc) + h = http.client.HTTPConnection(schema.netloc, timeout=timeout) h.request('POST', url, body=body, headers=headers_merged) req = h.getresponse() encoding = req.getheader('Content-encoding') @@ -157,6 +165,8 @@ def post_multipart(url, headers, fields, files, retries=RETRIES): return req except OSError: logger.error(traceback.format_exc()) + except KeyboardInterrupt: + raise except: logger.error(traceback.format_exc()) #return None diff --git a/bcloud/pcs.py b/bcloud/pcs.py index bbe41f3..9502885 100644 --- a/bcloud/pcs.py +++ b/bcloud/pcs.py @@ -10,6 +10,7 @@ import json import os import re +import traceback from lxml import html from lxml.cssselect import CSSSelector as CSS @@ -168,6 +169,7 @@ def parse_share_page(content): else: json_str = script.text[start+33:end] try: + json_str = util.fix_json_escape(json_str) return json.loads(json.loads(json_str)) except ValueError: logger.warn(traceback.format_exc()) @@ -266,11 +268,11 @@ def enable_private_share(cookie, tokens, fid_list): else: return None, passwd -def verify_share_password(uk, shareid, pwd, vcode=''): +def verify_share_password(cookie, uk, shareid, pwd, vcode=''): '''验证共享文件的密码. 如果密码正确, 会在返回的请求头里加入一个cookie: BDCLND - + pwd - 四位的明文密码 vcode - 验证码; 目前还不支持 ''' @@ -282,7 +284,9 @@ def verify_share_password(uk, shareid, pwd, vcode=''): ]) data = 'pwd={0}&vcode={1}'.format(pwd, vcode) - req = net.urlopen(url, data=data.encode()) + req = net.urlopen(url, headers = { + 'Cookie': cookie.header_output(), + }, data=data.encode()) if req: content = req.data.decode() info = json.loads(content) @@ -557,7 +561,7 @@ def mkdir(cookie, tokens, path): @return 返回一个dict, 里面包含了fs_id, ctime等信息. ''' url = ''.join([ - const.PAN_API_URL, + const.PAN_API_URL, 'create?a=commit&channel=chunlei&clienttype=0&web=1', '&bdstoken=', tokens['bdstoken'], ]) @@ -841,7 +845,7 @@ def rapid_upload(cookie, tokens, source_path, path, upload_mode): def slice_upload(cookie, data): '''分片上传一个大文件 - + 分片上传完成后, 会返回这个分片的MD5, 用于最终的文件合并. 如果上传失败, 需要重新上传. 不需要指定上传路径, 上传后的数据会被存储在服务器的临时目录里. @@ -941,7 +945,7 @@ def search(cookie, tokens, key, path='/'): def cloud_add_link_task(cookie, tokens, source_url, save_path, vcode='', vcode_input=''): '''新建离线下载任务. - + source_url - 可以是http/https/ftp等一般的链接 可以是eMule这样的链接 path - 要保存到哪个目录, 比如 /Music/, 以/开头, 以/结尾的绝对路径. @@ -1047,7 +1051,7 @@ def cloud_query_sinfo(cookie, tokens, source_path): def cloud_query_magnetinfo(cookie, tokens, source_url, save_path): '''获取磁链的信息. - + 在新建磁链任务时, 要先获取这个磁链的信息, 比如里面包含哪些文件, 文件的名 称与大小等. @@ -1075,7 +1079,7 @@ def cloud_query_magnetinfo(cookie, tokens, source_url, save_path): def cloud_list_task(cookie, tokens, start=0): '''获取当前离线下载的任务信息 - + start - 从哪个任务开始, 从0开始计数, 会获取这50条任务信息 ''' url = ''.join([ @@ -1119,7 +1123,7 @@ def cloud_query_task(cookie, tokens, task_ids): def cloud_cancel_task(cookie, tokens, task_id): '''取消离线下载任务. - + task_id - 之前建立离线下载任务时的task id, 也可以从cloud_list_task()里 获取. ''' diff --git a/bcloud/util.py b/bcloud/util.py index b289759..3a66bd6 100644 --- a/bcloud/util.py +++ b/bcloud/util.py @@ -208,3 +208,9 @@ def validate_pathname(filepath): if path[0] in filter3 or path[-1] in filter3: return ValidatePathState.CHAR_ERROR3 return ValidatePathState.OK + +def fix_json_escape(json_str): + # Remove invalid escape + json_str = re.sub(r'\\(?![\\"/bfnrt])', "", json_str) + return json_str +