优化下载逻辑,待测试

This commit is contained in:
g1879 2024-10-23 18:30:03 +08:00
parent 4407a0daa1
commit 5423976bb2
5 changed files with 51 additions and 38 deletions

View File

@ -121,23 +121,15 @@ class Clicker(object):
return self.at(count=times)
def to_download(self, save_path=None, rename=None, suffix=None, new_tab=False, by_js=False, timeout=None):
tmp_save_path = None
if not self._ele.tab._browser._dl_mgr._running:
self._ele.tab._browser.set.download_path('.')
if self._ele.tab._type.endswith('Page'):
obj = browser = self._ele.tab._browser
obj = browser = self._ele.owner._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
elif new_tab:
obj = browser = self._ele.tab._browser
obj = browser = self._ele.owner._browser
tid = 'browser'
t_settings = TabDownloadSettings(self._ele.owner.tab_id)
b_settings = TabDownloadSettings('browser')
@ -145,15 +137,17 @@ class Clicker(object):
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())
if save_path:
tmp_path = obj.download_path
obj.set.download_path(save_path)
else:
tmp_path = None
if rename or suffix:
obj.set.download_file_name(rename, suffix)
if timeout is None:
@ -163,12 +157,11 @@ class Clicker(object):
self.left(by_js=by_js)
m = wait_mission(browser, tid, timeout)
if m:
if tmp_save_path:
m.path = tmp_save_path
if new_tab:
self._ele.owner.browser._dl_mgr._tab_missions.setdefault(self._ele.owner.tab_id, []).append(m)
m.from_tab = self._ele.owner
if tmp_path:
obj.set.download_path(tmp_path)
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
def to_upload(self, file_paths, by_js=False):

View File

@ -27,6 +27,7 @@ class DownloadManager(object):
self._missions = {} # {guid: DownloadMission}
self._tab_missions = {} # {tab_id: [DownloadMission, ...]}
self._flags = {} # {tab_id: [bool, DownloadMission]}
self._tmp_path = '.'
self._running = False
@ -42,6 +43,7 @@ class DownloadManager(object):
self._browser._driver.set_callback('Browser.downloadWillBegin', self._onDownloadWillBegin)
r = self._browser._run_cdp('Browser.setDownloadBehavior', downloadPath=self._browser._download_path,
behavior='allowAndName', eventsEnabled=True)
self._tmp_path = self._browser._download_path
if 'error' in r:
print('浏览器版本太低无法使用下载管理功能。')
self._running = True
@ -126,14 +128,17 @@ class DownloadManager(object):
name = kwargs['suggestedFilename']
skip = False
overwrite = None # 存在且重命名
goal_path = Path(settings.path) / name
if goal_path.exists():
if settings.when_file_exists == 'skip':
skip = True
elif settings.when_file_exists == 'overwrite':
goal_path.unlink()
overwrite = True # 存在且覆盖
else: # 不存在
overwrite = False
m = DownloadMission(self, tab_id, guid, settings.path, name, kwargs['url'], self._browser.download_path)
m = DownloadMission(self, tab_id, guid, settings.path, name, kwargs['url'], self._tmp_path, overwrite)
self._missions[guid] = m
if self.get_flag(tab) is False: # 取消该任务
@ -155,13 +160,16 @@ class DownloadManager(object):
elif kwargs['state'] == 'completed':
if mission.state == 'skipped':
Path(f'{mission.save_path}{sep}{mission.id}').unlink(True)
Path(f'{mission.tmp_path}{sep}{mission.id}').unlink(True)
self.set_done(mission, 'skipped')
return
mission.received_bytes = kwargs['receivedBytes']
mission.total_bytes = kwargs['totalBytes']
form_path = f'{mission.save_path}{sep}{mission.id}'
to_path = str(get_usable_path(f'{mission.path}{sep}{mission.name}'))
form_path = f'{mission.tmp_path}{sep}{mission.id}'
if mission._overwrite is None:
to_path = str(get_usable_path(f'{mission.folder}{sep}{mission.name}'))
else:
to_path = f'{mission.folder}{sep}{mission.name}'
not_moved = True
for _ in range(10):
try:
@ -204,19 +212,20 @@ class TabDownloadSettings(object):
class DownloadMission(object):
def __init__(self, mgr, tab_id, _id, path, name, url, save_path):
def __init__(self, mgr, tab_id, _id, folder, name, url, tmp_path, overwrite):
self._mgr = mgr
self.url = url
self.tab_id = tab_id
self.from_tab = None
self.id = _id
self.path = path
self.folder = folder
self.name = name
self.state = 'running'
self.total_bytes = None
self.received_bytes = 0
self.final_path = None
self.save_path = save_path
self.tmp_path = tmp_path
self._overwrite = overwrite
self._is_done = False
def __repr__(self):
@ -240,7 +249,7 @@ class DownloadMission(object):
while self.name is None and perf_counter() < end_time:
sleep(0.01)
print(f'文件名:{self.name}')
print(f'标路径:{self.path}')
print(f'录路径:{self.folder}')
if timeout is None:
while not self.is_done:
@ -260,11 +269,16 @@ class DownloadMission(object):
if show:
if self.state == 'completed':
print(f'下载完成 {self.final_path}')
if self._overwrite is None:
print(f'完成并重命名 {self.final_path}')
elif self._overwrite is False:
print(f'下载完成 {self.final_path}')
else:
print(f'已覆盖 {self.final_path}')
elif self.state == 'canceled':
print(f'下载取消')
elif self.state == 'skipped':
print(f'已跳过')
print(f'已跳过 {self.folder}{sep}{self.name}')
print()
return self.final_path if self.final_path else False

View File

@ -19,6 +19,7 @@ class DownloadManager(object):
_tab_missions: dict = ...
_flags: dict = ...
_running: bool = ...
_tmp_path: str = ...
def __init__(self, browser: Chromium):
"""
@ -145,31 +146,34 @@ class DownloadMission(object):
_mgr: DownloadManager = ...
url: str = ...
id: str = ...
path: str = ...
folder: str = ...
name: str = ...
state: str = ...
total_bytes: Optional[int] = ...
received_bytes: int = ...
final_path: Optional[str] = ...
save_path: str = ...
tmp_path: str = ...
_overwrite: bool = ...
_is_done: bool = ...
def __init__(self,
mgr: DownloadManager,
tab_id: str,
_id: str,
path: str,
folder: str,
name: str,
url: str,
save_path: str):
tmp_path: str,
overwrite:bool):
"""
:param mgr: BrowserDownloadManager对象
:param tab_id: 标签页id
:param _id: 任务id
:param path: 最终保存路径
:param folder: 最终保存文件夹路径
:param name: 文件名
:param url: url
:param save_path: 下载临时路径
:param tmp_path: 下载临时路径
:param overwrite: 是否已存在同名文件None表示重命名
"""
...

View File

@ -271,9 +271,11 @@ class ChromiumPageSetter(TabSetter):
self._owner._DownloadKit.set.save_path(path)
def download_file_name(self, name=None, suffix=None):
# super().download_file_name(name=name, suffix=suffix)
self._owner.browser.set.download_file_name(name, suffix)
def when_download_file_exists(self, mode):
self._owner.browser.set.when_download_file_exists(mode)
class WebPageSetter(ChromiumPageSetter):
def __init__(self, owner):

View File

@ -24,7 +24,7 @@ def from_selenium(driver):
address, port = driver.caps.get('goog:chromeOptions', {}).get('debuggerAddress', ':').split(':')
if not address:
raise RuntimeError('获取失败。')
co = ChromiumOptions().set_local_port(f'{address}:{port}')
co = ChromiumOptions().set_local_port(port)
co._ua_set = True
return Chromium(co)