Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

对 Episode 新增自定义字符串模板 #105

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Comic.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def __init__(self, comic_id: int, mainGUI: MainGUI) -> None:
"cookie": f"SESSDATA={mainGUI.getConfig('cookie')}",
}
self.payload = {"comic_id": self.comic_id}
self.episode_template_name = "${ord}-${title}"

############################################################
def getComicInfo(self) -> dict:
Expand Down Expand Up @@ -89,6 +90,7 @@ def _() -> dict:
self.data["author_name"] = self.data["author_name"].replace("作者:", "").replace("出品:", "")
self.data["author_name"] = myStrFilter(self.data["author_name"])
self.data["styles"] = ",".join(self.data["styles"])
self.data["episode_template_name"] = self.episode_template_name
if self.comic_id in self.mainGUI.my_library:
self.data["save_path"] = self.mainGUI.my_library[self.comic_id].get("comic_path")
else:
Expand Down
17 changes: 11 additions & 6 deletions src/Episode.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
__app_name__,
__copyright__,
__version__,
fileNameParser,
isCheckSumValid,
logger,
myStrFilter,
Expand Down Expand Up @@ -57,15 +58,10 @@ def __init__(
self.imgs_token = None
self.author = comic_info["author_name"]
self.save_method = mainGUI.getConfig("save_method")

self.episode_template_name = comic_info["episode_template_name"]
if self.save_method == "Cbz压缩包":
self.comicinfoxml = ComicInfoXML(comic_info, episode)

# ?###########################################################
# ? 修复标题中的特殊字符
episode["short_title"] = myStrFilter(episode["short_title"])
episode["title"] = myStrFilter(episode["title"])

# ?###########################################################
# ? 修复重复标题
if episode["short_title"] == episode["title"] or episode["title"] == "":
Expand All @@ -91,6 +87,15 @@ def __init__(
"referer": f"https://manga.bilibili.com/detail/mc{comic_id}/{self.id}?from=manga_homepage",
"cookie": f"SESSDATA={mainGUI.getConfig('cookie')}",
}
info_dict = dict()
info_dict["comic_id"] = comic_id
info_dict["ep_id"] = self.id
info_dict["ord"] = self.ord
info_dict["title"] = self.title
info_dict["size"] = self.size
info_dict["available"] = self.available
info_dict["author"] = comic_info["author_name"]
self.title = fileNameParser(self.episode_template_name, info_dict)
self.save_path = comic_info["save_path"]
self.epi_path = os.path.join(self.save_path, f"{self.title}")

Expand Down
53 changes: 52 additions & 1 deletion src/Utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,62 @@
s = re.sub(r"\|", "丨", s)
s = re.sub(r"\s+$", "", s)
s = re.sub(r"^\s+", " ", s)
s = re.sub(r"\.", "·", s)

return s


def fileNameParser(template: str, info: dict) -> str:
"""根据模板和元数据生成文件名

Args:
template (str): 模板 模板样式 ${variable[ ?? fallback][ then function_name([arg])]} variable: 变量名 fallback: 变量不存在时的默认值 function_name: 函数名 arg: 函数参数
info (dict): 元数据

Returns:
str: 文件名
"""

result = re.sub(r"(?<!\$)\$\{(?P<variable>\w+)(?:\s+\?\?\s+(?P<fallback>\d+(?:\.\d+)|\w+|\".*?\"))?(?:(?<!\?\?)\s+then\s+(?P<function_name>padEnd|padStart)\((?P<arg>\d+)?\))?\}",

Check notice on line 125 in src/Utils.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/Utils.py#L125

line too long (182 > 159 characters) (E501)
lambda m: __inner__fileNameParser(m, info), template)
# ? 过滤非法字符
result = myStrFilter(result)
return result


def __inner__fileNameParser(match: re.Match, info: dict) -> str:
"""根据模板和元数据生成文件名

Args:
match (re.Match): 正则匹配结果
info (dict): 元数据

Returns:
str: 文件名
"""
match_dict = match.groupdict()
variable = match_dict.get("variable")
fallback = match_dict.get("fallback")
function_name = match_dict.get("function_name")
arg = match_dict.get("arg")
args = str(arg).split(",") if arg is not None else []
value = ""
if variable not in info:
if fallback is None:
return match.group(0)
else:
value = fallback
else:
value = str(info[variable])
if function_name is None:
return value
else:
if function_name == "padEnd":
return value.ljust(int(args[0] if args[0] is not None else 0), "0")
elif function_name == "padStart":
return value.rjust(int(args[0] if args[0] is not None else 0), "0")
else:
return match.group(0)

############################################################


Expand Down