-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChapterRenamer.py
143 lines (134 loc) · 6.81 KB
/
ChapterRenamer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import os, re
class Chapter(object):
def __init__(self, text, OP_range=[87,93], ED_range=[87,93], OP_at_end=False):
if not isinstance(text, str):
raise TypeError('Input must be a str containing Chapter Infos.')
elif not isinstance(OP_range, (list, tuple)):
raise TypeError('OP_range must be a list or a tuple.')
elif not isinstance(ED_range, (list, tuple)):
raise TypeError('ED_range must be a list or a tuple.')
elif OP_range[0] > OP_range[1] or ED_range[0] > ED_range[1]:
raise TypeError('The first number should not be greater than the second.')
p1 = 'CHAPTER([0-9]+)=([0-9]+):([0-9]+):([0-9]+)\\.([0-9]+)'
p2 = 'CHAPTER([0-9]+)NAME=(.+)'
times = re.findall(p1, text)
for i in range(len(times)):
times[i] = list(map(int, list(times[i])))
self.times = times
chapterNames = []
for i in re.findall(p2, text):
chapterNames.append(i[1])
self.chapterNames = chapterNames
self.__OP, self.__ED = None, None
self.__OP_range, self.__ED_range = OP_range, ED_range
self.__OP_at_end = OP_at_end
def seekForOP(self):
if self.__OP_at_end:
for i in range(len(self.times)-1, -1, -1):
if self.times[i][2] < 17: break
elif self.__OP_range[0] <= self.times[i][2]*60+self.times[i][3]-self.times[i-1][2]*60-self.times[i-1][3] <= self.__OP_range[1]:
self.__OP = i; break
else:
for i in range(len(self.times)):
if self.times[i][2] > 8: break
elif self.__OP_range[0] <= self.times[i+1][2]*60+self.times[i+1][3]-self.times[i][2]*60-self.times[i][3] <= self.__OP_range[1]:
self.__OP = i; break
def seekForED(self):
for i in range(len(self.times)-1, -1, -1):
if self.times[i][2] < 17: break
elif self.__ED_range[0] <= self.times[i][2]*60+self.times[i][3]-self.times[i-1][2]*60-self.times[i-1][3] <= self.__ED_range[1]:
self.__ED = i-1; break
def rename(self):
if self.__OP_at_end:
self.seekForOP()
else:
self.seekForOP(); self.seekForED()
if self.__OP != None:
self.chapterNames[self.__OP] = 'Opening'
if not self.__OP_at_end:
if self.__OP == 1:
self.chapterNames[0] = 'Avant'
else:
if self.__OP >= 2:
self.chapterNames[0] = 'Avant'
if self.__ED:
self.chapterNames[self.__ED] = 'Ending'
for i in range(self.__ED-self.__OP - 1):
self.chapterNames[i + self.__OP + 1] = 'Part ' + chr(65+i)
if len(self.chapterNames) > self.__ED + 1:
self.chapterNames[-1] = 'Preview'
if len(self.chapterNames) > self.__ED + 2:
self.chapterNames[-2] = 'Part ' + chr(64+self.__ED-self.__OP)
else:
if not self.__OP_at_end:
self.chapterNames[-1] = 'Preview'
for i in range(len(self.chapterNames)-self.__OP-2):
self.chapterNames[i+self.__OP+1] = 'Part ' + chr(65+i)
else:
if len(self.chapterNames) > self.__OP + 1:
self.chapterNames[-1] = 'Preview'
if self.__OP >= 2:
for i in range(self.__OP -1):
self.chapterNames[i+1] = 'Part ' + chr(65+i)
else:
for i in range(self.__OP):
self.chapterNames[i] = 'Part ' + chr(65+i)
else:
if self.__ED != None:
self.chapterNames[self.__ED] = 'Ending'
if self.__ED == 2:
noAvant = True
self.chapterNames[0], self.chapterNames[1] = 'Part A', 'Part B'
else:
noAvant = False
self.chapterNames[0] = 'Avant'
for i in range(self.__ED-1):
self.chapterNames[i+1] = 'Part ' + chr(65 + i)
if len(self.chapterNames) > self.__ED + 1:
self.chapterNames[-1] = 'Preview'
if len(self.chapterNames) > self.__ED + 2:
self.chapterNames[-2] = 'Part ' + chr(65 + self.__ED - int(not noAvant))
else:
if len(self.chapterNames) == 2:
self.chapterNames = ['Part A', 'Part B']
elif len(self.chapterNames) == 3:
self.chapterNames = ['Part A', 'Part B', 'Preview']
else:
self.chapterNames[0] = 'Avant'
self.chapterNames[-1] = 'Preview'
for i in range(len(self.chapterNames)-2):
self.chapterNames[i+1] = 'Part ' + chr(65 + i)
result = ''
for i in range(len(self.chapterNames)):
result += 'CHAPTER{0:0>2}={1:0>2}:{2:0>2}:{3:0>2}.{4:0>3}\n'.format(i+1, self.times[i][1], self.times[i][2], self.times[i][3], self.times[i][4])
result += 'CHAPTER{0:0>2}NAME={1}\n'.format(i+1, self.chapterNames[i])
self.produced = result
def guiProceed():
chapterFiles_original = filedialog.askopenfilenames(title='[LP-Raws@LPSub] 选择章节文件 - Chapters Renamer by Lambholl', initialdir=__file__, filetypes=[('章节文件', '.txt')])
FirstOPatEnd = messagebox.askyesno('OP 是否处于结尾', '在你所选中的文件中,第一集的 OP 是否处于结尾处?')
try:
for i in range(len(chapterFiles_original)):
with open(chapterFiles_original[i], 'r', encoding='utf-8') as fb:
textOriginal = fb.read()
chaps = Chapter(textOriginal, OP_at_end=True) if FirstOPatEnd and i==0 else Chapter(textOriginal)
chaps.rename()
with open(chapterFiles_original[i], 'w', encoding='utf-8') as fb:
fb.write(chaps.produced)
if messagebox.askyesno('处理成功', '处理后的文件已覆盖原文件\n是否继续处理别的文件?'):
guiProceed()
else:
os._exit(1)
except Exception as e:
messagebox.showerror('错误', str(e))
guiProceed()
if __name__ == '__main__':
import tkinter as tk
from tkinter import ttk, filedialog, messagebox
import ctypes
ctypes.windll.shcore.SetProcessDpiAwareness(1)
ScaleFactor=ctypes.windll.shcore.GetScaleFactorForDevice(0)
guiProceed()
# LPSub Chapters Proceeder
# Written by Lambholl on 11 July, 2022
# Updated last on 11 July, 2023
# Version 1.1.2