完善下载功能逻辑

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

View File

@ -3,7 +3,7 @@
@Author : g1879
@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_driver import ChromiumDriver
@ -127,12 +127,12 @@ class ChromiumTabRect(object):
class BrowserDownloadManager(object):
_page: ChromiumPage = ...
_missions: Set[DownloadMission] = ...
_missions: Dict[str, DownloadMission] = ...
def __init__(self, page: ChromiumPage): ...
@property
def missions(self) -> Set[DownloadMission]: ...
def missions(self) -> Dict[str, DownloadMission]: ...
def add_mission(self, mission: DownloadMission) -> None: ...

View File

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