Skip to content
This repository has been archived by the owner on Feb 9, 2022. It is now read-only.

Commit

Permalink
添加自定义替换规则
Browse files Browse the repository at this point in the history
  • Loading branch information
yodhcn committed Sep 13, 2020
1 parent c975898 commit 5938266
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,29 @@

重命名后:[Hypnotic_Yanh] 哀しみのイき人形《催眠音声・男女版同梱》 (RJ149268)

**自定义替换规则**: 请修改 "config.json" 中的 "replace_rules", 例如

```json
"replace_rules": [
{
"from": ":",
"to": ""
},
{
"from": "?",
"to": ""
},
{
"from": "/",
"to": ""
},
{
"from": "\"",
"to": ""
}
],
```

### 注意:

如需在自定义模板包含 "(cv)", 请将其放在模板字符串的末尾
Expand Down
Binary file not shown.
16 changes: 13 additions & 3 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

# 默认配置
template = 'workno [circle] title (cv)' # 默认命名模板
replace_rules = [] # 替换规则

RJ_WEBPATH = 'https://www.dlsite.com/maniax/work/=/product_id/'
RT_WEBPATH = 'https://www.dlsite.com.tw/work/product_id/'
Expand Down Expand Up @@ -169,6 +170,12 @@ def nameChange():
else:
new_name = new_name.replace("(cv)", "")

# 自定义替换规则
for rule in replace_rules:
if 'from' in rule and 'to' in rule:
new_name = new_name.replace(
rule["from"], rule["to"])

# 将Windows文件名中的非法字符替换
# re.sub(pattern, repl, string)
new_name = re.sub(filter, " ", new_name)
Expand Down Expand Up @@ -227,7 +234,7 @@ def thread_it(func, *args):
basedir = os.path.abspath(os.path.dirname(__file__))
try:
fname = os.path.join(basedir, 'config.json')
with open(fname, 'r') as f:
with open(fname, 'r', encoding='utf-8') as f:
config = json.load(f)
if config["template"]: # 模板非空
if ("workno" in config["template"]):
Expand All @@ -242,10 +249,13 @@ def thread_it(func, *args):
text.insert(tk.END, "**使用默认命名模板:\n")
text.insert(tk.END, " workno [circle] title (cv)\n\n")

if config["replace_rules"] and type(config["replace_rules"]) == list and len(config["replace_rules"]):
replace_rules = config["replace_rules"]

except os.error as err:
# 生成配置文件
with open(fname, "w") as f:
json.dump({'template': ''}, f, sort_keys=True,
with open(fname, "w", encoding='utf-8') as f:
json.dump({'template': '', "replace_rules": []}, f, ensure_ascii=False, sort_keys=True,
indent=4, separators=(',', ': '))
text.insert(tk.END, "**使用默认命名模板:\n")
text.insert(tk.END, " workno [circle] title (cv)\n")
Expand Down

0 comments on commit 5938266

Please sign in to comment.