mirror of
https://github.com/JoeanAmier/XHS-Downloader.git
synced 2026-03-22 06:57:16 +08:00
refactor: 重构项目翻译模块
This commit is contained in:
@@ -26,8 +26,6 @@ from .static import (
|
||||
HEADERS,
|
||||
PROJECT,
|
||||
USERAGENT,
|
||||
SEC_CH_UA,
|
||||
SEC_CH_UA_PLATFORM,
|
||||
FILE_SIGNATURES,
|
||||
FILE_SIGNATURES_LENGTH,
|
||||
MAX_WORKERS,
|
||||
@@ -37,4 +35,3 @@ from .tools import (
|
||||
logging,
|
||||
sleep_time,
|
||||
)
|
||||
from .translator import Translate
|
||||
|
||||
@@ -3,7 +3,6 @@ from re import compile
|
||||
from re import sub
|
||||
from shutil import move
|
||||
from shutil import rmtree
|
||||
from typing import Callable
|
||||
|
||||
from httpx import AsyncClient
|
||||
from httpx import AsyncHTTPTransport
|
||||
@@ -14,11 +13,10 @@ from httpx import get
|
||||
|
||||
from source.expansion import remove_empty_directories
|
||||
from .static import HEADERS
|
||||
# from .static import SEC_CH_UA
|
||||
# from .static import SEC_CH_UA_PLATFORM
|
||||
from .static import USERAGENT
|
||||
from .static import WARNING
|
||||
from .tools import logging
|
||||
from ..translation import _
|
||||
|
||||
__all__ = ["Manager"]
|
||||
|
||||
@@ -26,19 +24,19 @@ __all__ = ["Manager"]
|
||||
class Manager:
|
||||
NAME = compile(r"[^\u4e00-\u9fffa-zA-Z0-9-_!?,。;:“”()《》]")
|
||||
NAME_KEYS = (
|
||||
'收藏数量',
|
||||
'评论数量',
|
||||
'分享数量',
|
||||
'点赞数量',
|
||||
'作品标签',
|
||||
'作品ID',
|
||||
'作品标题',
|
||||
'作品描述',
|
||||
'作品类型',
|
||||
'发布时间',
|
||||
'最后更新时间',
|
||||
'作者昵称',
|
||||
'作者ID',
|
||||
"收藏数量",
|
||||
"评论数量",
|
||||
"分享数量",
|
||||
"点赞数量",
|
||||
"作品标签",
|
||||
"作品ID",
|
||||
"作品标题",
|
||||
"作品描述",
|
||||
"作品类型",
|
||||
"发布时间",
|
||||
"最后更新时间",
|
||||
"作者昵称",
|
||||
"作者ID",
|
||||
)
|
||||
NO_PROXY = {
|
||||
"http://": None,
|
||||
@@ -55,8 +53,6 @@ class Manager:
|
||||
folder: str,
|
||||
name_format: str,
|
||||
chunk: int,
|
||||
# sec_ch_ua: str,
|
||||
# sec_ch_ua_platform: str,
|
||||
user_agent: str,
|
||||
cookie: str,
|
||||
proxy: str | dict,
|
||||
@@ -69,19 +65,14 @@ class Manager:
|
||||
live_download: bool,
|
||||
download_record: bool,
|
||||
folder_mode: bool,
|
||||
# server: bool,
|
||||
transition: Callable[[str], str],
|
||||
_print: bool,
|
||||
):
|
||||
self.root = root
|
||||
self.temp = root.joinpath("./temp")
|
||||
self.path = self.__check_path(path)
|
||||
self.folder = self.__check_folder(folder)
|
||||
self.message = transition
|
||||
self.blank_headers = HEADERS | {
|
||||
'user-agent': user_agent or USERAGENT,
|
||||
# 'sec-ch-ua': sec_ch_ua or SEC_CH_UA,
|
||||
# 'sec-ch-ua-platform': sec_ch_ua_platform or SEC_CH_UA_PLATFORM,
|
||||
}
|
||||
self.headers = self.blank_headers | {
|
||||
'cookie': cookie,
|
||||
@@ -121,7 +112,6 @@ class Manager:
|
||||
self.image_download = self.check_bool(image_download, True)
|
||||
self.video_download = self.check_bool(video_download, True)
|
||||
self.live_download = self.check_bool(live_download, True)
|
||||
# self.server = self.check_bool(server, False)
|
||||
|
||||
def __check_path(self, path: str) -> Path:
|
||||
if not path:
|
||||
@@ -211,11 +201,11 @@ class Manager:
|
||||
}
|
||||
)
|
||||
response.raise_for_status()
|
||||
self.proxy_tip = (self.message("代理 {0} 测试成功").format(proxy),)
|
||||
self.proxy_tip = (_("代理 {0} 测试成功").format(proxy),)
|
||||
return proxy
|
||||
except TimeoutException:
|
||||
self.proxy_tip = (
|
||||
self.message("代理 {0} 测试超时").format(proxy),
|
||||
_("代理 {0} 测试超时").format(proxy),
|
||||
WARNING,
|
||||
)
|
||||
except (
|
||||
@@ -223,7 +213,7 @@ class Manager:
|
||||
HTTPStatusError,
|
||||
) as e:
|
||||
self.proxy_tip = (
|
||||
self.message("代理 {0} 测试失败:{1}").format(
|
||||
_("代理 {0} 测试失败:{1}").format(
|
||||
proxy,
|
||||
e,
|
||||
),
|
||||
|
||||
@@ -4,7 +4,7 @@ from re import compile
|
||||
|
||||
from aiosqlite import connect
|
||||
|
||||
from source.module import Manager
|
||||
from ..module import Manager
|
||||
|
||||
__all__ = ["IDRecorder", "DataRecorder", ]
|
||||
|
||||
|
||||
@@ -4,8 +4,6 @@ from pathlib import Path
|
||||
from platform import system
|
||||
|
||||
from .static import ROOT
|
||||
# from .static import SEC_CH_UA
|
||||
# from .static import SEC_CH_UA_PLATFORM
|
||||
from .static import USERAGENT
|
||||
|
||||
__all__ = ['Settings']
|
||||
@@ -16,8 +14,6 @@ class Settings:
|
||||
"work_path": "",
|
||||
"folder_name": "Download",
|
||||
"name_format": "发布时间 作者昵称 作品标题",
|
||||
# "sec_ch_ua": SEC_CH_UA,
|
||||
# "sec_ch_ua_platform": SEC_CH_UA_PLATFORM,
|
||||
"user_agent": USERAGENT,
|
||||
"cookie": "",
|
||||
"proxy": None,
|
||||
@@ -32,7 +28,6 @@ class Settings:
|
||||
"folder_mode": False,
|
||||
"download_record": True,
|
||||
"language": "zh_CN",
|
||||
# "server": False,
|
||||
}
|
||||
encode = "UTF-8-SIG" if system() == "Windows" else "UTF-8"
|
||||
|
||||
|
||||
@@ -15,24 +15,11 @@ USERSCRIPT = "https://raw.githubusercontent.com/JoeanAmier/XHS-Downloader/master
|
||||
|
||||
USERAGENT = ('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 '
|
||||
'Safari/537.36 Edg/128.0.0.0')
|
||||
SEC_CH_UA = '"Chromium";v="128", "Not;A=Brand";v="24", "Microsoft Edge";v="128"'
|
||||
SEC_CH_UA_PLATFORM = '"Windows"'
|
||||
|
||||
HEADERS = {
|
||||
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
|
||||
# 'accept-language': 'zh-CN,zh;q=0.9',
|
||||
'cache-control': 'no-cache',
|
||||
# 'dnt': '1',
|
||||
'pragma': 'no-cache',
|
||||
# 'priority': 'u=0, i',
|
||||
# 'sec-ch-ua': SEC_CH_UA,
|
||||
# 'sec-ch-ua-mobile': '?0',
|
||||
# 'sec-ch-ua-platform': SEC_CH_UA_PLATFORM,
|
||||
# 'sec-fetch-dest': 'document',
|
||||
# 'sec-fetch-mode': 'navigate',
|
||||
# 'sec-fetch-site': 'none',
|
||||
# 'sec-fetch-user': '?1',
|
||||
# 'upgrade-insecure-requests': '1',
|
||||
'user-agent': USERAGENT,
|
||||
}
|
||||
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
from gettext import translation
|
||||
|
||||
from ..module import ROOT
|
||||
|
||||
__all__ = ["Translate"]
|
||||
|
||||
|
||||
class Translate:
|
||||
SUPPORT = {
|
||||
"zh_CN",
|
||||
"en_US",
|
||||
}
|
||||
|
||||
def __init__(self, language: str):
|
||||
self.language = self.__check_language(language)
|
||||
self.translate = translation(
|
||||
"xhs",
|
||||
localedir=ROOT.joinpath("locale"),
|
||||
languages=[self.language],
|
||||
fallback=True,
|
||||
)
|
||||
|
||||
def __check_language(self, language: str) -> str:
|
||||
return language if language in self.SUPPORT else "zh_CN"
|
||||
|
||||
def message(self):
|
||||
return self.translate.gettext
|
||||
Reference in New Issue
Block a user