click.to_download()删除new_tab参数;优化下载逻辑

This commit is contained in:
g1879 2024-10-24 18:14:11 +08:00
parent ce9b17e25d
commit 9d3b7f3ec1
7 changed files with 29 additions and 30 deletions

View File

@ -64,6 +64,7 @@ class Chromium(object):
self._frames = {}
self._drivers = {}
self._all_drivers = {}
self._relation = {}
self._newest_tab_id = None
self._set = None
@ -388,7 +389,9 @@ class Chromium(object):
and not kwargs['targetInfo']['url'].startswith('devtools://')):
try:
tab_id = kwargs['targetInfo']['targetId']
self._frames[tab_id] = tab_id
d = Driver(tab_id, 'page', self.address)
self._relation[tab_id] = kwargs['targetInfo'].get('openerId', None)
self._drivers[tab_id] = d
self._all_drivers.setdefault(tab_id, set()).add(d)
self._newest_tab_id = tab_id
@ -404,6 +407,7 @@ class Chromium(object):
d.stop()
self._drivers.pop(tab_id, None)
self._all_drivers.pop(tab_id, None)
self._relation.pop(tab_id, None)
def _on_disconnect(self):
if not self._disconnect_flag:

View File

@ -40,6 +40,7 @@ class Chromium(object):
_frames: dict = ...
_drivers: Dict[str, Driver] = ...
_all_drivers: Dict[str, Set[Driver]] = ...
_relation: Dict[str, Optional[str]] = ...
_process_id: Optional[int] = ...
_dl_mgr: DownloadManager = ...
_timeouts: Timeout = ...

View File

@ -43,6 +43,7 @@ class ChromiumPage(ChromiumBase):
self._type = 'ChromiumPage'
self.set.timeouts(base=timeout) # 即将废弃
self._tab = self
self._browser._dl_mgr._page_id = self.tab_id
def __repr__(self):
return f'<ChromiumPage browser_id={self.browser.id} tab_id={self.tab_id}>'

View File

@ -10,7 +10,6 @@ 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
from ..errors import CanNotClickError, CDPError, NoRectError, AlertExistsError
@ -119,35 +118,20 @@ class Clicker(object):
def multi(self, times=2):
return self.at(count=times)
def to_download(self, save_path=None, rename=None, suffix=None, new_tab=False, by_js=False, timeout=None):
def to_download(self, save_path=None, rename=None, suffix=None, by_js=False, timeout=None, new_tab=None):
# 即将废弃new_tab参数
if not self._ele.tab._browser._dl_mgr._running:
self._ele.tab._browser.set.download_path('.')
when_file_exists = None
tmp_path = None
if self._ele.tab._type.endswith('Page'):
obj = browser = self._ele.owner._browser
tid = 'browser'
elif new_tab:
obj = browser = self._ele.owner._browser
tid = 'browser'
t_settings = TabDownloadSettings(self._ele.owner.tab_id)
b_settings = TabDownloadSettings('browser')
when_file_exists = b_settings.when_file_exists
b_settings.when_file_exists = t_settings.when_file_exists
b_settings.rename = t_settings.rename
b_settings.suffix = t_settings.suffix
t_settings.rename = None
t_settings.suffix = None
if not save_path:
tmp_path = b_settings.path
b_settings.path = t_settings.path
else:
obj = self._ele.owner._tab
browser = obj.browser
browser._dl_mgr._waiting_tab.add(self._ele.owner.tab_id)
tid = obj.tab_id
if save_path:
@ -164,11 +148,8 @@ class Clicker(object):
if tmp_path:
obj.set.download_path(tmp_path)
if when_file_exists:
browser.set.when_download_file_exists(when_file_exists)
if m and new_tab:
self._ele.owner.browser._dl_mgr._tab_missions.setdefault(self._ele.owner.tab_id, []).append(m)
m.from_tab = self._ele.owner.tab_id
browser._dl_mgr._waiting_tab.discard(self._ele.owner.tab_id)
return m
def to_upload(self, file_paths, by_js=False):

View File

@ -80,14 +80,12 @@ class Clicker(object):
save_path: Union[str, Path] = None,
rename: str = None,
suffix: str = None,
new_tab: bool = False,
by_js: bool = False,
timeout: float = None) -> DownloadMission:
"""点击触发下载
:param save_path: 保存路径为None保存在原来设置的如未设置保存到当前路径
:param rename: 重命名文件名
:param suffix: 指定文件后缀
:param new_tab: 该下载是否在新tab中触发
:param by_js: 是否用js方式点击逻辑与click()一致
:param timeout: 等待下载触发的超时时间为None则使用页面对象设置
:return: DownloadMission对象

View File

@ -27,7 +27,9 @@ class DownloadManager(object):
self._missions = {} # {guid: DownloadMission}
self._tab_missions = {} # {tab_id: [DownloadMission, ...]}
self._flags = {} # {tab_id: [bool, DownloadMission]}
self._waiting_tab = set() # click.to_download()专用
self._tmp_path = '.'
self._page_id = None
self._running = False
@ -97,11 +99,16 @@ class DownloadManager(object):
self._tab_missions.pop(tab_id, None)
self._flags.pop(tab_id, None)
TabDownloadSettings.TABS.pop(tab_id, None)
self._waiting_tab.discard(tab_id)
def _onDownloadWillBegin(self, **kwargs):
guid = kwargs['guid']
tab_id = self._browser._frames.get(kwargs['frameId'], 'browser')
tab = tab_id if tab_id in TabDownloadSettings.TABS else 'browser'
tab = 'browser' if tab_id in ('browser', self._page_id) else tab_id
opener = self._browser._relation.get(tab_id, None)
from_tab = None
if opener and opener in self._waiting_tab:
tab = from_tab = opener
settings = TabDownloadSettings(tab)
if settings.rename:
@ -139,16 +146,21 @@ class DownloadManager(object):
overwrite = False
m = DownloadMission(self, tab_id, guid, settings.path, name, kwargs['url'], self._tmp_path, overwrite)
if from_tab:
m.from_tab = from_tab
self._tab_missions.setdefault(from_tab, []).append(m)
self._missions[guid] = m
if self.get_flag(tab) is False: # 取消该任务
if self.get_flag('browser') is False or self.get_flag(tab) is False: # 取消该任务
self.cancel(m)
elif skip:
self.skip(m)
else:
self._tab_missions.setdefault(tab_id, []).append(m)
if self.get_flag(tab) is not None:
if self.get_flag('browser') is not None:
self._flags['browser'] = m
elif self.get_flag(tab) is not None:
self._flags[tab] = m
def _onDownloadProgress(self, **kwargs):

View File

@ -18,8 +18,10 @@ class DownloadManager(object):
_missions: Dict[str, DownloadMission] = ...
_tab_missions: dict = ...
_flags: dict = ...
_waiting_tab: set = ...
_running: bool = ...
_tmp_path: str = ...
_page_id: Optional[str] = ...
def __init__(self, browser: Chromium):
"""
@ -164,7 +166,7 @@ class DownloadMission(object):
name: str,
url: str,
tmp_path: str,
overwrite:bool):
overwrite: bool):
"""
:param mgr: BrowserDownloadManager对象
:param tab_id: 标签页id