新增临时文件夹机制

This commit is contained in:
JoeamAmier 2023-09-03 16:17:29 +08:00
parent 26c4a7d9c9
commit 265c724ca8
2 changed files with 7 additions and 5 deletions

View File

@ -10,7 +10,7 @@ __all__ = ['Download']
class Download:
manager = Manager()
temp = Path("./temp")
temp = Path("./Temp")
def __init__(
self,
@ -44,16 +44,18 @@ class Download:
self.download(urls[0], f"{name}.mp4")
def download(self, url: str, name: str):
temp = self.temp.joinpath(name)
file = self.root.joinpath(name)
if self.manager.is_exists(file):
print(f"{file} 已存在,跳过下载!")
print(f"{name} 已存在,跳过下载!")
return
try:
with get(url, headers=self.headers, proxies=self.proxies, stream=True) as response:
with file.open("wb") as f:
with temp.open("wb") as f:
for chunk in response.iter_content(chunk_size=self.chunk):
f.write(chunk)
self.manager.move(temp, file)
print(f"{name} 下载成功!")
except exceptions.ChunkedEncodingError:
self.manager.delete(file)
self.manager.delete(temp)
print(f"网络异常,{name} 下载失败!")

View File

@ -14,5 +14,5 @@ class Manager:
path.unlink()
@staticmethod
def remove(temp: Path, path: Path):
def move(temp: Path, path: Path):
move(temp.resolve(), path.resolve())