mirror of
https://gitee.com/g1879/DrissionPage.git
synced 2024-12-10 04:00:23 +08:00
click.to_download()删除new_tab参数;优化下载逻辑
This commit is contained in:
parent
ce9b17e25d
commit
9d3b7f3ec1
@ -64,6 +64,7 @@ class Chromium(object):
|
|||||||
self._frames = {}
|
self._frames = {}
|
||||||
self._drivers = {}
|
self._drivers = {}
|
||||||
self._all_drivers = {}
|
self._all_drivers = {}
|
||||||
|
self._relation = {}
|
||||||
self._newest_tab_id = None
|
self._newest_tab_id = None
|
||||||
|
|
||||||
self._set = None
|
self._set = None
|
||||||
@ -388,7 +389,9 @@ class Chromium(object):
|
|||||||
and not kwargs['targetInfo']['url'].startswith('devtools://')):
|
and not kwargs['targetInfo']['url'].startswith('devtools://')):
|
||||||
try:
|
try:
|
||||||
tab_id = kwargs['targetInfo']['targetId']
|
tab_id = kwargs['targetInfo']['targetId']
|
||||||
|
self._frames[tab_id] = tab_id
|
||||||
d = Driver(tab_id, 'page', self.address)
|
d = Driver(tab_id, 'page', self.address)
|
||||||
|
self._relation[tab_id] = kwargs['targetInfo'].get('openerId', None)
|
||||||
self._drivers[tab_id] = d
|
self._drivers[tab_id] = d
|
||||||
self._all_drivers.setdefault(tab_id, set()).add(d)
|
self._all_drivers.setdefault(tab_id, set()).add(d)
|
||||||
self._newest_tab_id = tab_id
|
self._newest_tab_id = tab_id
|
||||||
@ -404,6 +407,7 @@ class Chromium(object):
|
|||||||
d.stop()
|
d.stop()
|
||||||
self._drivers.pop(tab_id, None)
|
self._drivers.pop(tab_id, None)
|
||||||
self._all_drivers.pop(tab_id, None)
|
self._all_drivers.pop(tab_id, None)
|
||||||
|
self._relation.pop(tab_id, None)
|
||||||
|
|
||||||
def _on_disconnect(self):
|
def _on_disconnect(self):
|
||||||
if not self._disconnect_flag:
|
if not self._disconnect_flag:
|
||||||
|
@ -40,6 +40,7 @@ class Chromium(object):
|
|||||||
_frames: dict = ...
|
_frames: dict = ...
|
||||||
_drivers: Dict[str, Driver] = ...
|
_drivers: Dict[str, Driver] = ...
|
||||||
_all_drivers: Dict[str, Set[Driver]] = ...
|
_all_drivers: Dict[str, Set[Driver]] = ...
|
||||||
|
_relation: Dict[str, Optional[str]] = ...
|
||||||
_process_id: Optional[int] = ...
|
_process_id: Optional[int] = ...
|
||||||
_dl_mgr: DownloadManager = ...
|
_dl_mgr: DownloadManager = ...
|
||||||
_timeouts: Timeout = ...
|
_timeouts: Timeout = ...
|
||||||
|
@ -43,6 +43,7 @@ class ChromiumPage(ChromiumBase):
|
|||||||
self._type = 'ChromiumPage'
|
self._type = 'ChromiumPage'
|
||||||
self.set.timeouts(base=timeout) # 即将废弃
|
self.set.timeouts(base=timeout) # 即将废弃
|
||||||
self._tab = self
|
self._tab = self
|
||||||
|
self._browser._dl_mgr._page_id = self.tab_id
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return f'<ChromiumPage browser_id={self.browser.id} tab_id={self.tab_id}>'
|
return f'<ChromiumPage browser_id={self.browser.id} tab_id={self.tab_id}>'
|
||||||
|
@ -10,7 +10,6 @@ from time import perf_counter, sleep
|
|||||||
from .waiter import wait_mission
|
from .waiter import wait_mission
|
||||||
from .._functions.settings import Settings
|
from .._functions.settings import Settings
|
||||||
from .._functions.web import offset_scroll
|
from .._functions.web import offset_scroll
|
||||||
from .._units.downloader import TabDownloadSettings
|
|
||||||
from ..errors import CanNotClickError, CDPError, NoRectError, AlertExistsError
|
from ..errors import CanNotClickError, CDPError, NoRectError, AlertExistsError
|
||||||
|
|
||||||
|
|
||||||
@ -119,35 +118,20 @@ class Clicker(object):
|
|||||||
def multi(self, times=2):
|
def multi(self, times=2):
|
||||||
return self.at(count=times)
|
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:
|
if not self._ele.tab._browser._dl_mgr._running:
|
||||||
self._ele.tab._browser.set.download_path('.')
|
self._ele.tab._browser.set.download_path('.')
|
||||||
|
|
||||||
when_file_exists = None
|
|
||||||
tmp_path = None
|
tmp_path = None
|
||||||
if self._ele.tab._type.endswith('Page'):
|
if self._ele.tab._type.endswith('Page'):
|
||||||
obj = browser = self._ele.owner._browser
|
obj = browser = self._ele.owner._browser
|
||||||
tid = '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:
|
else:
|
||||||
obj = self._ele.owner._tab
|
obj = self._ele.owner._tab
|
||||||
browser = obj.browser
|
browser = obj.browser
|
||||||
|
browser._dl_mgr._waiting_tab.add(self._ele.owner.tab_id)
|
||||||
tid = obj.tab_id
|
tid = obj.tab_id
|
||||||
|
|
||||||
if save_path:
|
if save_path:
|
||||||
@ -164,11 +148,8 @@ class Clicker(object):
|
|||||||
|
|
||||||
if tmp_path:
|
if tmp_path:
|
||||||
obj.set.download_path(tmp_path)
|
obj.set.download_path(tmp_path)
|
||||||
if when_file_exists:
|
browser._dl_mgr._waiting_tab.discard(self._ele.owner.tab_id)
|
||||||
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
|
|
||||||
return m
|
return m
|
||||||
|
|
||||||
def to_upload(self, file_paths, by_js=False):
|
def to_upload(self, file_paths, by_js=False):
|
||||||
|
@ -80,14 +80,12 @@ class Clicker(object):
|
|||||||
save_path: Union[str, Path] = None,
|
save_path: Union[str, Path] = None,
|
||||||
rename: str = None,
|
rename: str = None,
|
||||||
suffix: str = None,
|
suffix: str = None,
|
||||||
new_tab: bool = False,
|
|
||||||
by_js: bool = False,
|
by_js: bool = False,
|
||||||
timeout: float = None) -> DownloadMission:
|
timeout: float = None) -> DownloadMission:
|
||||||
"""点击触发下载
|
"""点击触发下载
|
||||||
:param save_path: 保存路径,为None保存在原来设置的,如未设置保存到当前路径
|
:param save_path: 保存路径,为None保存在原来设置的,如未设置保存到当前路径
|
||||||
:param rename: 重命名文件名
|
:param rename: 重命名文件名
|
||||||
:param suffix: 指定文件后缀
|
:param suffix: 指定文件后缀
|
||||||
:param new_tab: 该下载是否在新tab中触发
|
|
||||||
:param by_js: 是否用js方式点击,逻辑与click()一致
|
:param by_js: 是否用js方式点击,逻辑与click()一致
|
||||||
:param timeout: 等待下载触发的超时时间,为None则使用页面对象设置
|
:param timeout: 等待下载触发的超时时间,为None则使用页面对象设置
|
||||||
:return: DownloadMission对象
|
:return: DownloadMission对象
|
||||||
|
@ -27,7 +27,9 @@ class DownloadManager(object):
|
|||||||
self._missions = {} # {guid: DownloadMission}
|
self._missions = {} # {guid: DownloadMission}
|
||||||
self._tab_missions = {} # {tab_id: [DownloadMission, ...]}
|
self._tab_missions = {} # {tab_id: [DownloadMission, ...]}
|
||||||
self._flags = {} # {tab_id: [bool, DownloadMission]}
|
self._flags = {} # {tab_id: [bool, DownloadMission]}
|
||||||
|
self._waiting_tab = set() # click.to_download()专用
|
||||||
self._tmp_path = '.'
|
self._tmp_path = '.'
|
||||||
|
self._page_id = None
|
||||||
|
|
||||||
self._running = False
|
self._running = False
|
||||||
|
|
||||||
@ -97,11 +99,16 @@ class DownloadManager(object):
|
|||||||
self._tab_missions.pop(tab_id, None)
|
self._tab_missions.pop(tab_id, None)
|
||||||
self._flags.pop(tab_id, None)
|
self._flags.pop(tab_id, None)
|
||||||
TabDownloadSettings.TABS.pop(tab_id, None)
|
TabDownloadSettings.TABS.pop(tab_id, None)
|
||||||
|
self._waiting_tab.discard(tab_id)
|
||||||
|
|
||||||
def _onDownloadWillBegin(self, **kwargs):
|
def _onDownloadWillBegin(self, **kwargs):
|
||||||
guid = kwargs['guid']
|
guid = kwargs['guid']
|
||||||
tab_id = self._browser._frames.get(kwargs['frameId'], 'browser')
|
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)
|
settings = TabDownloadSettings(tab)
|
||||||
if settings.rename:
|
if settings.rename:
|
||||||
@ -139,16 +146,21 @@ class DownloadManager(object):
|
|||||||
overwrite = False
|
overwrite = False
|
||||||
|
|
||||||
m = DownloadMission(self, tab_id, guid, settings.path, name, kwargs['url'], self._tmp_path, overwrite)
|
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
|
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)
|
self.cancel(m)
|
||||||
elif skip:
|
elif skip:
|
||||||
self.skip(m)
|
self.skip(m)
|
||||||
else:
|
else:
|
||||||
self._tab_missions.setdefault(tab_id, []).append(m)
|
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
|
self._flags[tab] = m
|
||||||
|
|
||||||
def _onDownloadProgress(self, **kwargs):
|
def _onDownloadProgress(self, **kwargs):
|
||||||
|
@ -18,8 +18,10 @@ class DownloadManager(object):
|
|||||||
_missions: Dict[str, DownloadMission] = ...
|
_missions: Dict[str, DownloadMission] = ...
|
||||||
_tab_missions: dict = ...
|
_tab_missions: dict = ...
|
||||||
_flags: dict = ...
|
_flags: dict = ...
|
||||||
|
_waiting_tab: set = ...
|
||||||
_running: bool = ...
|
_running: bool = ...
|
||||||
_tmp_path: str = ...
|
_tmp_path: str = ...
|
||||||
|
_page_id: Optional[str] = ...
|
||||||
|
|
||||||
def __init__(self, browser: Chromium):
|
def __init__(self, browser: Chromium):
|
||||||
"""
|
"""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user