-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchapterTime.py
More file actions
258 lines (245 loc) · 12.1 KB
/
chapterTime.py
File metadata and controls
258 lines (245 loc) · 12.1 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
import os
import time
import json
import re
import pandas as pd
from csv import DictWriter, reader
from tkinter.filedialog import askopenfilename, asksaveasfilename
def clear(): os.system('cls')
def printChapterTime():
print(r'''
______ __ _
.' ___ |[ | / |_
/ .' \_| | |--. ,--. _ .--. `| |-'.---. _ .--.
| | | .-. | `'_\ :[ '/'`\ \| | / /__\\[ `/'`\]
\ `.___.'\ | | | | // | |,| \__/ || |,| \__., | |
`.____ .'[___]|__]\'-;__/| ;.__/ \__/ '.__.'[___]
_________ _ [__|
| _ _ |(_) _
|_/ | | \_|__ _ .--..--. .---. ,/_\,
| | [ | [ `.-. .-. |/ /__\\ ,/_/ \_\,
_| |_ | | | | | | | || \__., /_/ ___ \_\
|_____| [___][___||__||__]'.__.' /_/ |(V)| \_\
| .-. |
| / / \ |
| \ \ / |
| '-' |
'--,-,--'
| |
| |
| |
/\|
\/|
/\
\/
''')
def printWolf():
print(r'''
__
.d$$b
.' TO$;\ AWWWWOOOOOO"
/ : TP._;
/ _.; :Tb| SIR SPENCER, WOLF OF KANSAS CITY SAYS:
/ / ;j$j
_.-' d$$$$ THANKS FOR USING CHAPTER TIME!
.' .. d$$$$; THIS IS VALUE FOR VALUE SOFTWARE!
/ / P' d$$$$P. |\ IF YOU RECEIVED VALUE FROM USING
/ ' .d$$$P' |\^'l THIS PROGRAM, CONSIDER RETURNING
.' `T$P^''"'' : EQUIVALENT VALUE IN ONE OF THE
._.' _.' ; FOLLOWING WAYS:
`-.- '.-'-' ._. _.-'.-''
`.-' _____ ._ .-' - SHARE CHAPTER TIME WITH A FRIEND
- (.g$$$$$$$b. .' - BOOST THE BOWL AFTER BOWL PODCAST
'' ^^ T$$$P ^) .(: - SEND SATS TO ONE OF THESE LN ADDRESSES:
_ / -' /.' /:/; sirspencer@fountain.fm
._.'-'`-' ')/ /;/; sirspencer@getalby.com
`-.- '..--'' ' / / ;
.-' ..--'' -' : GOT AN IDEA TO MAKE IT BETTER? SUBMIT A
..--''--.- ' (\ .-(\ PR TO THE PROJECT HERE:
..--'' `-\(\/;` https://github.com/SpencerPearson/chapter-time
_. :
;`- TOOT ME ON MASTODON: @spencer@mk.spook.social
:\ EMAIL ME: spencer@bowlafterbowl.com
;
''')
def getSeconds(time_str):
"""Get seconds from time."""
h, m, s = time_str.split(':')
return int(h) * 3600 + int(m) * 60 + int(s)
tailDotRGX = re.compile(r'(?:(\.)|(\.\d*?[1-9]\d*?))0+(?=\b|[^0-9])')
def removeZeros(a):
return tailDotRGX.sub(r'\2', a)
os.system("title Chapter Time -- Podcast Chapter Converter")
def main():
finished = False
while finished == False:
clear()
printChapterTime()
print('Please choose what you would like to do:')
choice = input('1) convert JSON chapters to CSV markers\n'
+ '2) convert CSV markers to JSON chapters\n'
+ '3) see Value For Value info\n\n'
+ 'Selection: ')
if choice == '1':
converted = False
while converted == False:
fullPath = askopenfilename()
print(f'\nOpening chapter file from path:'
+ f'\n{fullPath}')
f = open(fullPath)
jsonData = json.load(f)
timestamps = []
counter = 0
print(f'Converting JSON chapters to timestamps...')
for i in jsonData['chapters']:
# get startTime and name of chapter
startTime = i['startTime']
chapterName = i['title']
# remove any commas in chapterName
if ',' in chapterName:
chapterName = chapterName.replace(',', '')
# convert seconds to hh:mm:ss
hms = time.strftime('%H:%M:%S', time.gmtime(startTime))
print(f'Chapter {counter + 1}: {chapterName}\nJSON seconds: {startTime}\nFormatted time: {hms}')
print('------------------------------------------------------')
time.sleep(.2)
# add to timestamp list
timestamps.append({'Name': chapterName, 'Start': hms, 'Duration': '0:00.000', 'Format': 'decimal', 'Type': 'Cue', 'Description': ''})
counter += 1
confStamps = input('Review formatted times above. Does that look right?'
+ f'\n(Y)es to save changes to .csv file, (N)o to cancel.'
+ '\n(Y)es/(N)o: ').lower()
if confStamps == 'y' or confStamps == 'yes' or confStamps == '1':
clear()
printChapterTime()
# make keys from dict list
keys = timestamps[0].keys()
savePath = asksaveasfilename(defaultextension='.csv')
with open(savePath, 'w', newline='') as outputFile:
dictWriter = DictWriter(outputFile, keys, delimiter='\t')
dictWriter.writeheader()
dictWriter.writerows(timestamps)
input('Markers file created! Press Enter to continue...')
keepGoin = input('Are you all finished?\nEnter (Y)es to exit, or anything else to return to main menu.')
converted = True
if keepGoin == 'yes' or keepGoin == 'y' or keepGoin == '1':
clear()
printWolf()
input('Press Enter to exit Chapter Time...')
finished = True
else:
clear()
printChapterTime()
print(f'Changes discarded. Please check that your timestamps are correct before trying again.')
input('Press Enter to continue...')
clear()
printChapterTime()
keepGoin = input('Are you all finished?\nEnter (Y)es to exit, or anything else to return to main menu.')
converted = True
if keepGoin == 'yes' or keepGoin == 'y' or keepGoin == '1':
clear()
printWolf()
input('Press Enter to exit Chapter Time...')
finished = True
elif choice == '2':
converted = False
while converted == False:
fullPath = askopenfilename()
print(f'\nOpening csv markers file from path:\n'
+ f'{fullPath}')
f = open(fullPath)
csvReader = reader(f)
timestamps = []
counter = 0
print(f'Converting timestamps to seconds...')
for i in csvReader:
#skip first loop
if counter > 0:
# get start time
list = i[0].split('\t')
startTime = list[1]
ogTime = startTime
# remove ms from time
if len(startTime.split('.')) > 1:
ms = startTime.split('.')[1]
else:
ms = None
startTime = startTime.split('.')[0]
if len(startTime.split(':')) < 3:
startTime = '0:' + startTime
# convert hh:mm:ss to seconds
seconds = getSeconds(startTime)
if ms != None:
if ms != '000':
seconds = str(seconds) + '.' + ms
else:
seconds = str(seconds)
seconds = float(removeZeros(seconds))
if seconds == 0.0:
seconds = 0.001
# add to timestamp list
print(f'Marker {counter}: {ogTime} => {seconds} seconds')
time.sleep(.2)
timestamps.append(seconds)
timestamps.sort()
counter += 1
print(f'timestamps converted! Here are your timestamps:')
print(timestamps)
input('Press enter to continue...')
clear()
printChapterTime()
# open json chapters file
# fileName = getFileNameFormat(show, episode)
print('Please locate the original JSON file to overwrite.')
input('Press enter to select the file...')
clear()
printChapterTime()
oldJSON = askopenfilename()
with open(oldJSON, 'r+') as jsonFile:
jsonChapters = json.load(jsonFile)
counter = 0
print(f'Replacing old chapter times with new timestamps...')
for i in jsonChapters['chapters']:
oldTime = i['startTime']
# update timestamp
i['startTime'] = timestamps[counter]
counter += 1
print(f'Chapter {counter}: {i['title']}\nOld time: {oldTime} ==> New time: {i['startTime']}')
print('-------------------------------------------')
time.sleep(.25)
confStamps = input('Review timestamps above. Does that look right?'
+ f'\n(Y)es to save changes to JSON file, (N)o to cancel.'
+ '\n(Y)es/(N)o: ').lower()
if confStamps == 'y' or confStamps == 'yes' or confStamps == '1':
clear()
printChapterTime()
jsonFile.seek(0)
jsonFile.write(json.dumps(jsonChapters))
jsonFile.truncate()
print(f'Chapters file updated! Changes to JSON file saved!')
time.sleep(2)
converted = True
else:
clear()
printChapterTime()
print(f'Changes discarded. Please check that your timestamps are correct before trying again.')
input('Press Enter to continue...')
converted = True
keepGoin = input('Are you all finished?\nEnter (Y)es to exit, or anything else to return to main menu.')
if keepGoin == 'yes' or keepGoin == 'y' or keepGoin == '1':
clear()
printWolf()
input('Press Enter to exit Chapter Time...')
finished = True
elif choice == '3':
clear()
printWolf()
input('Press Enter to return to the menu...')
else:
print('\nInvalid selection, try again!')
time.sleep(2)
clear()
printChapterTime()
continue
if __name__ == '__main__':
main()