完善下载功能逻辑

This commit is contained in:
g1879 2023-09-08 00:36:04 +08:00
parent e5055decd8
commit 7394ab8059
3 changed files with 10 additions and 14 deletions

View File

@ -451,7 +451,7 @@ class BrowserDownloadManager(object):
page.set.download_path(page.download_path) page.set.download_path(page.download_path)
self._page.browser_driver.set_listener('Browser.downloadProgress', self._onDownloadProgress) self._page.browser_driver.set_listener('Browser.downloadProgress', self._onDownloadProgress)
self._page.browser_driver.set_listener('Browser.downloadWillBegin', self._onDownloadWillBegin) self._page.browser_driver.set_listener('Browser.downloadWillBegin', self._onDownloadWillBegin)
self._missions = set() self._missions = {}
@property @property
def missions(self): def missions(self):
@ -462,7 +462,7 @@ class BrowserDownloadManager(object):
:param mission: DownloadMission对象 :param mission: DownloadMission对象
:return: None :return: None
""" """
self._missions.add(mission) self._missions[mission.id] = mission
def cancel(self, mission): def cancel(self, mission):
"""取消一个下载任务 """取消一个下载任务
@ -470,7 +470,7 @@ class BrowserDownloadManager(object):
:return: None :return: None
""" """
self._page.browser_driver.call_method('Browser.cancelDownload', guid=mission.id) self._page.browser_driver.call_method('Browser.cancelDownload', guid=mission.id)
self._missions.remove(mission) self._missions.pop(mission.id)
def _onDownloadWillBegin(self, **kwargs): def _onDownloadWillBegin(self, **kwargs):
"""用于获取弹出新标签页触发的下载任务""" """用于获取弹出新标签页触发的下载任务"""
@ -482,7 +482,6 @@ class BrowserDownloadManager(object):
"""下载状态变化时执行""" """下载状态变化时执行"""
if kwargs['guid'] in self._missions: if kwargs['guid'] in self._missions:
mission = self._missions[kwargs['guid']] mission = self._missions[kwargs['guid']]
# print(mission)
if kwargs['state'] == 'inProgress': if kwargs['state'] == 'inProgress':
mission.state = 'running' mission.state = 'running'
mission.received_bytes = kwargs['receivedBytes'] mission.received_bytes = kwargs['receivedBytes']
@ -492,15 +491,12 @@ class BrowserDownloadManager(object):
mission.received_bytes = kwargs['receivedBytes'] mission.received_bytes = kwargs['receivedBytes']
mission.total_bytes = kwargs['totalBytes'] mission.total_bytes = kwargs['totalBytes']
form_path = f'{self._page.download_path}\\{mission.id}' form_path = f'{self._page.download_path}\\{mission.id}'
to_path = get_usable_path(f'{mission.path}\\{mission.name}') to_path = str(get_usable_path(f'{mission.path}\\{mission.name}'))
move(form_path, to_path) move(form_path, to_path)
mission.final_path = to_path mission._set_done('completed', final_path=to_path)
mission.state = 'completed'
self._missions.pop(mission.id)
else: else:
mission.state = 'canceled' mission._set_done('canceled')
self._missions.pop(mission.id)
class Alert(object): class Alert(object):

View File

@ -3,7 +3,7 @@
@Author : g1879 @Author : g1879
@Contact : g1879@qq.com @Contact : g1879@qq.com
""" """
from typing import Union, Tuple, List, Dict, Optional, Set from typing import Union, Tuple, List, Dict
from .chromium_base import ChromiumBase from .chromium_base import ChromiumBase
from .chromium_driver import ChromiumDriver from .chromium_driver import ChromiumDriver
@ -127,12 +127,12 @@ class ChromiumTabRect(object):
class BrowserDownloadManager(object): class BrowserDownloadManager(object):
_page: ChromiumPage = ... _page: ChromiumPage = ...
_missions: Set[DownloadMission] = ... _missions: Dict[str, DownloadMission] = ...
def __init__(self, page: ChromiumPage): ... def __init__(self, page: ChromiumPage): ...
@property @property
def missions(self) -> Set[DownloadMission]: ... def missions(self) -> Dict[str, DownloadMission]: ...
def add_mission(self, mission: DownloadMission) -> None: ... def add_mission(self, mission: DownloadMission) -> None: ...

View File

@ -247,7 +247,7 @@ class ChromiumPageWaiter(ChromiumBaseWaiter):
if self._driver._dl_mgr._missions: if self._driver._dl_mgr._missions:
if cancel_if_timeout: if cancel_if_timeout:
for m in self._driver._dl_mgr._missions: for m in self._driver._dl_mgr._missions.values():
m.cancel() m.cancel()
return False return False
else: else: