Skip to content

Commit

Permalink
Support downloading cover image if necessary.
Browse files Browse the repository at this point in the history
  • Loading branch information
ch010060 committed Sep 16, 2020
1 parent 1be0748 commit a67a713
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 23 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# DLsite_Renamer
![範例圖片](https://i.imgur.com/8EH3l16.png)

### Purpose
Rename the DLsite works and download the cover If required.

### How to use:
1. Download the binary [release pack](https://github.com/ch010060/DLsite_Renamer/releases) and run .exe or run python script by yourself.
2. There are two versions:
Expand Down Expand Up @@ -77,15 +80,17 @@ VJ開頭 => 遊戲 作品
1. config.json 文件使用 **UTF-8** 編碼, 請不要用 Windows 系統自帶的記事本進行編輯,推薦使用專業的**文件編輯器**,例如: [Notepad3](https://www.appinn.com/notepad3/), [Notepad++](https://notepad-plus-plus.org/), [vscode](https://code.visualstudio.com/)
![Notepad3](https://i.imgur.com/L73BXEZ.png)
2. 去除標題中【】之間的內容主要用於去掉不必要的標註,可多加利用~
3. 若封面(cover.jpg)已存在資料夾中,則會自動跳過下載

### (Optional) CLI version without GUI and loop:
```
usage: dlsite_renamer-cli.py [-h] [-d] -i PATH
usage: dlsite_renamer-cli.py [-h] [-d] [-c] -i PATH
Renamer for DLsite works
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
-i PATH, --PATH PATH path for processing
```
32 changes: 24 additions & 8 deletions dlsite_renamer-cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import random
import sys
import argparse
import urllib.request

# 默認設定
template_RJ = 'workno title ' # 默認RJ命名模板(Voice)
Expand Down Expand Up @@ -150,15 +151,16 @@ def match_code(work_code):
url = VJ_G_WEBPATH + work_code
r = s.get(url, allow_redirects=False, cookies=R_COOKIE)
if r.status_code != 200:
return r.status_code, "", "", [], [], "", ""
return r.status_code, "", "", "", [], [], "", ""
except os.error as err:
print("**請求超時!\n")
print(" 請檢查網絡連接\n")
return "", "", "", [], [], "", ""
return "", "", "", "", [], [], "", ""

# fromstring()在解析xml格式時, 將字串轉換為Element對像, 解析樹的根節點
# 在python中, 對get請求返回的r.content做fromstring()處理, 可以方便進行後續的xpath()定位等
tree = html.fromstring(r.content)
img_url = tree.xpath('//meta[@name="twitter:image:src"]/@content')[0]
title = tree.xpath('//a[@itemprop="url"]/text()')[0]
circle = tree.xpath(
'//span[@itemprop="brand" and @class="maker_name"]/*/text()')[0]
Expand All @@ -177,14 +179,14 @@ def match_code(work_code):
if len(release_date) >= 11:
release_date = release_date[2]+release_date[3]+release_date[5]+release_date[6]+release_date[8]+release_date[9]

return 200, title, circle, cvList, authorList, work_age[0], release_date
return 200, img_url, title, circle, cvList, authorList, work_age[0], release_date

except os.error as err:
print("**請求超時!\n")
print(" 請檢查網絡連接\n")
return "", "", "", [], [], "", ""
return "", "", "", "", [], [], "", ""

def nameChange(path, del_flag):
def nameChange(path, del_flag, cover_flag):
print("選擇路徑: " + path + "\n")
# os.listdir()返回指定的資料夾包含的檔案或資料夾的名字的列表
files = os.listdir(path)
Expand All @@ -204,7 +206,7 @@ def nameChange(path, del_flag):
else:
#print('Processing: ' + code)
print('Processing: ' + code + '\n')
r_status, title, circle, cvList, authorList, work_age, release_date = match_code(code)
r_status, img_url, title, circle, cvList, authorList, work_age, release_date = match_code(code)
# 如果順利爬取網頁訊息
if r_status == 200 and title and circle:
if del_flag:
Expand Down Expand Up @@ -239,6 +241,19 @@ def nameChange(path, del_flag):
else:
new_name = new_name.replace("(CV. cv)", "")


# 要下載封面且是資料夾
if cover_flag and img_url and os.path.isdir(os.path.join(path, file)):
try: # 嘗試下載封面
store_path = os.path.join(path, file, "cover.jpg")
if not os.path.isfile(store_path):
print(" 下載封面...\n")
urllib.request.urlretrieve(img_url, store_path)
else:
print("**封面已存在,跳過下載!\n")
except os.error as err:
print("**下載封面過程中出現錯誤!\n")

# 將Windows文件名中的非法字元替換
# re.sub(pattern, repl, string)
new_name = re.sub(filter, " ", new_name)
Expand Down Expand Up @@ -277,8 +292,9 @@ 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")
parser = argparse.ArgumentParser(description="Renamer for DLsite works v3.0")
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('-i', "--PATH", type=dir_path, required=True, help='path for processing')
return parser.parse_args()

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

nameChange(args.PATH,args.DEL)
nameChange(args.PATH,args.DEL,args.COVER)
47 changes: 34 additions & 13 deletions dlsite_renamer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import os
import json
import random
import urllib.request

# 默認設定
template_RJ = 'workno title ' # 默認RJ命名模板(Voice)
Expand Down Expand Up @@ -148,15 +149,16 @@ def match_code(work_code):
url = VJ_G_WEBPATH + work_code
r = s.get(url, allow_redirects=False, cookies=R_COOKIE)
if r.status_code != 200:
return r.status_code, "", "", [], [], "", ""
return r.status_code, "", "", "", [], [], "", ""
except os.error as err:
text.insert(tk.END, "**請求超時!\n")
text.insert(tk.END, " 請檢查網絡連接\n")
return "", "", "", [], [], "", ""
return "", "", "", "", [], [], "", ""

# fromstring()在解析xml格式時, 將字串轉換為Element對像, 解析樹的根節點
# 在python中, 對get請求返回的r.content做fromstring()處理, 可以方便進行後續的xpath()定位等
tree = html.fromstring(r.content)
img_url = tree.xpath('//meta[@name="twitter:image:src"]/@content')[0]
title = tree.xpath('//a[@itemprop="url"]/text()')[0]
circle = tree.xpath(
'//span[@itemprop="brand" and @class="maker_name"]/*/text()')[0]
Expand All @@ -175,20 +177,21 @@ def match_code(work_code):
if len(release_date) >= 11:
release_date = release_date[2]+release_date[3]+release_date[5]+release_date[6]+release_date[8]+release_date[9]

return 200, title, circle, cvList, authorList, work_age[0], release_date
return 200, img_url, title, circle, cvList, authorList, work_age[0], release_date

except os.error as err:
text.insert(tk.END, "**請求超時!\n")
text.insert(tk.END, " 請檢查網絡連接\n")
return "", "", "", [], [], "", ""
return "", "", "", "", [], [], "", ""

def nameChange():
# askdirectory()檔案對話框, 選擇目錄, 返回目錄名
path = filedialog.askdirectory()
if path == "":
messagebox.showinfo(title="錯誤", message="請選擇路徑!" + "\n")
else:
cbtn.config(state=tk.DISABLED)
cbtn_deltext.config(state=tk.DISABLED)
cbtn_dlcover.config(state=tk.DISABLED)
btn.config(state=tk.DISABLED)
btn['text'] = "等待完成"
text.insert(tk.END, "選擇路徑: " + path + "\n")
Expand All @@ -210,10 +213,10 @@ def nameChange():
else:
#print('Processing: ' + code)
text.insert(tk.END, 'Processing: ' + code + '\n')
r_status, title, circle, cvList, authorList, work_age, release_date = match_code(code)
r_status, img_url, title, circle, cvList, authorList, work_age, release_date = match_code(code)
# 如果順利爬取網頁訊息
if r_status == 200 and title and circle:
if var1.get():
if deltext.get():
# 刪除title中的【.*?】
title = re.sub(u"\\【.*?】", "", title)

Expand Down Expand Up @@ -244,6 +247,19 @@ def nameChange():
new_name = new_name.replace("cv", cv[1:])
else:
new_name = new_name.replace("(CV. cv)", "")


# 要下載封面且是資料夾
if dlcover.get() and img_url and os.path.isdir(os.path.join(path, file)):
try: # 嘗試下載封面
store_path = os.path.join(path, file, "cover.jpg")
if not os.path.isfile(store_path):
text.insert(tk.END, " 下載封面...\n")
urllib.request.urlretrieve(img_url, store_path)
else:
text.insert(tk.END, "**封面已存在,跳過下載!\n")
except os.error as err:
text.insert(tk.END, "**下載封面過程中出現錯誤!\n")

# 將Windows文件名中的非法字元替換
# re.sub(pattern, repl, string)
Expand Down Expand Up @@ -278,7 +294,8 @@ def nameChange():
text.insert(tk.END, "*******完成!*******\n\n\n\n")
tk.messagebox.showinfo(title="提示", message="完成!")

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

Expand All @@ -296,9 +313,9 @@ def thread_it(func, *args):


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

text = tk.Text(root)
text.pack()
Expand Down Expand Up @@ -359,13 +376,17 @@ def thread_it(func, *args):
text.insert(tk.END, "**使用默認命名模板:\n")
text.insert(tk.END, " workno title \n")

var1 = tk.IntVar() # 定義var1整型變數用來存放選擇行為返回值
cbtn = tk.Checkbutton(root, text='去除title中【】之間的內容', variable=var1,
deltext = tk.IntVar() # 定義整數變數用來存放選擇行為返回值
dlcover = 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)

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

btn.pack()
cbtn.pack()
cbtn_deltext.pack()
cbtn_dlcover.pack()

root.mainloop()

0 comments on commit a67a713

Please sign in to comment.