mirror of
https://github.com/JoeanAmier/XHS-Downloader.git
synced 2026-03-22 06:57:16 +08:00
style: 代码格式化和字符串处理优化
- 优化代码缩进和换行,提高可读性 - 统一使用单引号或双引号,保持一致性 - 移除冗余的空格和括号,精简代码
This commit is contained in:
@@ -38,25 +38,44 @@ class BrowserCookie:
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def run(cls, domains: list[str], console: Console = None, ) -> str:
|
||||
def run(
|
||||
cls,
|
||||
domains: list[str],
|
||||
console: Console = None,
|
||||
) -> str:
|
||||
console = console or Console()
|
||||
options = "\n".join(f"{i}. {k}: {v[1]}" for i, (k, v) in enumerate(cls.SUPPORT_BROWSER.items(), start=1))
|
||||
options = "\n".join(
|
||||
f"{i}. {k}: {v[1]}"
|
||||
for i, (k, v) in enumerate(cls.SUPPORT_BROWSER.items(), start=1)
|
||||
)
|
||||
if browser := console.input(
|
||||
_("读取指定浏览器的 Cookie 并写入配置文件\n"
|
||||
"Windows 系统需要以管理员身份运行程序才能读取 Chromium、Chrome、Edge 浏览器 Cookie!\n"
|
||||
"{options}\n请输入浏览器名称或序号:").format(options=options), ):
|
||||
return cls.get(browser, domains, console, )
|
||||
_(
|
||||
"读取指定浏览器的 Cookie 并写入配置文件\n"
|
||||
"Windows 系统需要以管理员身份运行程序才能读取 Chromium、Chrome、Edge 浏览器 Cookie!\n"
|
||||
"{options}\n请输入浏览器名称或序号:"
|
||||
).format(options=options),
|
||||
):
|
||||
return cls.get(
|
||||
browser,
|
||||
domains,
|
||||
console,
|
||||
)
|
||||
console.print(_("未选择浏览器!"))
|
||||
|
||||
@classmethod
|
||||
def get(cls, browser: str | int, domains: list[str], console: Console = None, ) -> str:
|
||||
def get(
|
||||
cls,
|
||||
browser: str | int,
|
||||
domains: list[str],
|
||||
console: Console = None,
|
||||
) -> str:
|
||||
console = console or Console()
|
||||
if not (browser := cls.__browser_object(browser)):
|
||||
console.print(_("浏览器名称或序号输入错误!"))
|
||||
return ""
|
||||
try:
|
||||
cookies = browser(domains=domains)
|
||||
return "; ".join(f"{i["name"]}={i["value"]}" for i in cookies)
|
||||
return "; ".join(f"{i['name']}={i['value']}" for i in cookies)
|
||||
except RuntimeError:
|
||||
console.print(_("获取 Cookie 失败,未找到 Cookie 数据!"))
|
||||
return ""
|
||||
|
||||
@@ -30,7 +30,7 @@ class Cleaner:
|
||||
"|": "",
|
||||
"<": "",
|
||||
">": "",
|
||||
"\"": "",
|
||||
'"': "",
|
||||
"?": "",
|
||||
":": "",
|
||||
"*": "",
|
||||
@@ -80,7 +80,10 @@ class Cleaner:
|
||||
|
||||
text = self.filter(text)
|
||||
|
||||
text = replace_emoji(text, replace, )
|
||||
text = replace_emoji(
|
||||
text,
|
||||
replace,
|
||||
)
|
||||
|
||||
text = self.clear_spaces(text)
|
||||
|
||||
@@ -94,9 +97,16 @@ class Cleaner:
|
||||
return " ".join(string.split())
|
||||
|
||||
@classmethod
|
||||
def remove_control_characters(cls, text, replace="", ):
|
||||
def remove_control_characters(
|
||||
cls,
|
||||
text,
|
||||
replace="",
|
||||
):
|
||||
# 使用正则表达式匹配所有控制字符
|
||||
return cls.CONTROL_CHARACTERS.sub(replace, text, )
|
||||
return cls.CONTROL_CHARACTERS.sub(
|
||||
replace,
|
||||
text,
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@@ -16,9 +16,7 @@ class Converter:
|
||||
)
|
||||
|
||||
def run(self, content: str) -> dict:
|
||||
return self._filter_object(
|
||||
self._convert_object(
|
||||
self._extract_object(content)))
|
||||
return self._filter_object(self._convert_object(self._extract_object(content)))
|
||||
|
||||
def _extract_object(self, html: str) -> str:
|
||||
if not html:
|
||||
|
||||
@@ -15,7 +15,9 @@ def remove_empty_directories(path: Path) -> None:
|
||||
"\\_",
|
||||
"\\__",
|
||||
}
|
||||
for dir_path, dir_names, file_names in path.walk(top_down=False, ):
|
||||
for dir_path, dir_names, file_names in path.walk(
|
||||
top_down=False,
|
||||
):
|
||||
if any(i in str(dir_path) for i in exclude):
|
||||
continue
|
||||
if not dir_names and not file_names:
|
||||
|
||||
@@ -14,7 +14,8 @@ class Namespace:
|
||||
def depth_conversion(element):
|
||||
if isinstance(element, dict):
|
||||
return SimpleNamespace(
|
||||
**{k: depth_conversion(v) for k, v in element.items()})
|
||||
**{k: depth_conversion(v) for k, v in element.items()}
|
||||
)
|
||||
elif isinstance(element, list):
|
||||
return [depth_conversion(item) for item in element]
|
||||
else:
|
||||
@@ -25,14 +26,16 @@ class Namespace:
|
||||
def safe_extract(
|
||||
self,
|
||||
attribute_chain: str,
|
||||
default: Union[str, int, list, dict, SimpleNamespace] = ""):
|
||||
default: Union[str, int, list, dict, SimpleNamespace] = "",
|
||||
):
|
||||
return self.__safe_extract(self.data, attribute_chain, default)
|
||||
|
||||
@staticmethod
|
||||
def __safe_extract(
|
||||
data_object: SimpleNamespace,
|
||||
attribute_chain: str,
|
||||
default: Union[str, int, list, dict, SimpleNamespace] = "", ):
|
||||
default: Union[str, int, list, dict, SimpleNamespace] = "",
|
||||
):
|
||||
data = deepcopy(data_object)
|
||||
attributes = attribute_chain.split(".")
|
||||
for attribute in attributes:
|
||||
@@ -61,7 +64,8 @@ class Namespace:
|
||||
return cls.__safe_extract(
|
||||
data_object,
|
||||
attribute_chain,
|
||||
default, )
|
||||
default,
|
||||
)
|
||||
|
||||
@property
|
||||
def __dict__(self):
|
||||
@@ -70,10 +74,11 @@ class Namespace:
|
||||
@classmethod
|
||||
def convert_to_dict(cls, data) -> dict:
|
||||
return {
|
||||
key: cls.convert_to_dict(value) if isinstance(
|
||||
value,
|
||||
SimpleNamespace) else value for key,
|
||||
value in vars(data).items()}
|
||||
key: cls.convert_to_dict(value)
|
||||
if isinstance(value, SimpleNamespace)
|
||||
else value
|
||||
for key, value in vars(data).items()
|
||||
}
|
||||
|
||||
def __bool__(self):
|
||||
return bool(vars(self.data))
|
||||
|
||||
@@ -2,7 +2,7 @@ from unicodedata import name
|
||||
|
||||
|
||||
def is_chinese_char(char: str) -> bool:
|
||||
return 'CJK' in name(char, "")
|
||||
return "CJK" in name(char, "")
|
||||
|
||||
|
||||
def truncate_string(s: str, length: int = 64) -> str:
|
||||
|
||||
Reference in New Issue
Block a user