perf(download.py): 优化文件下载功能

This commit is contained in:
JoeanAmier
2024-12-04 19:50:15 +08:00
parent 561984356f
commit 4a8b0e57d4
7 changed files with 31 additions and 5 deletions

Binary file not shown.

View File

@@ -300,3 +300,6 @@ msgstr "Works download record switch"
msgid "文件 {0} 格式判断失败,错误信息:{1}" msgid "文件 {0} 格式判断失败,错误信息:{1}"
msgstr "Format recognition failed for file {0}, error message: {1}" msgstr "Format recognition failed for file {0}, error message: {1}"
msgid "文件 {0} 缓存异常,重新下载"
msgstr "File {0} cache exception, download again"

View File

@@ -300,3 +300,6 @@ msgstr ""
msgid "文件 {0} 格式判断失败,错误信息:{1}" msgid "文件 {0} 格式判断失败,错误信息:{1}"
msgstr "" msgstr ""
msgid "文件 {0} 缓存异常,重新下载"
msgstr ""

View File

@@ -6,6 +6,7 @@ from typing import TYPE_CHECKING, Any
from aiofiles import open from aiofiles import open
from httpx import HTTPError from httpx import HTTPError
from ..expansion import CacheError
from ..module import ERROR from ..module import ERROR
from ..module import ( from ..module import (
FILE_SIGNATURES_LENGTH, FILE_SIGNATURES_LENGTH,
@@ -196,6 +197,10 @@ class Download:
try: try:
async with self.client.stream("GET", url, headers=headers, ) as response: async with self.client.stream("GET", url, headers=headers, ) as response:
await sleep_time() await sleep_time()
if response.status_code == 416:
raise CacheError(
self.message("文件 {0} 缓存异常,重新下载").format(temp.name),
)
response.raise_for_status() response.raise_for_status()
# self.__create_progress( # self.__create_progress(
# bar, # bar,
@@ -220,7 +225,6 @@ class Download:
logging(log, self.message("文件 {0} 下载成功").format(real.name)) logging(log, self.message("文件 {0} 下载成功").format(real.name))
return True return True
except HTTPError as error: except HTTPError as error:
# self.manager.delete(temp)
# self.__create_progress(bar, None) # self.__create_progress(bar, None)
logging( logging(
log, log,
@@ -234,6 +238,13 @@ class Download:
# WARNING, # WARNING,
# ) # )
return False return False
except CacheError as error:
self.manager.delete(temp)
logging(
log,
str(error),
ERROR,
)
@staticmethod @staticmethod
def __create_progress(bar, total: int | None, completed=0, ): def __create_progress(bar, total: int | None, completed=0, ):

View File

@@ -1,9 +1,10 @@
from .browser import BrowserCookie from .browser import BrowserCookie
from .cleaner import Cleaner
from .converter import Converter from .converter import Converter
from .error import CacheError
from .file_folder import file_switch
from .file_folder import remove_empty_directories
from .namespace import Namespace from .namespace import Namespace
from .truncate import beautify_string from .truncate import beautify_string
from .truncate import trim_string from .truncate import trim_string
from .truncate import truncate_string from .truncate import truncate_string
from .file_folder import file_switch
from .file_folder import remove_empty_directories
from .cleaner import Cleaner

View File

@@ -0,0 +1,7 @@
class CacheError(Exception):
def __init__(self, message: str):
super().__init__(message)
self.message = message
def __str__(self):
return self.message

View File

@@ -2,4 +2,5 @@
1. 优化文件名称非法字符处理 1. 优化文件名称非法字符处理
2. 适配新版本 HTTPX 库 2. 适配新版本 HTTPX 库
3. 移除内置延时机制 3. 优化文件下载功能
4. 移除内置延时机制