mirror of
https://github.com/JoeanAmier/XHS-Downloader.git
synced 2026-03-22 15:07:17 +08:00
style: 代码格式化和字符串处理优化
- 优化代码缩进和换行,提高可读性 - 统一使用单引号或双引号,保持一致性 - 移除冗余的空格和括号,精简代码
This commit is contained in:
@@ -72,10 +72,10 @@ class Manager:
|
||||
self.path = self.__check_path(path)
|
||||
self.folder = self.__check_folder(folder)
|
||||
self.blank_headers = HEADERS | {
|
||||
'user-agent': user_agent or USERAGENT,
|
||||
"user-agent": user_agent or USERAGENT,
|
||||
}
|
||||
self.headers = self.blank_headers | {
|
||||
'cookie': cookie,
|
||||
"cookie": cookie,
|
||||
}
|
||||
self.retry = retry
|
||||
self.chunk = chunk
|
||||
@@ -86,10 +86,13 @@ class Manager:
|
||||
self.download_record = self.check_bool(download_record, True)
|
||||
self.proxy_tip = None
|
||||
self.proxy = self.__check_proxy(proxy)
|
||||
self.print_proxy_tip(_print, )
|
||||
self.print_proxy_tip(
|
||||
_print,
|
||||
)
|
||||
self.request_client = AsyncClient(
|
||||
headers=self.headers | {
|
||||
'referer': 'https://www.xiaohongshu.com/',
|
||||
headers=self.headers
|
||||
| {
|
||||
"referer": "https://www.xiaohongshu.com/",
|
||||
},
|
||||
timeout=timeout,
|
||||
verify=False,
|
||||
@@ -177,11 +180,7 @@ class Manager:
|
||||
def __check_name_format(self, format_: str) -> str:
|
||||
keys = format_.split()
|
||||
return next(
|
||||
(
|
||||
"发布时间 作者昵称 作品标题"
|
||||
for key in keys
|
||||
if key not in self.NAME_KEYS
|
||||
),
|
||||
("发布时间 作者昵称 作品标题" for key in keys if key not in self.NAME_KEYS),
|
||||
format_,
|
||||
)
|
||||
|
||||
@@ -198,7 +197,7 @@ class Manager:
|
||||
timeout=10,
|
||||
headers={
|
||||
"User-Agent": USERAGENT,
|
||||
}
|
||||
},
|
||||
)
|
||||
response.raise_for_status()
|
||||
self.proxy_tip = (_("代理 {0} 测试成功").format(proxy),)
|
||||
@@ -220,7 +219,11 @@ class Manager:
|
||||
WARNING,
|
||||
)
|
||||
|
||||
def print_proxy_tip(self, _print: bool = True, log=None, ) -> None:
|
||||
def print_proxy_tip(
|
||||
self,
|
||||
_print: bool = True,
|
||||
log=None,
|
||||
) -> None:
|
||||
if _print and self.proxy_tip:
|
||||
logging(log, *self.proxy_tip)
|
||||
|
||||
@@ -240,6 +243,6 @@ class Manager:
|
||||
# 使用空字符串替换匹配到的部分
|
||||
cookie_string = sub(pattern, "", cookie_string)
|
||||
# 去除多余的分号和空格
|
||||
cookie_string = sub(r';\s*$', "", cookie_string) # 删除末尾的分号和空格
|
||||
cookie_string = sub(r';\s*;', ";", cookie_string) # 删除中间多余分号后的空格
|
||||
return cookie_string.strip('; ')
|
||||
cookie_string = sub(r";\s*$", "", cookie_string) # 删除末尾的分号和空格
|
||||
cookie_string = sub(r";\s*;", ";", cookie_string) # 删除中间多余分号后的空格
|
||||
return cookie_string.strip("; ")
|
||||
|
||||
@@ -5,7 +5,10 @@ from aiosqlite import connect
|
||||
|
||||
from ..module import Manager
|
||||
|
||||
__all__ = ["IDRecorder", "DataRecorder", ]
|
||||
__all__ = [
|
||||
"IDRecorder",
|
||||
"DataRecorder",
|
||||
]
|
||||
|
||||
|
||||
class IDRecorder:
|
||||
@@ -18,7 +21,9 @@ class IDRecorder:
|
||||
async def _connect_database(self):
|
||||
self.database = await connect(self.file)
|
||||
self.cursor = await self.database.cursor()
|
||||
await self.database.execute("CREATE TABLE IF NOT EXISTS explore_id (ID TEXT PRIMARY KEY);")
|
||||
await self.database.execute(
|
||||
"CREATE TABLE IF NOT EXISTS explore_id (ID TEXT PRIMARY KEY);"
|
||||
)
|
||||
await self.database.commit()
|
||||
|
||||
async def select(self, id_: str):
|
||||
@@ -95,11 +100,14 @@ class DataRecorder(IDRecorder):
|
||||
|
||||
async def add(self, **kwargs) -> None:
|
||||
if self.switch:
|
||||
await self.database.execute(f"""REPLACE INTO explore_data (
|
||||
await self.database.execute(
|
||||
f"""REPLACE INTO explore_data (
|
||||
{", ".join(i[0] for i in self.DATA_TABLE)}
|
||||
) VALUES (
|
||||
{", ".join("?" for _ in kwargs)}
|
||||
);""", self.__generate_values(kwargs))
|
||||
);""",
|
||||
self.__generate_values(kwargs),
|
||||
)
|
||||
await self.database.commit()
|
||||
|
||||
async def __delete(self, id_: str) -> None:
|
||||
|
||||
@@ -6,7 +6,7 @@ from platform import system
|
||||
from .static import ROOT
|
||||
from .static import USERAGENT
|
||||
|
||||
__all__ = ['Settings']
|
||||
__all__ = ["Settings"]
|
||||
|
||||
|
||||
class Settings:
|
||||
|
||||
@@ -3,10 +3,11 @@ from pathlib import Path
|
||||
VERSION_MAJOR = 2
|
||||
VERSION_MINOR = 5
|
||||
VERSION_BETA = True
|
||||
__version__ = f"{VERSION_MAJOR}.{VERSION_MINOR}.{"beta" if VERSION_BETA else "stable"}"
|
||||
__version__ = f"{VERSION_MAJOR}.{VERSION_MINOR}.{'beta' if VERSION_BETA else 'stable'}"
|
||||
ROOT = Path(__file__).resolve().parent.parent.parent
|
||||
PROJECT = f"XHS-Downloader V{VERSION_MAJOR}.{
|
||||
VERSION_MINOR} {"Beta" if VERSION_BETA else "Stable"}"
|
||||
PROJECT = f"XHS-Downloader V{VERSION_MAJOR}.{VERSION_MINOR} {
|
||||
'Beta' if VERSION_BETA else 'Stable'
|
||||
}"
|
||||
|
||||
REPOSITORY = "https://github.com/JoeanAmier/XHS-Downloader"
|
||||
LICENCE = "GNU General Public License v3.0"
|
||||
@@ -14,8 +15,10 @@ RELEASES = "https://github.com/JoeanAmier/XHS-Downloader/releases/latest"
|
||||
|
||||
USERSCRIPT = "https://raw.githubusercontent.com/JoeanAmier/XHS-Downloader/master/static/XHS-Downloader.js"
|
||||
|
||||
USERAGENT = ("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 "
|
||||
"Safari/537.36")
|
||||
USERAGENT = (
|
||||
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 "
|
||||
"Safari/537.36"
|
||||
)
|
||||
|
||||
HEADERS = {
|
||||
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,"
|
||||
@@ -32,26 +35,35 @@ ERROR = "b bright_red"
|
||||
WARNING = "b bright_yellow"
|
||||
INFO = "b bright_green"
|
||||
|
||||
FILE_SIGNATURES: tuple[tuple[int, bytes, str,], ...] = (
|
||||
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", "jpeg"),
|
||||
(0, b"\x89\x50\x4E\x47\x0D\x0A\x1A\x0A", "png"),
|
||||
(0, b"\xff\xd8\xff", "jpeg"),
|
||||
(0, b"\x89\x50\x4e\x47\x0d\x0a\x1a\x0a", "png"),
|
||||
(4, b"\x66\x74\x79\x70\x61\x76\x69\x66", "avif"),
|
||||
(4, b"\x66\x74\x79\x70\x68\x65\x69\x63", "heic"),
|
||||
(8, b"\x57\x45\x42\x50", "webp"),
|
||||
(4, b"\x66\x74\x79\x70\x4D\x53\x4E\x56", "mp4"),
|
||||
(4, b"\x66\x74\x79\x70\x69\x73\x6F\x6D", "mp4"),
|
||||
(4, b"\x66\x74\x79\x70\x6D\x70\x34\x32", "m4v"),
|
||||
(4, b"\x66\x74\x79\x70\x4d\x53\x4e\x56", "mp4"),
|
||||
(4, b"\x66\x74\x79\x70\x69\x73\x6f\x6d", "mp4"),
|
||||
(4, b"\x66\x74\x79\x70\x6d\x70\x34\x32", "m4v"),
|
||||
(4, b"\x66\x74\x79\x70\x71\x74\x20\x20", "mov"),
|
||||
(0, b"\x1A\x45\xDF\xA3", "mkv"),
|
||||
(0, b"\x00\x00\x01\xB3", "mpg"),
|
||||
(0, b"\x00\x00\x01\xBA", "mpg"),
|
||||
(0, b"\x1a\x45\xdf\xa3", "mkv"),
|
||||
(0, b"\x00\x00\x01\xb3", "mpg"),
|
||||
(0, b"\x00\x00\x01\xba", "mpg"),
|
||||
(0, b"\x46\x4c\x56\x01", "flv"),
|
||||
(8, b"\x41\x56\x49\x20", "avi"),
|
||||
)
|
||||
FILE_SIGNATURES_LENGTH = max(offset + len(signature) for offset, signature, _ in FILE_SIGNATURES)
|
||||
FILE_SIGNATURES_LENGTH = max(
|
||||
offset + len(signature) for offset, signature, _ in FILE_SIGNATURES
|
||||
)
|
||||
|
||||
MAX_WORKERS: int = 4
|
||||
|
||||
|
||||
@@ -22,7 +22,10 @@ def retry(function):
|
||||
def logging(log, text, style=INFO):
|
||||
string = Text(text, style=style)
|
||||
if log:
|
||||
log.write(string, scroll_end=True, )
|
||||
log.write(
|
||||
string,
|
||||
scroll_end=True,
|
||||
)
|
||||
else:
|
||||
print(string)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user