diff --git a/locale/en_GB/LC_MESSAGES/xhs.mo b/locale/en_GB/LC_MESSAGES/xhs.mo index 2011a5b..f08fa11 100644 Binary files a/locale/en_GB/LC_MESSAGES/xhs.mo and b/locale/en_GB/LC_MESSAGES/xhs.mo differ diff --git a/locale/en_GB/LC_MESSAGES/xhs.po b/locale/en_GB/LC_MESSAGES/xhs.po index 5e6b769..177261e 100644 --- a/locale/en_GB/LC_MESSAGES/xhs.po +++ b/locale/en_GB/LC_MESSAGES/xhs.po @@ -297,3 +297,6 @@ msgstr "Browser name or serial number input error!" msgid "作品下载记录开关" msgstr "Works download record switch" + +msgid "文件 {0} 格式判断失败,错误信息:{1}" +msgstr "Format recognition failed for file {0}, error message: {1}" diff --git a/locale/zh_CN/LC_MESSAGES/xhs.po b/locale/zh_CN/LC_MESSAGES/xhs.po index e5cd8f0..eca5426 100644 --- a/locale/zh_CN/LC_MESSAGES/xhs.po +++ b/locale/zh_CN/LC_MESSAGES/xhs.po @@ -297,3 +297,6 @@ msgstr "" msgid "作品下载记录开关" msgstr "" + +msgid "文件 {0} 格式判断失败,错误信息:{1}" +msgstr "" diff --git a/source/application/download.py b/source/application/download.py index dd3dc9a..9429b92 100644 --- a/source/application/download.py +++ b/source/application/download.py @@ -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}") diff --git a/source/module/__init__.py b/source/module/__init__.py index dadc533..5734240 100644 --- a/source/module/__init__.py +++ b/source/module/__init__.py @@ -28,6 +28,8 @@ from .static import ( USERAGENT, SEC_CH_UA, SEC_CH_UA_PLATFORM, + FILE_SIGNATURES, + FILE_SIGNATURES_LENGTH, ) from .tools import ( retry, diff --git a/source/module/static.py b/source/module/static.py index 1b96b80..6529327 100644 --- a/source/module/static.py +++ b/source/module/static.py @@ -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) diff --git a/static/Release_Notes.md b/static/Release_Notes.md index b7115a4..9f1437d 100644 --- a/static/Release_Notes.md +++ b/static/Release_Notes.md @@ -2,6 +2,8 @@ 1. 优化文件后缀处理逻辑 +
旧版本升级后首次运行请删除配置文件 settings.json,删除后重新运行程序会自动生成新的默认配置文件!