mirror of
https://gitee.com/g1879/DrissionPage.git
synced 2024-12-10 04:00:23 +08:00
改进下载功能
This commit is contained in:
parent
a37ea0a50d
commit
36590206fb
@ -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)
|
||||
|
@ -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]: ...
|
||||
|
@ -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
|
||||
|
@ -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: ...
|
||||
|
Loading…
x
Reference in New Issue
Block a user