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,