基本完善下载功能

This commit is contained in:
g1879 2023-01-22 23:17:43 +08:00
parent 3adb4508a6
commit 6154192741
5 changed files with 61 additions and 12 deletions

View File

@ -99,12 +99,13 @@ class ChromiumPage(ChromiumBase):
self._tab_obj.Page.javascriptDialogOpening = self._on_alert_open self._tab_obj.Page.javascriptDialogOpening = self._on_alert_open
self._tab_obj.Page.javascriptDialogClosed = self._on_alert_close self._tab_obj.Page.javascriptDialogClosed = self._on_alert_close
self._main_tab = self.tab_id self._main_tab = self.tab_id
try: self.download_set.use_DownloadKit()
self.run_cdp('Browser.setDownloadBehavior', behavior='allow', # try:
downloadPath=self._driver_options.download_path, not_change=True) # self.run_cdp('Browser.setDownloadBehavior', behavior='allow',
except: # downloadPath=self._driver_options.download_path, not_change=True)
self.run_cdp('Page.setDownloadBehavior', behavior='allow', # except:
downloadPath=self._driver_options.download_path, not_change=True) # self.run_cdp('Page.setDownloadBehavior', behavior='allow',
# downloadPath=self._driver_options.download_path, not_change=True)
@property @property
def tabs_count(self): def tabs_count(self):
@ -404,6 +405,8 @@ class ChromiumDownloadSetter(DownloadSetter):
super().__init__(page) super().__init__(page)
self._behavior = 'allow' self._behavior = 'allow'
self._download_th = None self._download_th = None
self._split = False
self._file_exists = 'rename'
def save_path(self, path): def save_path(self, path):
"""设置下载路径 """设置下载路径
@ -437,20 +440,46 @@ class ChromiumDownloadSetter(DownloadSetter):
self._page.driver.Browser.setDownloadBehavior(behavior='deny') self._page.driver.Browser.setDownloadBehavior(behavior='deny')
self._behavior = 'deny' self._behavior = 'deny'
def split(self, on_off):
"""设置是否允许拆分大文件用多线程下载
:param on_off: 是否启用多线程下载大文件
:return: None
"""
self._split = on_off
@property
def if_file_exists(self):
"""返回用于设置存在同名文件时处理方法的对象"""
return FileExists(self)
def _download_by_DownloadKit(self, **kwargs): def _download_by_DownloadKit(self, **kwargs):
"""拦截浏览器下载并用downloadKit下载""" """拦截浏览器下载并用downloadKit下载"""
self._page.run_cdp('Browser.cancelDownload', guid=kwargs['guid'], not_change=True) self._page.run_cdp('Browser.cancelDownload', guid=kwargs['guid'], not_change=True)
self._page.download.add(kwargs['url'], self._page.download_path, kwargs['suggestedFilename']) self._page.download.add(file_url=kwargs['url'], goal_path=self._page.download_path,
rename=kwargs['suggestedFilename'], split=self._split, file_exists=self._file_exists)
if self._download_th is None or not self._download_th.is_alive(): if self._download_th is None or not self._download_th.is_alive():
self._download_th = Thread(target=self._wait_download_complete, daemon=False) self._download_th = Thread(target=self._wait_download_complete, daemon=False)
self._download_th.start() self._download_th.start()
print(f'下载:{kwargs["url"]}')
def _wait_download_complete(self): def _wait_download_complete(self):
"""等待下载完成""" """等待下载完成"""
self._page.download.wait() self._page.download.wait()
class FileExists(object):
def __init__(self, setter):
self._setter = setter
def skip(self):
self._setter._file_exists = 'skip'
def rename(self):
self._setter._file_exists = 'rename'
def overwrite(self):
self._setter._file_exists = 'overwrite'
class Alert(object): class Alert(object):
"""用于保存alert信息的类""" """用于保存alert信息的类"""

View File

@ -110,18 +110,36 @@ class ChromiumDownloadSetter(object):
self._page: ChromiumPage = ... self._page: ChromiumPage = ...
self._behavior: str = ... self._behavior: str = ...
self._download_th: Thread = ... self._download_th: Thread = ...
self._split: bool = ...
self._file_exists: str = ...
def _wait_download_complete(self) -> None: ... def _wait_download_complete(self) -> None: ...
def save_path(self, path) -> None: ... def save_path(self, path: Union[str, Path]) -> None: ...
def use_browser(self) -> None: ... def use_browser(self) -> None: ...
def use_DownloadKit(self) -> None: ... def use_DownloadKit(self) -> None: ...
def split(self, on_off: bool) -> None: ...
@property
def if_file_exists(self) -> FileExists: ...
def _download_by_DownloadKit(self, **kwargs) -> None: ... def _download_by_DownloadKit(self, **kwargs) -> None: ...
class FileExists(object):
def __init__(self, setter: ChromiumDownloadSetter):
self._setter: ChromiumDownloadSetter = ...
def skip(self): ...
def rename(self): ...
def overwrite(self): ...
class Alert(object): class Alert(object):
def __init__(self): def __init__(self):

View File

@ -12,8 +12,10 @@ from requests.cookies import RequestsCookieJar
from requests.structures import CaseInsensitiveDict from requests.structures import CaseInsensitiveDict
from .base import BasePage from .base import BasePage
from .chromium_page import ChromiumPage
from .configs.session_options import SessionOptions from .configs.session_options import SessionOptions
from .session_element import SessionElement from .session_element import SessionElement
from .web_page import WebPage
class SessionPage(BasePage): class SessionPage(BasePage):
@ -155,7 +157,7 @@ class SessionPage(BasePage):
class DownloadSetter(object): class DownloadSetter(object):
def __init__(self, page: BasePage): def __init__(self, page: Union[SessionPage, WebPage, ChromiumPage]):
self._page: SessionPage = ... self._page: SessionPage = ...
def save_path(self, path: Union[str, Path]): ... def save_path(self, path: Union[str, Path]): ...

View File

@ -3,6 +3,6 @@ requests
tldextract tldextract
lxml lxml
cssselect cssselect
DownloadKit>=0.4.1 DownloadKit>=0.4.2
FlowViewer>=0.2.1 FlowViewer>=0.2.1
websocket-client websocket-client

View File

@ -22,7 +22,7 @@ setup(
"lxml", "lxml",
"tldextract", "tldextract",
"requests", "requests",
"DownloadKit", "DownloadKit>=0.4.2",
"FlowViewer", "FlowViewer",
"websocket-client" "websocket-client"
], ],