改进下载功能,待测试

This commit is contained in:
g1879 2023-10-16 00:14:17 +08:00
parent ed2cc9a579
commit a37ea0a50d
6 changed files with 22 additions and 29 deletions

View File

@ -4,6 +4,7 @@
@Contact : g1879@qq.com
"""
from abc import abstractmethod
from pathlib import Path
from re import sub
from urllib.parse import quote
@ -366,7 +367,7 @@ class BasePage(BaseParser):
self.retry_times = 3
self.retry_interval = 2
self._DownloadKit = None
self._download_path = ''
self._download_path = str(Path('.').absolute())
@property
def title(self):
@ -403,7 +404,7 @@ class BasePage(BaseParser):
def download(self):
"""返回下载器对象"""
if self._DownloadKit is None:
self._DownloadKit = DownloadKit(session=self, goal_path=self.download_path)
self._DownloadKit = DownloadKit(driver=self, goal_path=self.download_path)
return self._DownloadKit
def _before_connect(self, url, retry, interval):

View File

@ -22,8 +22,9 @@ class BrowserDownloadManager(object):
"""
:param page: ChromiumPage对象
"""
if page.browser_driver.id in BrowserDownloadManager.BROWSERS:
if hasattr(self, '_created'):
return
self._created = True
self._page = page
self._lock = Lock()
@ -39,6 +40,9 @@ class BrowserDownloadManager(object):
self._page.browser_driver.set_listener('Browser.downloadProgress', self._onDownloadProgress)
self._page.browser_driver.set_listener('Browser.downloadWillBegin', self._onDownloadWillBegin)
self._page.browser_driver.call_method('Browser.setDownloadBehavior',
downloadPath=self._page.download_path,
behavior='allowAndName', eventsEnabled=True)
BrowserDownloadManager.BROWSERS[page.browser_driver.id] = self
@ -54,6 +58,9 @@ 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)
def set_rename(self, tab_id, rename):
"""设置某个tab的重命名文件名
@ -120,7 +127,7 @@ class BrowserDownloadManager(object):
def _onDownloadWillBegin(self, **kwargs):
"""用于获取弹出新标签页触发的下载任务"""
guid = kwargs['guid']
end = perf_counter() + .3
end = perf_counter() + .5
while perf_counter() < end:
tab_id = self._guid_and_tab.get(guid, None)
if tab_id:
@ -178,7 +185,7 @@ class BrowserDownloadManager(object):
move(form_path, to_path)
self.set_done(mission, 'completed', final_path=to_path)
else:
else: # canceled
self.set_done(mission, 'canceled')
@ -197,11 +204,16 @@ class TabDownloadSettings(object):
"""
:param tab_id: tab id
"""
if hasattr(self, '_created'):
return
self._created = True
self.tab_id = tab_id
self.rename = None
self.path = ''
self.when_file_exists = 'rename'
TabDownloadSettings.TABS[tab_id] = self
class DownloadMission(object):
def __init__(self, mgr, tab_id, _id, path, name, url):
@ -217,7 +229,6 @@ class DownloadMission(object):
self.final_path = None
def __repr__(self):
# return f'<DownloadMission {self.id} {self.state} {self.rate}>'
return f'<DownloadMission {id(self)} {self.rate}>'
@property

View File

@ -48,7 +48,7 @@ class ChromiumBase(BasePage):
self._actions = None
self._listener = None
self._download_path = ''
self._download_path = str(Path('.').absolute())
if isinstance(address, int) or (isinstance(address, str) and address.isdigit()):
address = f'127.0.0.1:{address}'

View File

@ -3,6 +3,7 @@
@Author : g1879
@Contact : g1879@qq.com
"""
from pathlib import Path
from time import perf_counter, sleep
from .browser_download_manager import BrowserDownloadManager
@ -65,7 +66,7 @@ class ChromiumPage(ChromiumBase):
if self._driver_options.timeouts['implicit'] is not None:
self._timeout = self._driver_options.timeouts['implicit']
self._page_load_strategy = self._driver_options.page_load_strategy
self._download_path = self._driver_options.download_path
self._download_path = str(Path(self._driver_options.download_path).absolute())
def _connect_browser(self, tab_id=None):
"""连接浏览器,在第一次时运行

View File

@ -123,6 +123,7 @@ class ChromiumBaseSetter(object):
:return: None
"""
self._page._download_path = str(Path(path).absolute())
self._page.browser._dl_mgr.set_path(self._page.tab_id, path)
if self._page._DownloadKit:
self._page._DownloadKit.set.goal_path(path)
@ -131,14 +132,6 @@ class TabSetter(ChromiumBaseSetter):
def __init__(self, page):
super().__init__(page)
def download_path(self, path):
"""设置下载路径
:param path: 下载路径
:return: None
"""
super().download_path(path)
self._page.browser._dl_mgr.set_path(self._page.tab_id, path)
def download_file_name(self, name):
"""设置下一个被下载文件的名称
:param name: 文件名可不含后缀
@ -176,15 +169,6 @@ class ChromiumPageSetter(ChromiumBaseSetter):
tab_or_id = tab_or_id.tab_id
self._page._control_session.get(f'http://{self._page.address}/json/activate/{tab_or_id}')
def download_path(self, path):
"""设置下载路径
:param path: 下载路径
:return: None
"""
super().download_path(path)
self._page.browser_driver.call_method('Browser.setDownloadBehavior', downloadPath=self._page.download_path,
behavior='allowAndName', eventsEnabled=True)
class SessionPageSetter(object):
def __init__(self, page):

View File

@ -56,8 +56,6 @@ class ChromiumBaseSetter(object):
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: ...
@ -73,8 +71,6 @@ class ChromiumPageSetter(ChromiumBaseSetter):
def tab_to_front(self, tab_or_id: Union[str, ChromiumTab] = None) -> None: ...
def download_path(self, path: Union[str, Path]) -> None: ...
class SessionPageSetter(object):
def __init__(self, page: SessionPage):