Skip to content

Commit

Permalink
Support recursively processing.
Browse files Browse the repository at this point in the history
  • Loading branch information
ch010060 committed Sep 20, 2020
1 parent baef701 commit c5dfbc2
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 19 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# DLsite_Renamer
![範例圖片](https://i.imgur.com/BSOwho9.png)
![使用者介面](https://i.imgur.com/L3AMWfp.png)

### Purpose
Rename the DLsite works and download the cover If required.
Expand Down Expand Up @@ -86,13 +86,14 @@ VJ開頭 => 遊戲 作品

### (Optional) CLI version without GUI and loop:
```
usage: dlsite_renamer-cli.py [-h] [-d] [-c] -i PATH
usage: dlsite_renamer-cli.py [-h] [-d] [-c] [-r] -i PATH
Renamer for DLsite works v3.0
optional arguments:
-h, --help show this help message and exit
-d, --DEL delete string in 【】
-c, --COVER download cover
-r, --RECUR recursively processing
-i PATH, --PATH PATH path for processing
```
Binary file not shown.
Binary file not shown.
25 changes: 16 additions & 9 deletions dlsite_renamer-cli.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from tkinter import filedialog
from tkinter import messagebox
from lxml import html
from glob import glob
import tkinter as tk
import threading
import requests
Expand Down Expand Up @@ -30,7 +31,7 @@

# re.compile()返回一個匹配對像
# ensure path name is exactly RJ\d\d\d\d\d\d or BJ\d\d\d\d\d\d or VJ\d\d\d\d\d\d
pattern = re.compile("[BbRrVv][EeJj]\d{6}|$")
pattern = re.compile("[BRV][EJ]\d{6}|$")
# filter to substitute illegal filenanme characters to " "
filter = re.compile('[\\\/:"*?<>|]+')

Expand Down Expand Up @@ -165,11 +166,16 @@ def match_code(code):
print(" 請檢查網絡連接\n")
return "", "", "", "", [], [], "", "", ""

def nameChange(path, del_flag, cover_flag):
def nameChange(path, del_flag, cover_flag, recur_flag):
print("選擇路徑: " + path + "\n")
# os.listdir()返回指定的資料夾包含的檔案或資料夾的名字的列表
files = os.listdir(path)
if recur_flag: # 遞迴檢索
files = [y for x in os.walk(path) for y in glob(os.path.join(x[0], '*'))]
else: # 根目錄檢索
files = os.listdir(path)
for file in files:
if recur_flag: # 遞迴檢索需要修正路徑
path = os.path.split(file)[0]
# 嘗試獲取code
code = re.findall(pattern, file.upper())[0]
# 如果沒能提取到code
Expand Down Expand Up @@ -233,15 +239,15 @@ def nameChange(path, del_flag, cover_flag):

# 1. 將Windows文件名中的非法字元替換成全形
# re.match(pattern, string, flags=0)
fixed_filenmae = "";
fixed_filename = "";
for char in new_name:
if re.match(filter, char):
fixed_filenmae += strB2Q(char)
fixed_filename += strB2Q(char)
else:
fixed_filenmae += char
fixed_filename += char

# 2. 多空格轉單空格
new_name = ' '.join(fixed_filenmae.split())
new_name = ' '.join(fixed_filename.split())

# 嘗試重命名
try:
Expand Down Expand Up @@ -279,9 +285,10 @@ def dir_path(path):
raise argparse.ArgumentTypeError(f"\"{path}\" is not a valid path!")

def process_command():
parser = argparse.ArgumentParser(description="Renamer for DLsite works v3.2")
parser = argparse.ArgumentParser(description="Renamer for DLsite works v3.3")
parser.add_argument('-d', "--DEL", action='store_true', help='delete string in 【】')
parser.add_argument('-c', "--COVER", action='store_true', help='download cover')
parser.add_argument('-r', "--RECUR", action='store_true', help='recursively processing')
parser.add_argument('-i', "--PATH", type=dir_path, required=True, help='path for processing')
return parser.parse_args()

Expand Down Expand Up @@ -344,4 +351,4 @@ def process_command():
print("**使用默認命名模板:\n")
print(" workno title \n")

nameChange(args.PATH,args.DEL,args.COVER)
nameChange(args.PATH,args.DEL,args.COVER,args.RECUR)
28 changes: 20 additions & 8 deletions dlsite_renamer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from tkinter import filedialog
from tkinter import messagebox
from lxml import html
from glob import glob
import tkinter as tk
import threading
import requests
Expand Down Expand Up @@ -28,7 +29,7 @@

# re.compile()返回一個匹配對像
# ensure path name is exactly RJ\d\d\d\d\d\d or BJ\d\d\d\d\d\d or VJ\d\d\d\d\d\d
pattern = re.compile("[BbRrVv][EeJj]\d{6}|$")
pattern = re.compile("[BRV][EJ]\d{6}|$")
# filter to substitute illegal filenanme characters to " "
filter = re.compile('[\\\/:"*?<>|]+')

Expand Down Expand Up @@ -171,12 +172,18 @@ def nameChange():
else:
cbtn_deltext.config(state=tk.DISABLED)
cbtn_dlcover.config(state=tk.DISABLED)
cbtn_recursive.config(state=tk.DISABLED)
btn.config(state=tk.DISABLED)
btn['text'] = "等待完成"
text.insert(tk.END, "選擇路徑: " + path + "\n")
# os.listdir()返回指定的資料夾包含的檔案或資料夾的名字的列表
files = os.listdir(path)
if recursive.get(): # 遞迴檢索
files = [y for x in os.walk(path) for y in glob(os.path.join(x[0], '*'))]
else: # 根目錄檢索
files = os.listdir(path)
for file in files:
if recursive.get(): # 遞迴檢索需要修正路徑
path = os.path.split(file)[0]
# 嘗試獲取code
code = re.findall(pattern, file.upper())[0]
# 如果沒能提取到code
Expand Down Expand Up @@ -240,15 +247,15 @@ def nameChange():

# 1. 將Windows文件名中的非法字元替換成全形
# re.match(pattern, string, flags=0)
fixed_filenmae = "";
fixed_filename = "";
for char in new_name:
if re.match(filter, char):
fixed_filenmae += strB2Q(char)
fixed_filename += strB2Q(char)
else:
fixed_filenmae += char
fixed_filename += char

# 2. 多空格轉單空格
new_name = ' '.join(fixed_filenmae.split())
new_name = ' '.join(fixed_filename.split())

# 嘗試重命名
try:
Expand Down Expand Up @@ -283,6 +290,7 @@ def nameChange():

cbtn_deltext.config(state=tk.NORMAL)
cbtn_dlcover.config(state=tk.NORMAL)
cbtn_recursive.config(state=tk.NORMAL)
btn.config(state=tk.NORMAL)
btn['text'] = "選擇路徑"

Expand All @@ -300,7 +308,7 @@ def thread_it(func, *args):


root = tk.Tk() # 實例化object,建立視窗root
root.title('DLsite重命名工具 v3.2') # 給視窗的標題取名字
root.title('DLsite重命名工具 v3.3') # 給視窗的標題取名字
root.eval('tk::PlaceWindow . center')
root.geometry('350x450') # 設定視窗的大小(橫向 * 縱向)

Expand Down Expand Up @@ -365,15 +373,19 @@ def thread_it(func, *args):

deltext = tk.IntVar() # 定義整數變數用來存放選擇行為返回值
dlcover = tk.IntVar()
recursive = tk.IntVar()
cbtn_deltext = tk.Checkbutton(root, text='去除title中【】之間的內容', variable=deltext,
onvalue=1, offvalue=0) # 傳值原理類似於radiobutton物件
cbtn_dlcover = tk.Checkbutton(root, text='下載封面', variable=dlcover,
onvalue=1, offvalue=0)
onvalue=1, offvalue=0)
cbtn_recursive = tk.Checkbutton(root, text='遞迴檢索', variable=recursive,
onvalue=1, offvalue=0)

btn = tk.Button(root, text='選擇路徑', command=lambda: thread_it(nameChange))

btn.pack()
cbtn_deltext.pack()
cbtn_dlcover.pack()
cbtn_recursive.pack()

root.mainloop()

0 comments on commit c5dfbc2

Please sign in to comment.