From 69801b4c3a55cd3cd36f5f5f0216331b14963960 Mon Sep 17 00:00:00 2001 From: g1879 Date: Sun, 22 Jan 2023 23:55:04 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9F=BA=E6=9C=AC=E5=AE=8C=E6=88=903.1.0?= =?UTF-8?q?=EF=BC=8C=E5=BE=85=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/chromium_page.py | 36 +--------------------------------- DrissionPage/chromium_page.pyi | 21 ++------------------ DrissionPage/session_page.py | 34 ++++++++++++++++++++++++++++++++ DrissionPage/session_page.pyi | 16 +++++++++++++++ MANIFEST.in | 8 ++++++-- requirements.txt | 2 +- setup.py | 4 ++-- 7 files changed, 62 insertions(+), 59 deletions(-) diff --git a/DrissionPage/chromium_page.py b/DrissionPage/chromium_page.py index 0afb2c2..2e595ab 100644 --- a/DrissionPage/chromium_page.py +++ b/DrissionPage/chromium_page.py @@ -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信息的类""" diff --git a/DrissionPage/chromium_page.pyi b/DrissionPage/chromium_page.pyi index a7c4aab..7c55366 100644 --- a/DrissionPage/chromium_page.pyi +++ b/DrissionPage/chromium_page.pyi @@ -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): diff --git a/DrissionPage/session_page.py b/DrissionPage/session_page.py index 1b88eb6..a1fab0c 100644 --- a/DrissionPage/session_page.py +++ b/DrissionPage/session_page.py @@ -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 diff --git a/DrissionPage/session_page.pyi b/DrissionPage/session_page.pyi index 7ae575f..ee0a9bf 100644 --- a/DrissionPage/session_page.pyi +++ b/DrissionPage/session_page.pyi @@ -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: ... diff --git a/MANIFEST.in b/MANIFEST.in index 247a544..11a494f 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,2 +1,6 @@ -include DrissionPage/configs.ini -include DrissionPage/*.pyi \ No newline at end of file +include DrissionPage/configs/configs.ini +include DrissionPage/*.pyi +include DrissionPage/configs/*.py +include DrissionPage/configs/*.pyi +include DrissionPage/functions/*.py +include DrissionPage/functions/*.pyi \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 7c43261..ab49a53 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,6 +3,6 @@ requests tldextract lxml cssselect -DownloadKit>=0.4.2 +DownloadKit>=0.4.3 FlowViewer>=0.2.1 websocket-client \ No newline at end of file diff --git a/setup.py b/setup.py index a84e239..f46539a 100644 --- a/setup.py +++ b/setup.py @@ -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" ],