mirror of
https://gitee.com/g1879/DrissionPage.git
synced 2024-12-10 04:00:23 +08:00
基本完善下载功能
This commit is contained in:
parent
3adb4508a6
commit
6154192741
@ -99,12 +99,13 @@ class ChromiumPage(ChromiumBase):
|
||||
self._tab_obj.Page.javascriptDialogOpening = self._on_alert_open
|
||||
self._tab_obj.Page.javascriptDialogClosed = self._on_alert_close
|
||||
self._main_tab = self.tab_id
|
||||
try:
|
||||
self.run_cdp('Browser.setDownloadBehavior', behavior='allow',
|
||||
downloadPath=self._driver_options.download_path, not_change=True)
|
||||
except:
|
||||
self.run_cdp('Page.setDownloadBehavior', behavior='allow',
|
||||
downloadPath=self._driver_options.download_path, not_change=True)
|
||||
self.download_set.use_DownloadKit()
|
||||
# try:
|
||||
# self.run_cdp('Browser.setDownloadBehavior', behavior='allow',
|
||||
# downloadPath=self._driver_options.download_path, not_change=True)
|
||||
# except:
|
||||
# self.run_cdp('Page.setDownloadBehavior', behavior='allow',
|
||||
# downloadPath=self._driver_options.download_path, not_change=True)
|
||||
|
||||
@property
|
||||
def tabs_count(self):
|
||||
@ -404,6 +405,8 @@ class ChromiumDownloadSetter(DownloadSetter):
|
||||
super().__init__(page)
|
||||
self._behavior = 'allow'
|
||||
self._download_th = None
|
||||
self._split = False
|
||||
self._file_exists = 'rename'
|
||||
|
||||
def save_path(self, path):
|
||||
"""设置下载路径
|
||||
@ -437,20 +440,46 @@ class ChromiumDownloadSetter(DownloadSetter):
|
||||
self._page.driver.Browser.setDownloadBehavior(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):
|
||||
"""拦截浏览器下载并用downloadKit下载"""
|
||||
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():
|
||||
self._download_th = Thread(target=self._wait_download_complete, daemon=False)
|
||||
self._download_th.start()
|
||||
print(f'下载:{kwargs["url"]}')
|
||||
|
||||
def _wait_download_complete(self):
|
||||
"""等待下载完成"""
|
||||
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):
|
||||
"""用于保存alert信息的类"""
|
||||
|
||||
|
@ -110,18 +110,36 @@ class ChromiumDownloadSetter(object):
|
||||
self._page: ChromiumPage = ...
|
||||
self._behavior: str = ...
|
||||
self._download_th: Thread = ...
|
||||
self._split: bool = ...
|
||||
self._file_exists: str = ...
|
||||
|
||||
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_DownloadKit(self) -> None: ...
|
||||
|
||||
def split(self, on_off: bool) -> None: ...
|
||||
|
||||
@property
|
||||
def if_file_exists(self) -> FileExists: ...
|
||||
|
||||
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):
|
||||
|
||||
def __init__(self):
|
||||
|
@ -12,8 +12,10 @@ from requests.cookies import RequestsCookieJar
|
||||
from requests.structures import CaseInsensitiveDict
|
||||
|
||||
from .base import BasePage
|
||||
from .chromium_page import ChromiumPage
|
||||
from .configs.session_options import SessionOptions
|
||||
from .session_element import SessionElement
|
||||
from .web_page import WebPage
|
||||
|
||||
|
||||
class SessionPage(BasePage):
|
||||
@ -155,7 +157,7 @@ class SessionPage(BasePage):
|
||||
|
||||
|
||||
class DownloadSetter(object):
|
||||
def __init__(self, page: BasePage):
|
||||
def __init__(self, page: Union[SessionPage, WebPage, ChromiumPage]):
|
||||
self._page: SessionPage = ...
|
||||
|
||||
def save_path(self, path: Union[str, Path]): ...
|
||||
|
@ -3,6 +3,6 @@ requests
|
||||
tldextract
|
||||
lxml
|
||||
cssselect
|
||||
DownloadKit>=0.4.1
|
||||
DownloadKit>=0.4.2
|
||||
FlowViewer>=0.2.1
|
||||
websocket-client
|
Loading…
x
Reference in New Issue
Block a user