mirror of
https://github.com/JoeanAmier/XHS-Downloader.git
synced 2026-03-22 15:07:17 +08:00
perf(download.py): 优化代码逻辑
1. 更新英语翻译 2. 修复代码逻辑错误 3. 优化代码变量名 4. 优化代码格式
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
from asyncio import Semaphore
|
||||
from asyncio import gather
|
||||
from os import path
|
||||
from pathlib import Path
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
@@ -8,12 +7,15 @@ from aiofiles import open
|
||||
from httpx import HTTPError
|
||||
|
||||
from source.module import ERROR
|
||||
from source.module import (
|
||||
FILE_SIGNATURES_LENGTH,
|
||||
FILE_SIGNATURES,
|
||||
)
|
||||
# from source.module import WARNING
|
||||
from source.module import Manager
|
||||
from source.module import logging
|
||||
from source.module import retry as re_download
|
||||
from source.module import sleep_time
|
||||
from source.module.static import FILE_HEADER_MAX_LENGTH, MAGIC_DICT
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from httpx import AsyncClient
|
||||
@@ -179,7 +181,13 @@ class Download:
|
||||
async for chunk in response.aiter_bytes(self.chunk):
|
||||
await f.write(chunk)
|
||||
# self.__update_progress(bar, len(chunk))
|
||||
real = await self.__fix_suffix(temp, path, name, suffix, log)
|
||||
real = await self.__suffix_with_file(
|
||||
temp,
|
||||
path,
|
||||
name,
|
||||
suffix,
|
||||
log,
|
||||
)
|
||||
self.manager.move(temp, real)
|
||||
# self.__create_progress(bar, None)
|
||||
logging(log, self.message("文件 {0} 下载成功").format(real.name))
|
||||
@@ -245,25 +253,24 @@ class Download:
|
||||
headers["Range"] = f"bytes={(p := self.__get_resume_byte_position(file))}-"
|
||||
return p
|
||||
|
||||
@staticmethod
|
||||
async def __fix_suffix(
|
||||
temp: Path,
|
||||
path: Path,
|
||||
name: str,
|
||||
suffix: str,
|
||||
log
|
||||
async def __suffix_with_file(
|
||||
self,
|
||||
temp: Path,
|
||||
path: Path,
|
||||
name: str,
|
||||
default_suffix: str,
|
||||
log,
|
||||
) -> Path:
|
||||
try:
|
||||
async with open(temp, "rb") as f:
|
||||
file_start = await f.read(FILE_HEADER_MAX_LENGTH)
|
||||
for offset, magic, suffix in MAGIC_DICT:
|
||||
if file_start[offset:offset + len(magic)] == magic:
|
||||
file_start = await f.read(FILE_SIGNATURES_LENGTH)
|
||||
for offset, signature, suffix in FILE_SIGNATURES:
|
||||
if file_start[offset:offset + len(signature)] == signature:
|
||||
return path.joinpath(f"{name}.{suffix}")
|
||||
return path.joinpath(f"{name}.{suffix}")
|
||||
except Exception as error:
|
||||
logging(
|
||||
log,
|
||||
f"文件 {temp.name} 后缀修复失败,错误信息: {repr(error)}",
|
||||
self.message("文件 {0} 格式判断失败,错误信息:{1}").format(temp.name, repr(error)),
|
||||
ERROR,
|
||||
)
|
||||
return path.joinpath(f"{name}.{suffix}")
|
||||
return path.joinpath(f"{name}.{default_suffix}")
|
||||
|
||||
@@ -28,6 +28,8 @@ from .static import (
|
||||
USERAGENT,
|
||||
SEC_CH_UA,
|
||||
SEC_CH_UA_PLATFORM,
|
||||
FILE_SIGNATURES,
|
||||
FILE_SIGNATURES_LENGTH,
|
||||
)
|
||||
from .tools import (
|
||||
retry,
|
||||
|
||||
@@ -1,31 +1,8 @@
|
||||
from pathlib import Path
|
||||
|
||||
__all__ = [
|
||||
"VERSION_MAJOR",
|
||||
"VERSION_MINOR",
|
||||
"VERSION_BETA",
|
||||
"ROOT",
|
||||
"REPOSITORY",
|
||||
"LICENCE",
|
||||
"RELEASES",
|
||||
"MASTER",
|
||||
"PROMPT",
|
||||
"GENERAL",
|
||||
"PROGRESS",
|
||||
"ERROR",
|
||||
"WARNING",
|
||||
"INFO",
|
||||
"USERSCRIPT",
|
||||
"HEADERS",
|
||||
"PROJECT",
|
||||
"USERAGENT",
|
||||
"SEC_CH_UA",
|
||||
"SEC_CH_UA_PLATFORM",
|
||||
]
|
||||
|
||||
VERSION_MAJOR = 2
|
||||
VERSION_MINOR = 2
|
||||
VERSION_BETA = False
|
||||
VERSION_MINOR = 3
|
||||
VERSION_BETA = True
|
||||
ROOT = Path(__file__).resolve().parent.parent.parent
|
||||
PROJECT = f"XHS-Downloader V{VERSION_MAJOR}.{
|
||||
VERSION_MINOR}{" Beta" if VERSION_BETA else ""}"
|
||||
@@ -67,13 +44,14 @@ ERROR = "b bright_red"
|
||||
WARNING = "b bright_yellow"
|
||||
INFO = "b bright_green"
|
||||
|
||||
MAGIC_DICT = {
|
||||
# 分别为偏移量(字节)、魔数、后缀名
|
||||
FILE_SIGNATURES: tuple[tuple[int, bytes, str,], ...] = (
|
||||
# 分别为偏移量(字节)、十六进制签名、后缀
|
||||
# 参考:https://en.wikipedia.org/wiki/List_of_file_signatures
|
||||
# 参考:https://www.garykessler.net/library/file_sigs.html
|
||||
(0, b"\xFF\xD8\xFF", "jpg"),
|
||||
(0, b"\x89\x50\x4E\x47\x0D\x0A\x1A\x0A", "png"),
|
||||
(0, b"\x00\x00\x00", "avif"),
|
||||
(4, b"\x66\x74\x79\x70\x68\x65\x69\x63", "heic"),
|
||||
(8, b"\x57\x45\x42\x50", "webp"),
|
||||
}
|
||||
FILE_HEADER_MAX_LENGTH = max(offset + len(magic) for offset, magic, _ in MAGIC_DICT)
|
||||
)
|
||||
FILE_SIGNATURES_LENGTH = max(offset + len(signature) for offset, signature, _ in FILE_SIGNATURES)
|
||||
|
||||
Reference in New Issue
Block a user