基本完成3.1.0,待测试

This commit is contained in:
g1879 2023-01-22 23:55:04 +08:00
parent 6154192741
commit 69801b4c3a
7 changed files with 62 additions and 59 deletions

View File

@ -100,12 +100,6 @@ class ChromiumPage(ChromiumBase):
self._tab_obj.Page.javascriptDialogClosed = self._on_alert_close
self._main_tab = self.tab_id
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):
@ -405,8 +399,6 @@ 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):
"""设置下载路径
@ -440,23 +432,11 @@ 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(file_url=kwargs['url'], goal_path=self._page.download_path,
rename=kwargs['suggestedFilename'], split=self._split, file_exists=self._file_exists)
rename=kwargs['suggestedFilename'])
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()
@ -466,20 +446,6 @@ class ChromiumDownloadSetter(DownloadSetter):
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信息的类"""

View File

@ -11,6 +11,7 @@ from typing import Union, Tuple, List
from DownloadKit import DownloadKit
from requests import Session
from session_page import DownloadSetter
from .configs.chromium_options import ChromiumOptions
from .chromium_base import ChromiumBase
from .chromium_driver import ChromiumDriver
@ -105,13 +106,11 @@ class ChromiumPage(ChromiumBase):
def _on_alert_open(self, **kwargs): ...
class ChromiumDownloadSetter(object):
class ChromiumDownloadSetter(DownloadSetter):
def __init__(self, page: ChromiumPage):
self._page: ChromiumPage = ...
self._behavior: str = ...
self._download_th: Thread = ...
self._split: bool = ...
self._file_exists: str = ...
def _wait_download_complete(self) -> None: ...
@ -121,25 +120,9 @@ class ChromiumDownloadSetter(object):
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):

View File

@ -335,6 +335,18 @@ class DownloadSetter(object):
def __init__(self, page):
self._page = page
@property
def if_file_exists(self):
"""返回用于设置存在同名文件时处理方法的对象"""
return FileExists(self)
def split(self, on_off):
"""设置是否允许拆分大文件用多线程下载
:param on_off: 是否启用多线程下载大文件
:return: None
"""
self._page.download.split = on_off
def save_path(self, path):
"""设置下载保存路径
:param path: 下载保存路径
@ -346,6 +358,28 @@ class DownloadSetter(object):
self._page.download.goal_path = path
class FileExists(object):
"""用于设置存在同名文件时处理方法"""
def __init__(self, setter):
"""
:param setter: DownloadSetter对象
"""
self._setter = setter
def skip(self):
"""设为跳过"""
self._setter._page.download.file_exists = 'skip'
def rename(self):
"""设为重命名,文件名后加序号"""
self._setter._page.download._file_exists = 'rename'
def overwrite(self):
"""设为覆盖"""
self._setter._page.download._file_exists = 'overwrite'
def check_headers(kwargs, headers, arg) -> bool:
"""检查kwargs或headers中是否有arg所示属性"""
return arg in kwargs['headers'] or arg in headers

View File

@ -160,9 +160,25 @@ class DownloadSetter(object):
def __init__(self, page: Union[SessionPage, WebPage, ChromiumPage]):
self._page: SessionPage = ...
@property
def if_file_exists(self) -> FileExists: ...
def split(self, on_off: bool) -> None: ...
def save_path(self, path: Union[str, Path]): ...
class FileExists(object):
def __init__(self, setter: DownloadSetter):
self._setter: DownloadSetter = ...
def skip(self): ...
def rename(self): ...
def overwrite(self): ...
def check_headers(kwargs: Union[dict, CaseInsensitiveDict], headers: Union[dict, CaseInsensitiveDict],
arg: str) -> bool: ...

View File

@ -1,2 +1,6 @@
include DrissionPage/configs.ini
include DrissionPage/configs/configs.ini
include DrissionPage/*.pyi
include DrissionPage/configs/*.py
include DrissionPage/configs/*.pyi
include DrissionPage/functions/*.py
include DrissionPage/functions/*.pyi

View File

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

View File

@ -6,7 +6,7 @@ with open("README.md", "r", encoding='utf-8') as fh:
setup(
name="DrissionPage",
version="3.0.34",
version="3.1.0",
author="g1879",
author_email="g1879@qq.com",
description="A module that integrates selenium and requests session, encapsulates common page operations.",
@ -22,7 +22,7 @@ setup(
"lxml",
"tldextract",
"requests",
"DownloadKit>=0.4.2",
"DownloadKit>=0.4.3",
"FlowViewer",
"websocket-client"
],