自动跳过已下载的文件

This commit is contained in:
JoeamAmier 2023-09-01 22:40:09 +08:00
parent 674e0db065
commit d378e43dde
4 changed files with 24 additions and 14 deletions

View File

@ -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} 下载失败!")

View File

@ -1,6 +0,0 @@
__all__ = ['Manager']
class Manager:
def __init__(self):
pass

13
source/Manager.py Normal file
View File

@ -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()

View File

@ -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,