From a37ea0a50dee872fec3cc6e43ba95fddcaad5eb2 Mon Sep 17 00:00:00 2001 From: g1879 Date: Mon, 16 Oct 2023 00:14:17 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9B=E4=B8=8B=E8=BD=BD=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=EF=BC=8C=E5=BE=85=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/base.py | 5 +++-- DrissionPage/browser_download_manager.py | 19 +++++++++++++++---- DrissionPage/chromium_base.py | 2 +- DrissionPage/chromium_page.py | 3 ++- DrissionPage/setter.py | 18 +----------------- DrissionPage/setter.pyi | 4 ---- 6 files changed, 22 insertions(+), 29 deletions(-) diff --git a/DrissionPage/base.py b/DrissionPage/base.py index c7b89d8..48880a6 100644 --- a/DrissionPage/base.py +++ b/DrissionPage/base.py @@ -4,6 +4,7 @@ @Contact : g1879@qq.com """ from abc import abstractmethod +from pathlib import Path from re import sub from urllib.parse import quote @@ -366,7 +367,7 @@ class BasePage(BaseParser): self.retry_times = 3 self.retry_interval = 2 self._DownloadKit = None - self._download_path = '' + self._download_path = str(Path('.').absolute()) @property def title(self): @@ -403,7 +404,7 @@ class BasePage(BaseParser): def download(self): """返回下载器对象""" if self._DownloadKit is None: - self._DownloadKit = DownloadKit(session=self, goal_path=self.download_path) + self._DownloadKit = DownloadKit(driver=self, goal_path=self.download_path) return self._DownloadKit def _before_connect(self, url, retry, interval): diff --git a/DrissionPage/browser_download_manager.py b/DrissionPage/browser_download_manager.py index 4614ac2..e7df58e 100644 --- a/DrissionPage/browser_download_manager.py +++ b/DrissionPage/browser_download_manager.py @@ -22,8 +22,9 @@ class BrowserDownloadManager(object): """ :param page: ChromiumPage对象 """ - if page.browser_driver.id in BrowserDownloadManager.BROWSERS: + if hasattr(self, '_created'): return + self._created = True self._page = page self._lock = Lock() @@ -39,6 +40,9 @@ class BrowserDownloadManager(object): self._page.browser_driver.set_listener('Browser.downloadProgress', self._onDownloadProgress) self._page.browser_driver.set_listener('Browser.downloadWillBegin', self._onDownloadWillBegin) + self._page.browser_driver.call_method('Browser.setDownloadBehavior', + downloadPath=self._page.download_path, + behavior='allowAndName', eventsEnabled=True) BrowserDownloadManager.BROWSERS[page.browser_driver.id] = self @@ -54,6 +58,9 @@ class BrowserDownloadManager(object): :return: None """ self._tabs_settings.setdefault(tab_id, TabDownloadSettings(tab_id)).path = str(Path(path).absolute()) + self._page.browser_driver.call_method('Browser.setDownloadBehavior', + downloadPath=str(Path(path).absolute()), + behavior='allowAndName', eventsEnabled=True) def set_rename(self, tab_id, rename): """设置某个tab的重命名文件名 @@ -120,7 +127,7 @@ class BrowserDownloadManager(object): def _onDownloadWillBegin(self, **kwargs): """用于获取弹出新标签页触发的下载任务""" guid = kwargs['guid'] - end = perf_counter() + .3 + end = perf_counter() + .5 while perf_counter() < end: tab_id = self._guid_and_tab.get(guid, None) if tab_id: @@ -178,7 +185,7 @@ class BrowserDownloadManager(object): move(form_path, to_path) self.set_done(mission, 'completed', final_path=to_path) - else: + else: # canceled self.set_done(mission, 'canceled') @@ -197,11 +204,16 @@ class TabDownloadSettings(object): """ :param tab_id: tab id """ + if hasattr(self, '_created'): + return + self._created = True self.tab_id = tab_id self.rename = None self.path = '' self.when_file_exists = 'rename' + TabDownloadSettings.TABS[tab_id] = self + class DownloadMission(object): def __init__(self, mgr, tab_id, _id, path, name, url): @@ -217,7 +229,6 @@ class DownloadMission(object): self.final_path = None def __repr__(self): - # return f'' return f'' @property diff --git a/DrissionPage/chromium_base.py b/DrissionPage/chromium_base.py index 1fb3cc2..7179964 100644 --- a/DrissionPage/chromium_base.py +++ b/DrissionPage/chromium_base.py @@ -48,7 +48,7 @@ class ChromiumBase(BasePage): self._actions = None self._listener = None - self._download_path = '' + self._download_path = str(Path('.').absolute()) if isinstance(address, int) or (isinstance(address, str) and address.isdigit()): address = f'127.0.0.1:{address}' diff --git a/DrissionPage/chromium_page.py b/DrissionPage/chromium_page.py index ea6a459..8e39bca 100644 --- a/DrissionPage/chromium_page.py +++ b/DrissionPage/chromium_page.py @@ -3,6 +3,7 @@ @Author : g1879 @Contact : g1879@qq.com """ +from pathlib import Path from time import perf_counter, sleep from .browser_download_manager import BrowserDownloadManager @@ -65,7 +66,7 @@ class ChromiumPage(ChromiumBase): if self._driver_options.timeouts['implicit'] is not None: self._timeout = self._driver_options.timeouts['implicit'] self._page_load_strategy = self._driver_options.page_load_strategy - self._download_path = self._driver_options.download_path + self._download_path = str(Path(self._driver_options.download_path).absolute()) def _connect_browser(self, tab_id=None): """连接浏览器,在第一次时运行 diff --git a/DrissionPage/setter.py b/DrissionPage/setter.py index c7e0b31..ad17d0a 100644 --- a/DrissionPage/setter.py +++ b/DrissionPage/setter.py @@ -123,6 +123,7 @@ class ChromiumBaseSetter(object): :return: None """ self._page._download_path = str(Path(path).absolute()) + self._page.browser._dl_mgr.set_path(self._page.tab_id, path) if self._page._DownloadKit: self._page._DownloadKit.set.goal_path(path) @@ -131,14 +132,6 @@ class TabSetter(ChromiumBaseSetter): def __init__(self, page): super().__init__(page) - def download_path(self, path): - """设置下载路径 - :param path: 下载路径 - :return: None - """ - super().download_path(path) - self._page.browser._dl_mgr.set_path(self._page.tab_id, path) - def download_file_name(self, name): """设置下一个被下载文件的名称 :param name: 文件名,可不含后缀 @@ -176,15 +169,6 @@ class ChromiumPageSetter(ChromiumBaseSetter): tab_or_id = tab_or_id.tab_id self._page._control_session.get(f'http://{self._page.address}/json/activate/{tab_or_id}') - def download_path(self, path): - """设置下载路径 - :param path: 下载路径 - :return: None - """ - super().download_path(path) - self._page.browser_driver.call_method('Browser.setDownloadBehavior', downloadPath=self._page.download_path, - behavior='allowAndName', eventsEnabled=True) - class SessionPageSetter(object): def __init__(self, page): diff --git a/DrissionPage/setter.pyi b/DrissionPage/setter.pyi index 26130cb..16bc234 100644 --- a/DrissionPage/setter.pyi +++ b/DrissionPage/setter.pyi @@ -56,8 +56,6 @@ class ChromiumBaseSetter(object): class TabSetter(ChromiumBaseSetter): def __init__(self, page): ... - def download_path(self, path: Union[str, Path]) -> None: ... - def download_file_name(self, name: str) -> None: ... def when_download_file_exists(self, mode: str) -> None: ... @@ -73,8 +71,6 @@ class ChromiumPageSetter(ChromiumBaseSetter): def tab_to_front(self, tab_or_id: Union[str, ChromiumTab] = None) -> None: ... - def download_path(self, path: Union[str, Path]) -> None: ... - class SessionPageSetter(object): def __init__(self, page: SessionPage):