From 36590206fb568534198a66e2e21fdf94dc70d12d Mon Sep 17 00:00:00 2001 From: g1879 Date: Mon, 16 Oct 2023 18:03:48 +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?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/browser_download_manager.py | 16 +++++++++++++--- DrissionPage/browser_download_manager.pyi | 5 ++++- DrissionPage/setter.py | 12 ++++++------ DrissionPage/setter.pyi | 6 +++--- 4 files changed, 26 insertions(+), 13 deletions(-) diff --git a/DrissionPage/browser_download_manager.py b/DrissionPage/browser_download_manager.py index e7df58e..ce23f34 100644 --- a/DrissionPage/browser_download_manager.py +++ b/DrissionPage/browser_download_manager.py @@ -58,9 +58,10 @@ 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) + if tab_id == self._page.tab_id: + 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的重命名文件名 @@ -122,6 +123,8 @@ class BrowserDownloadManager(object): self._page.browser_driver.call_method('Browser.cancelDownload', guid=mission.id) if mission.final_path: Path(mission.final_path).unlink(True) + if mission.tab_id in self._tab_missions: + self._tab_missions[mission.tab_id].remove(mission.id) self._missions.pop(mission.id) def _onDownloadWillBegin(self, **kwargs): @@ -163,6 +166,8 @@ class BrowserDownloadManager(object): self.set_done(m, 'canceled', True) elif skip: self.set_done(m, 'skipped', True) + else: + self._tab_missions.setdefault(tab_id, []).append(guid) self._flags[tab_id] = m @@ -236,6 +241,11 @@ class DownloadMission(object): """以百分比形式返回下载进度""" return round((self.received_bytes / self.total_bytes) * 100, 2) if self.total_bytes else None + @property + def is_done(self): + """返回任务是否在运行中""" + return self.state == 'completed' + def cancel(self): """取消该任务,如任务已完成,删除已下载的文件""" self._mgr.set_done(self, state='canceled', cancel=True) diff --git a/DrissionPage/browser_download_manager.pyi b/DrissionPage/browser_download_manager.pyi index eb21561..bcc1c0e 100644 --- a/DrissionPage/browser_download_manager.pyi +++ b/DrissionPage/browser_download_manager.pyi @@ -56,7 +56,7 @@ class TabDownloadSettings(object): class DownloadMission(object): - tab: ChromiumBase = ... + tab_id: str = ... _mgr: BrowserDownloadManager = ... url: str = ... id: str = ... @@ -72,6 +72,9 @@ class DownloadMission(object): @property def rate(self) -> float: ... + @property + def is_done(self) -> bool: ... + def cancel(self) -> None: ... def wait(self, show: bool = True, timeout=None, cancel_if_timeout=True) -> Union[bool, str]: ... diff --git a/DrissionPage/setter.py b/DrissionPage/setter.py index ad17d0a..2298124 100644 --- a/DrissionPage/setter.py +++ b/DrissionPage/setter.py @@ -117,6 +117,11 @@ class ChromiumBaseSetter(object): self._page.run_cdp('Network.enable') self._page.run_cdp('Network.setExtraHTTPHeaders', headers=headers) + +class TabSetter(ChromiumBaseSetter): + def __init__(self, page): + super().__init__(page) + def download_path(self, path): """设置下载路径 :param path: 下载路径 @@ -127,11 +132,6 @@ class ChromiumBaseSetter(object): if self._page._DownloadKit: self._page._DownloadKit.set.goal_path(path) - -class TabSetter(ChromiumBaseSetter): - def __init__(self, page): - super().__init__(page) - def download_file_name(self, name): """设置下一个被下载文件的名称 :param name: 文件名,可不含后缀 @@ -145,7 +145,7 @@ class TabSetter(ChromiumBaseSetter): self._page.browser._dl_mgr.set_file_exists(self._page.tab_id, mode) -class ChromiumPageSetter(ChromiumBaseSetter): +class ChromiumPageSetter(TabSetter): def main_tab(self, tab_id=None): """设置主tab :param tab_id: 标签页id,不传入则设置当前tab diff --git a/DrissionPage/setter.pyi b/DrissionPage/setter.pyi index 16bc234..8e1aac4 100644 --- a/DrissionPage/setter.pyi +++ b/DrissionPage/setter.pyi @@ -50,18 +50,18 @@ class ChromiumBaseSetter(object): def upload_files(self, files: Union[str, list, tuple]) -> None: ... - def download_path(self, path: Union[str, Path]) -> None: ... - 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: ... -class ChromiumPageSetter(ChromiumBaseSetter): +class ChromiumPageSetter(TabSetter): _page: ChromiumPage = ... def main_tab(self, tab_id: str = None) -> None: ...