-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChapter_Cleanier.py
More file actions
287 lines (243 loc) · 8.08 KB
/
Chapter_Cleanier.py
File metadata and controls
287 lines (243 loc) · 8.08 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import re
import os
##################################################################################################################################
# #
# Chapter Cleaner V1 #
# Created By: Daren Thoman #
##################################################################################################################################
class CC:
"""
The Class CC Cleans a Raw Text files
That Have a Sieres of Chapter Timestamps with a Episode # At The top
Example:
ep1 or EP1 or Ep1 or 1
2302 or 23:02
23:94
The Patern Repets
Parameters:
input_file:
input_file is where the raw chapter ts text file goes
ts_list:
ts_list is used for just using make_xml & make_txt functions
chapter_file_type:
Used to select the chapter timestamp file type
Functions:
ts(Input_Timestamp)
ep(Input_Episode)
Input_anything(Input)
make_xml(self, output_file=None, TS_Language="eng")
make_txt(self, output_file=None)
CoreV2(self)
Create_output_files(self)"""
def __init__(self, input_file, ts_list=None, chapter_file_type="txt"):
self.input_file = input_file
if input_file != None and os.path.exists(input_file) is False:
raise FileNotFoundError
self.ts_list = ts_list
# xml and txt
self.chapter_file_type = chapter_file_type
# if self.chapter_file_type != "txt" or "xml":
# raise ValueError("invalid mode: chapter_file_type is not: txt, xml")
def ts(Input_Timestamp):
"""
Returns a found Timestamp using regex else None
Paramiters:
Input_Timestamp (str)
"""
Min = Sec = Mil = None
ts_regex = r"(?P<Min>^\d\d?)[:| ]?(?P<Sec>\d\d)(.(?P<Mil>\d\d?))?"
ts_prog = re.compile(ts_regex)
ts_result = ts_prog .match(Input_Timestamp)
try:
Min = ts_result.group("Min")
Sec = ts_result.group("Sec")
Mil = ts_result.group("Mil")
except AttributeError:
pass
if Min and Sec and Mil:
Mil = Mil.ljust(3, '0')
Min = Min.rjust(2, '0')
return ('00:%s:%s.%s' % (Min, Sec, Mil))
elif Min and Sec:
Min = Min.ljust(2, '0')
return ('00:%s:%s.000' % (Min, Sec))
else:
return None
def ep(Input_Episode):
"""Returns a found Episode number using regex else None
Paramiters:
Input_Episode (str)
Returns:
"Ep_##" (str) else: None
"""
EP_State = gr_three = gr_six = gr_four = None
ep_regex = r"((ep|Ep|EP)(\d\d?))?(^\d\d?$)?([D|d]\d[\s|:](\d\d?))?"
ep_prog = re.compile(ep_regex)
ep_result = ep_prog.match(Input_Episode)
try:
gr_four = ep_result.group(4)
gr_three = ep_result.group(3)
gr_six = ep_result.group(6)
except AttributeError:
pass
if gr_four or gr_three or gr_six:
if gr_four == None:
pass
else:
EP_State = ('Ep_%s' % (gr_four))
if gr_three == None:
pass
else:
EP_State = ('Ep_%s' % (gr_three))
if gr_six == None:
pass
else:
EP_State = ('Ep_%s' % (gr_six))
else:
EP_State = None
return EP_State
def Input_anything(Input):
"""Returns if the input is a TS. or EP. or neither and cleans it with ts() or ep()
Paramiters:
Input (str)
Returns:
State:
"EP" (str) or "TS" (str) or None
if a TS or EP Returns Result:
Result of ep() or ts() else None
"""
r_ep = CC.ep(Input)
r_ts = CC.ts(Input)
if type(r_ep) == str:
State = 'EP'
return State, r_ep
elif type(r_ts) == str:
State = 'TS'
return State, r_ts
else:
return None, None
def make_xml(self, output_file=None, language="eng"):
"""Turns a List into a XML Chapter File
Paramiters:
output_file=None:
outputs the TXT File if used alone you input CC(ts_list=input_list_here)
"""
from xml.dom import minidom
if output_file == None or False:
print("WARNING! make_xml is not outputing to file.")
root = minidom.Document()
Chapters = root.createElement('Chapters')
root.appendChild(Chapters)
EditionEntry = root.createElement("EditionEntry")
Chapters.appendChild(EditionEntry)
for index, ts in enumerate(self.ts_list):
ChapterAtom = root.createElement("ChapterAtom")
EditionEntry.appendChild(ChapterAtom)
ChapterTimeStart = root.createElement("ChapterTimeStart")
ChapterTimeStart.appendChild(root.createTextNode(ts))
ChapterAtom.appendChild(ChapterTimeStart)
try:
ChapterTimeEnd = root.createElement("ChapterTimeEnd")
ChapterTimeEnd.appendChild(
root.createTextNode(self.ts_list[index + 1]))
ChapterAtom.appendChild(ChapterTimeEnd)
except IndexError:
pass
ChapterDisplay = root.createElement("ChapterDisplay")
ChapterAtom.appendChild(ChapterDisplay)
ChapterString = root.createElement("ChapterString")
ChapterString.appendChild(
root.createTextNode(f"Chapter {index + 1:02}"))
ChapterDisplay.appendChild(ChapterString)
ChapterLanguage = root.createElement("ChapterLanguage")
ChapterLanguage.appendChild(root.createTextNode(language))
ChapterDisplay.appendChild(ChapterLanguage)
xml_str = root.toprettyxml(indent=" ")
print(xml_str, file=output_file)
def make_txt(self, output_file=None):
"""Turns a List into a XML Chapter File
Paramiters:
output_file=None:
outputs the TXT File if used alone you input CC(ts_list=input_list_here)
"""
if output_file == None or False:
print("Xml is not writing to FILE!\n\n\n")
i = -1
L = len(self.ts_list)
Chapter_number = 0
for line in self.ts_list:
Chapter_number = Chapter_number + 1
Chapter_number2 = str(Chapter_number)
Chapter_number2 = Chapter_number2.zfill(2)
i = i + 1
test = f"Chapter {Chapter_number2}"
if L - 1 == i:
test = "Ending"
if test == "Chapter 01":
test = "Intro"
Formarted_Body = f"CHAPTER{Chapter_number2}={self.ts_list[i]}\nCHAPTER{Chapter_number2}NAME={test}"
print(Formarted_Body, file=output_file)
def CoreV2(self):
"""This Function does the created naming of the files"""
exten = self.chapter_file_type
self.ts_list = []
with open(self.input_file, 'r') as File:
for line in File:
State, Temp = CC.Input_anything(line)
if State == 'EP':
if self.ts_list > []:
print(self.ts_list)
if exten == "txt":
CC.make_txt(self, output_file=ep_f)
elif exten == "xml":
CC.make_xml(self, output_file=ep_f)
ep_f.close()
print(f"Creating: {Temp}.{exten}")
ep_f = open(f"{Temp}.{exten}", "w")
self.ts_list = list()
elif State == 'TS':
self.ts_list.append(Temp)
else:
pass
print("\n", self.ts_list)
if exten == "txt":
CC.make_txt(self, output_file=ep_f)
elif exten == "xml":
CC.make_xml(self, output_file=ep_f)
else:
print("This Not A Valied Input")
raise ValueError
def Create_output_files(self, output_dir=None):
"""This the Main function creates the output folder
Paramiters:
output_dir="C:\\Users\\Username\\Documents\\"
is were the folder will output to
"""
if output_dir is None:
# output_dir = os.path.join(
# os.path.expanduser("~"), "\\Documents\\")
output_dir = os.getcwd()
# Takes the path off the File.
striped_file = os.path.basename(self.input_file)
title, exten = striped_file.split('.')
output_folder_name = title + "_Split & Cleaned"
output_folderpath = os.path.join(output_dir, output_folder_name)
if os.path.exists(output_folderpath):
print('WARNING!: this output folder: ',
output_folderpath, 'already Exist\n')
raise FileExistsError
else:
os.mkdir(output_folderpath)
print(striped_file)
print(output_folderpath)
os.chdir(output_folderpath)
CC.CoreV2(self)
if __name__ == '__main__':
f = os.path.join(os.getcwd(), "Sources\\Test Chapter File.txt")
# Timestamps = ["00:11:11.000", "00:22:00.000", "00:33:00.000", "00:44:00.000", "00:55:00.000"]
print("input file:", f)
f1 = CC(input_file=f, chapter_file_type="txt")
f1.Create_output_files()
os.startfile(output_folderpath)
# with open(f, "r") as file:
# print(file.readline())