mirror of
https://gitee.com/g1879/DrissionPage.git
synced 2024-12-10 04:00:23 +08:00
优化下载逻辑,修复tab只设置文件名时会下载到根目录的问题
This commit is contained in:
parent
da171e49c9
commit
e2653ed3b0
@ -8,6 +8,7 @@
|
||||
from pathlib import Path
|
||||
from time import perf_counter, sleep
|
||||
|
||||
from .waiter import wait_mission
|
||||
from .._functions.settings import Settings
|
||||
from .._functions.web import offset_scroll
|
||||
from .._units.downloader import TabDownloadSettings
|
||||
@ -123,26 +124,34 @@ class Clicker(object):
|
||||
tmp_save_path = None
|
||||
if not self._ele.tab._browser._dl_mgr._running:
|
||||
self._ele.tab._browser.set.download_path('.')
|
||||
if save_path:
|
||||
if new_tab:
|
||||
tmp_save_path = str(Path(save_path).absolute())
|
||||
else:
|
||||
self._ele.tab.set.download_path(save_path)
|
||||
elif new_tab:
|
||||
tmp_save_path = self._ele.owner._tab.download_path
|
||||
|
||||
if new_tab:
|
||||
obj = browser = self._ele.tab._browser
|
||||
tid = 'browser'
|
||||
t_settings = TabDownloadSettings(self._ele.owner.tab_id)
|
||||
b_settings = TabDownloadSettings('browser')
|
||||
b_settings.rename = t_settings.rename
|
||||
b_settings.suffix = t_settings.suffix
|
||||
t_settings.rename = None
|
||||
t_settings.suffix = None
|
||||
tmp_save_path = str(Path(save_path).absolute()) if save_path else self._ele.owner._tab.download_path
|
||||
|
||||
else:
|
||||
obj = self._ele.owner._tab
|
||||
browser = obj.browser
|
||||
tid = obj.tab_id
|
||||
if save_path:
|
||||
tmp_save_path = str(Path(save_path).absolute())
|
||||
|
||||
obj = self._ele.tab._browser if new_tab else self._ele.owner._tab
|
||||
if rename or suffix:
|
||||
obj.set.download_file_name(rename, suffix)
|
||||
if timeout is None:
|
||||
timeout = obj.timeout
|
||||
|
||||
browser._dl_mgr.set_flag(tid, True)
|
||||
self.left(by_js=by_js)
|
||||
r = obj.wait.download_begin(timeout=timeout)
|
||||
r = wait_mission(browser, tid, timeout)
|
||||
|
||||
if tmp_save_path:
|
||||
r.path = tmp_save_path
|
||||
return r
|
||||
|
@ -193,7 +193,7 @@ class TabDownloadSettings(object):
|
||||
self.tab_id = tab_id
|
||||
self.rename = None
|
||||
self.suffix = None
|
||||
self.path = ''
|
||||
self.path = '' if tab_id == 'browser' else self.TABS['browser'].path
|
||||
self.when_file_exists = 'rename'
|
||||
|
||||
TabDownloadSettings.TABS[tab_id] = self
|
||||
|
@ -50,18 +50,7 @@ class BrowserWaiter(OriginWaiter):
|
||||
self._owner._dl_mgr.set_flag('browser', False if cancel_it else True)
|
||||
if timeout is None:
|
||||
timeout = self._owner.timeout
|
||||
|
||||
r = False
|
||||
end_time = perf_counter() + timeout
|
||||
while perf_counter() < end_time:
|
||||
v = self._owner._dl_mgr.get_flag('browser')
|
||||
if not isinstance(v, bool):
|
||||
r = v
|
||||
break
|
||||
sleep(.005)
|
||||
|
||||
self._owner._dl_mgr.set_flag('browser', None)
|
||||
return r
|
||||
return wait_mission(self._owner, 'browser', timeout)
|
||||
|
||||
def downloads_done(self, timeout=None, cancel_if_timeout=True):
|
||||
if not self._owner._dl_mgr._running:
|
||||
@ -183,18 +172,7 @@ class BaseWaiter(OriginWaiter):
|
||||
self._owner.browser._dl_mgr.set_flag(self._owner.tab_id, False if cancel_it else True)
|
||||
if timeout is None:
|
||||
timeout = self._owner.timeout
|
||||
|
||||
r = False
|
||||
end_time = perf_counter() + timeout
|
||||
while perf_counter() < end_time:
|
||||
v = self._owner.browser._dl_mgr.get_flag(self._owner.tab_id)
|
||||
if not isinstance(v, bool):
|
||||
r = v
|
||||
break
|
||||
sleep(.005)
|
||||
|
||||
self._owner.browser._dl_mgr.set_flag(self._owner.tab_id, None)
|
||||
return r
|
||||
return wait_mission(self._owner.browser, self._owner.tab_id, timeout)
|
||||
|
||||
def url_change(self, text, exclude=False, timeout=None, raise_err=None):
|
||||
return self._owner if self._change('url', text, exclude, timeout, raise_err) else False
|
||||
@ -417,3 +395,17 @@ class FrameWaiter(BaseWaiter, ElementWaiter):
|
||||
@property
|
||||
def _timeout(self):
|
||||
return self._owner.timeout
|
||||
|
||||
|
||||
def wait_mission(browser, tid, timeout=None):
|
||||
r = False
|
||||
end_time = perf_counter() + timeout
|
||||
while perf_counter() < end_time:
|
||||
v = browser._dl_mgr.get_flag(tid)
|
||||
if not isinstance(v, bool):
|
||||
r = v
|
||||
break
|
||||
sleep(.005)
|
||||
|
||||
browser._dl_mgr.set_flag(tid, None)
|
||||
return r
|
||||
|
@ -768,3 +768,13 @@ class FrameWaiter(BaseWaiter, ElementWaiter):
|
||||
:return: 成功返回元素对象,失败返回False
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
def wait_mission(browser: Chromium, tid: str, timeout: float = None) -> Union[DownloadMission, False]:
|
||||
"""等待下载任务
|
||||
:param browser: Chromium对象
|
||||
:param tid: 标签页id
|
||||
:param timeout: 超时时间
|
||||
:return:
|
||||
"""
|
||||
...
|
||||
|
Loading…
x
Reference in New Issue
Block a user