代码拉取完成,页面将自动刷新
#!/opt/QPython311/bin/python3
# -*- coding: utf-8 -*-
import argparse
import os
import re
import sys
EPISODE_PATTERNS = [
r"第(\d+)集",
r"\[(\d+)集\]",
r"#(\d+)\s",
r"\[(\d{1,4})\]",
r"(\d+)v2",
r"-(\d+)\.",
r"- (\d+) \[",
r"\.(\d+)\.",
r"第(\d+)話",
r"第(\d+)話",
r"S\d+E(\d+)",
r" (\d+) ",
]
SEASON_PATTERNS = [
re.compile(r"第(\d+)季"),
re.compile(r"\.S(\d+)-"),
re.compile(r" S(\d+) "),
re.compile(r" Season(\d+) "),
re.compile(r" (\d)nd "),
]
def get_season(name: str):
name = name.replace("第二季", "第2季")
name = name.replace("第三季", "第3季")
name = name.replace("第四季", "第4季")
name = name.replace("第五季", "第5季")
name = name.replace("第六季", "第6季")
name = name.replace("第七季", "第7季")
name = name.replace("第八季", "第8季")
name = name.replace("第九季", "第9季")
name = name.replace("第十季", "第10季")
s = "1"
for ptn in SEASON_PATTERNS:
match = ptn.search(name)
if match:
s = match.groups()[0]
if len(s) == 1:
s = "S0" + s
return s
def process_subtitle_name(name: str):
"""
处理字幕
"""
root, ext = os.path.splitext(name)
if ext in {".srt", ".smi", ".ssa", ".ass"}:
return root + ".zh" + ext
return name
class RenameABC:
def get_name(self):
pass
def rename(self, path):
if ".@__thumb" in path:
return
if "DVD_MENU" in path:
return
new_name = self.get_name(os.path.basename(path))
if new_name is None:
print(path, "无法获取新的名字。")
return
new_path = os.path.join(os.path.dirname(path), new_name)
if os.path.exists(new_path):
return
os.rename(path, new_path)
print(path, "->", new_path)
class AutoRename(RenameABC):
"""自动重命名
内置目标字符串的正则
"""
def __init__(self):
self.ptns = [Matcher(p) for p in EPISODE_PATTERNS]
def get_name(self, name):
season = get_season(name)
for p in self.ptns:
new = p.get_name(name)
if new:
# return season + "E" + new
return process_subtitle_name(season + "E" + new)
skip_strs = [
"OP",
"OVA",
"ED",
"SP",
"CM",
]
class Matcher(RenameABC):
def __init__(self, src):
if "(" not in src or ")" not in src:
raise ValueError("必须使用分组")
self.ptn = re.compile(src)
def get_name(self, name):
root, ext = os.path.splitext(name)
for item in self.ptn.finditer(root):
return process_subtitle_name(item.groups()[0] + ext)
class ReplaceText(RenameABC):
"""不需要处理字幕"""
def __init__(self, src, new) -> None:
self.ptn = re.compile(src)
self.new = new
def get_name(self, name):
root, ext = os.path.splitext(name)
# return re.sub(self.src, self.new, root) + ext
return self.ptn.sub(self.new, root) + ext
def rename(args):
path = args.path
target = args.target
new_str = args.new
is_debug = args.debug
if not os.path.exists(path):
print(path, "文件夹不存在")
return
if args.clear:
new_str = ""
obj = None
if target is None:
obj = AutoRename()
elif new_str or new_str == "":
obj = ReplaceText(target, new_str)
else:
obj = Matcher(target)
if os.path.isfile(path):
if is_debug:
new_name = obj.get_name(path)
if new_name is None:
new_name = "None"
print(path + " -> " + new_name)
else:
obj.rename(path)
return
for root, _, files in os.walk(path):
for f in files:
# 不处理繁体字幕
sss = f[-12:].lower()
if ".tc." in sss:
continue
if "cht" in sss:
continue
_path = os.path.join(root, f)
if ".@__thumb" in _path:
continue
if is_debug:
new_name = obj.get_name(_path)
if new_name is None:
new_name = "None"
if _path == new_name:
continue
print(_path + " -> " + new_name)
else:
obj.rename(_path)
def qb(args):
"""qTorrent
下载完成后
1. 重命名文件
2. 重命名文件夹(TODO Metadata 需要数据)
- 获取文件动画名
- 获取 IMDB 真实的名字,中文名
- 重命名
3. 删除无关文件
4. 移动文件夹到指定目录
Args:
args (_type_): _description_
"""
path = args.file
root = args.directory
ar = AutoRename()
if os.path.isfile(path):
ar.rename(path)
return
if not os.path.isdir(path):
return
for root, _, files in os.walk(path):
for f in files:
_path = os.path.join(root, f)
ar.rename(_path)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="NAS Rename Tool")
subparsers = parser.add_subparsers(help="子命令")
parser_a = subparsers.add_parser(
"qb",
help="qBittorrent 自动重命名",
usage='python nrt.py qb -f "%%F" -d "%%D"',
)
parser_a.add_argument("-f", "--file", help="下载路径")
parser_a.add_argument("-d", "--directory", help="保存目录")
parser_a.add_argument("-t", "--t", help="移动文件夹到目标目录")
parser_b = subparsers.add_parser(
"rename",
help="重命名文件",
usage="""
python nrt.py rename -t "\d+" -n one 123.txt
python nrt.py rename -t ^ -n S01E 12.txt
python nrt.py rename -t $ -n .[1080P] 12.txt
""",
)
parser_b.add_argument("path", type=str, help="目录")
parser_b.add_argument(
"-t",
"--target",
help="目标字符串,支持正则匹配,默认命中后,删除除它以外的字符。注意,正则需要双引号。(可以单独使用。)",
)
parser_b.add_argument(
"-n",
"--new",
required=False,
help="新字符串,支持正则,开启这个选项后,进入替换模式,使用新的字符串替换目标字符串。(必须和-t参数一起使用。)",
)
parser_b.add_argument(
"-c",
"--clear",
action="store_true",
help="清楚目标字符串。(必须和-t参数一起使用。)",
)
parser_b.add_argument(
"-d",
"--debug",
action="store_true",
help="测试模式,不会修改文件名。",
)
if len(sys.argv) <= 1:
parser.print_help()
exit(1)
args = parser.parse_args()
cmd = sys.argv[1]
if cmd == "qb":
if len(sys.argv) == 2:
parser_a.print_help()
exit(1)
qb(parser.parse_args())
elif cmd == "rename":
rename(parser.parse_args())
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。