From d378e43dde31bdf9338b650e546e29dfb12a2fa1 Mon Sep 17 00:00:00 2001 From: JoeamAmier Date: Fri, 1 Sep 2023 22:40:09 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=8A=A8=E8=B7=B3=E8=BF=87=E5=B7=B2?= =?UTF-8?q?=E4=B8=8B=E8=BD=BD=E7=9A=84=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/Download.py | 16 +++++++++++----- source/Manage.py | 6 ------ source/Manager.py | 13 +++++++++++++ source/__init__.py | 3 --- 4 files changed, 24 insertions(+), 14 deletions(-) delete mode 100644 source/Manage.py create mode 100644 source/Manager.py diff --git a/source/Download.py b/source/Download.py index 3728ca7..944ced3 100644 --- a/source/Download.py +++ b/source/Download.py @@ -3,20 +3,21 @@ from pathlib import Path from requests import exceptions from requests import get +from .Manager import Manager + __all__ = ['Download'] class Download: + manager = Manager() def __init__( self, - manager, - path, - folder, + path: str, + folder: str, headers: dict, proxies=None, chunk=256 * 1024, ): - self.manager = manager self.root = self.init_root(path, folder) self.headers = headers self.proxies = { @@ -41,11 +42,16 @@ class Download: self.download(urls[0], f"{name}.mp4") def download(self, url: str, name: str): + file = self.root.joinpath(name) + if self.manager.is_exists(file): + print(f"{file} 已存在,跳过下载!") + return try: with get(url, headers=self.headers, proxies=self.proxies, stream=True) as response: - with self.root.joinpath(name).open("wb") as f: + with file.open("wb") as f: for chunk in response.iter_content(chunk_size=self.chunk): f.write(chunk) print(f"{name} 下载成功!") except exceptions.ChunkedEncodingError: + self.manager.delete(file) print(f"网络异常,{name} 下载失败!") diff --git a/source/Manage.py b/source/Manage.py deleted file mode 100644 index a20d6da..0000000 --- a/source/Manage.py +++ /dev/null @@ -1,6 +0,0 @@ -__all__ = ['Manager'] - - -class Manager: - def __init__(self): - pass diff --git a/source/Manager.py b/source/Manager.py new file mode 100644 index 0000000..0fcc5f0 --- /dev/null +++ b/source/Manager.py @@ -0,0 +1,13 @@ +from pathlib import Path + +__all__ = ['Manager'] + + +class Manager: + @staticmethod + def is_exists(path: Path) -> bool: + return path.exists() + + @staticmethod + def delete(path: Path): + path.unlink() diff --git a/source/__init__.py b/source/__init__.py index 86e8675..a260403 100644 --- a/source/__init__.py +++ b/source/__init__.py @@ -4,7 +4,6 @@ from .Download import Download from .Explore import Explore from .Html import Html from .Image import Image -from .Manage import Manager from .Settings import Settings from .Video import Video @@ -26,12 +25,10 @@ class XHS: chunk=256 * 1024, ): self.html = Html(self.headers, proxies, timeout) - self.manager = Manager() self.image = Image() self.video = Video() self.explore = Explore() self.download = Download( - self.manager, path, folder, self.headers,