From e81981bf99c1ff5e5bc93e619f5f152b1db8228a Mon Sep 17 00:00:00 2001 From: zoubenjia <53398369+zoubenjia@users.noreply.github.com> Date: Sun, 28 Aug 2022 19:37:04 +1000 Subject: [PATCH 1/9] Update pcs.py Add error handling for HTTPError exception to cope with http 403 error. --- baidupcs_py/baidupcs/pcs.py | 40 ++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/baidupcs_py/baidupcs/pcs.py b/baidupcs_py/baidupcs/pcs.py index 032374b..8c3da03 100644 --- a/baidupcs_py/baidupcs/pcs.py +++ b/baidupcs_py/baidupcs/pcs.py @@ -986,25 +986,29 @@ def download_link(self, remotepath: str, pcs: bool = False) -> Optional[str]: headers = dict(PCS_HEADERS) headers["Cookie"] = "; ".join([f"{k}={v}" for k, v in self.cookies.items()]) req = urllib.request.Request(url + "?" + params_str, headers=headers, method="GET") # type: ignore - resp = urllib.request.urlopen(req) # type: ignore - - # Error: "user is not authorized" - # This error occurs when the method is called by too many times - if resp.status != 200: - time.sleep(2) - continue - - info = json.loads(resp.read()) - - # This error is gotten when remote path is blocked - if info.get("host") == "issuecdn.baidupcs.com": - return None - - if not info.get("urls"): + try: + resp = urllib.request.urlopen(req) # type: ignore + + # Error: "user is not authorized" + # This error occurs when the method is called by too many times + if resp.status != 200: + time.sleep(2) + continue + + info = json.loads(resp.read()) + + # This error is gotten when remote path is blocked + if info.get("host") == "issuecdn.baidupcs.com": + return None + + if not info.get("urls"): + return None + else: + # return info["urls"][0]["url"].replace("&htype=", "") + return info["urls"][0]["url"] + exception urllib.error.HTTPError as e: + print(f"Error Code:{e.code}") return None - else: - # return info["urls"][0]["url"].replace("&htype=", "") - return info["urls"][0]["url"] def file_stream( self, From 3a2f84a93aa6b815fdfacdabd99f6bd14ecbb40a Mon Sep 17 00:00:00 2001 From: zoubenjia <53398369+zoubenjia@users.noreply.github.com> Date: Mon, 29 Aug 2022 11:41:24 +1000 Subject: [PATCH 2/9] Update pcs.py --- baidupcs_py/baidupcs/pcs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/baidupcs_py/baidupcs/pcs.py b/baidupcs_py/baidupcs/pcs.py index 8c3da03..055f3d8 100644 --- a/baidupcs_py/baidupcs/pcs.py +++ b/baidupcs_py/baidupcs/pcs.py @@ -1006,7 +1006,7 @@ def download_link(self, remotepath: str, pcs: bool = False) -> Optional[str]: else: # return info["urls"][0]["url"].replace("&htype=", "") return info["urls"][0]["url"] - exception urllib.error.HTTPError as e: + except urllib.error.HTTPError as e: print(f"Error Code:{e.code}") return None From 22262241f33446f723548786b9abd97443772886 Mon Sep 17 00:00:00 2001 From: zoubenjia <53398369+zoubenjia@users.noreply.github.com> Date: Mon, 29 Aug 2022 13:08:56 +1000 Subject: [PATCH 3/9] Update pcs.py --- baidupcs_py/baidupcs/pcs.py | 1 + 1 file changed, 1 insertion(+) diff --git a/baidupcs_py/baidupcs/pcs.py b/baidupcs_py/baidupcs/pcs.py index 055f3d8..d24bf9c 100644 --- a/baidupcs_py/baidupcs/pcs.py +++ b/baidupcs_py/baidupcs/pcs.py @@ -4,6 +4,7 @@ from pathlib import Path from urllib.parse import urlparse, quote_plus +from urllib.error import URLError, HTTPError from base64 import standard_b64encode import re import json From 7348d68a322371408e0dd3fbe92646fa83bf15e1 Mon Sep 17 00:00:00 2001 From: zoubenjia <53398369+zoubenjia@users.noreply.github.com> Date: Wed, 31 Aug 2022 01:32:29 +1000 Subject: [PATCH 4/9] Update download.py --- baidupcs_py/commands/download.py | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/baidupcs_py/commands/download.py b/baidupcs_py/commands/download.py index 493e382..67347c4 100644 --- a/baidupcs_py/commands/download.py +++ b/baidupcs_py/commands/download.py @@ -172,18 +172,23 @@ def except_callback(task_id: Optional[TaskID]): encrypt_password=encrypt_password, ) - if task_id is not None: - length = len(meDownloader) - _progress.update(task_id, total=length) - _progress.start_task(task_id) - - meDownloader.download( - Path(localpath), - task_id=task_id, - continue_=True, - done_callback=_wrap_done_callback, - except_callback=except_callback, - ) + try: + if task_id is not None: + length = len(meDownloader) + _progress.update(task_id, total=length) + _progress.start_task(task_id) + + meDownloader.download( + Path(localpath), + task_id=task_id, + continue_=True, + done_callback=_wrap_done_callback, + except_callback=except_callback, + ) + except IOError as e: + logger.error(f"IOError:{e}") + _progress.remove_task(task_id) + _progress.stop() def _aget_py_cmd( self, From 02a0e967d98fcde8b62bb9f8b4ad863cc91b3f05 Mon Sep 17 00:00:00 2001 From: zoubenjia <53398369+zoubenjia@users.noreply.github.com> Date: Wed, 31 Aug 2022 01:57:15 +1000 Subject: [PATCH 5/9] Update download.py --- baidupcs_py/commands/download.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/baidupcs_py/commands/download.py b/baidupcs_py/commands/download.py index 67347c4..8b669a6 100644 --- a/baidupcs_py/commands/download.py +++ b/baidupcs_py/commands/download.py @@ -184,12 +184,15 @@ def except_callback(task_id: Optional[TaskID]): continue_=True, done_callback=_wrap_done_callback, except_callback=except_callback, - ) + ) except IOError as e: + #catch 104 connection reset issue 59 logger.error(f"IOError:{e}") _progress.remove_task(task_id) _progress.stop() + + def _aget_py_cmd( self, url: str, From 598157a073ec9aaa83cfe0d53852bff20500c338 Mon Sep 17 00:00:00 2001 From: zoubenjia <53398369+zoubenjia@users.noreply.github.com> Date: Wed, 31 Aug 2022 02:26:05 +1000 Subject: [PATCH 6/9] supress error output --- baidupcs_py/commands/download.py | 1 - 1 file changed, 1 deletion(-) diff --git a/baidupcs_py/commands/download.py b/baidupcs_py/commands/download.py index 8b669a6..f108cdb 100644 --- a/baidupcs_py/commands/download.py +++ b/baidupcs_py/commands/download.py @@ -187,7 +187,6 @@ def except_callback(task_id: Optional[TaskID]): ) except IOError as e: #catch 104 connection reset issue 59 - logger.error(f"IOError:{e}") _progress.remove_task(task_id) _progress.stop() From bd042896ea7679fa951fb107df2b749c15c74bec Mon Sep 17 00:00:00 2001 From: zoubenjia Date: Wed, 31 Aug 2022 02:42:27 +1000 Subject: [PATCH 7/9] commit --- baidupcs_py/commands/download.py | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/baidupcs_py/commands/download.py b/baidupcs_py/commands/download.py index 493e382..5c3d261 100644 --- a/baidupcs_py/commands/download.py +++ b/baidupcs_py/commands/download.py @@ -172,18 +172,23 @@ def except_callback(task_id: Optional[TaskID]): encrypt_password=encrypt_password, ) - if task_id is not None: - length = len(meDownloader) - _progress.update(task_id, total=length) - _progress.start_task(task_id) - - meDownloader.download( - Path(localpath), - task_id=task_id, - continue_=True, - done_callback=_wrap_done_callback, - except_callback=except_callback, - ) + try: + if task_id is not None: + length = len(meDownloader) + _progress.update(task_id, total=length) + _progress.start_task(task_id) + + meDownloader.download( + Path(localpath), + task_id=task_id, + continue_=True, + done_callback=_wrap_done_callback, + except_callback=except_callback, + ) + except IOError as e: + #catch 104 connection reset issue 59 + _progress.remove_task(task_id) + _progress.stop() def _aget_py_cmd( self, From 4c387af0ee46ff4536098de84c0b476a873156a9 Mon Sep 17 00:00:00 2001 From: zoubenjia Date: Wed, 31 Aug 2022 03:04:16 +1000 Subject: [PATCH 8/9] commit --- baidupcs_py/commands/download.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/baidupcs_py/commands/download.py b/baidupcs_py/commands/download.py index 5c3d261..5b6782b 100644 --- a/baidupcs_py/commands/download.py +++ b/baidupcs_py/commands/download.py @@ -185,7 +185,7 @@ def except_callback(task_id: Optional[TaskID]): done_callback=_wrap_done_callback, except_callback=except_callback, ) - except IOError as e: + except IOError: #catch 104 connection reset issue 59 _progress.remove_task(task_id) _progress.stop() From 769d5a90efdabbcb6d31a550c8e6dae44297e6b6 Mon Sep 17 00:00:00 2001 From: zoubenjia Date: Wed, 31 Aug 2022 03:12:23 +1000 Subject: [PATCH 9/9] formatted by black --- baidupcs_py/commands/download.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/baidupcs_py/commands/download.py b/baidupcs_py/commands/download.py index 5b6782b..0605c34 100644 --- a/baidupcs_py/commands/download.py +++ b/baidupcs_py/commands/download.py @@ -179,14 +179,14 @@ def except_callback(task_id: Optional[TaskID]): _progress.start_task(task_id) meDownloader.download( - Path(localpath), - task_id=task_id, - continue_=True, - done_callback=_wrap_done_callback, - except_callback=except_callback, + Path(localpath), + task_id=task_id, + continue_=True, + done_callback=_wrap_done_callback, + except_callback=except_callback, ) except IOError: - #catch 104 connection reset issue 59 + # catch 104 connection reset issue 59 _progress.remove_task(task_id) _progress.stop()