forked from pythcon/PlexTVChannel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generatePlaylist.py
executable file
·365 lines (296 loc) · 15.1 KB
/
generatePlaylist.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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
import os
import sys
import random
import moviepy.editor as mp
import time
import math
from datetime import datetime, timedelta
import tvdb_api
# Display help
# Check if it is a backup file
backup = False
commercials = False
if not len(sys.argv) > 1:
print ("Incorrect Usage: python3 generateShowList.py <backup? (yes | no)> <commercials? (yes | no)>")
exit()
else:
if sys.argv[1] == "yes":
backup = True
print ("BACKUP FLAG WAS SET TO TRUE")
time.sleep(3)
if sys.argv[2] == "yes":
commercials = True
print ("COMMERCIALS FLAG SET TO TRUE")
time.sleep(3)
if dir == "":
print ("Please populate your 'dir' variable! It cannot be null. Open up the script")
exit()
########################################################
########################################################
# Edit these variable to your system! #
########################################################
########################################################
cartoons = ["show1", "show2", "show3"]
dir = "" # Directory to grab shows from (make sure there is a "/" at the end!)
tvDirectory = "" # Directory where files will be generated (make sure there is a "/" at the end!)
commercialsDirectory = "" # Directory where commercials will be pulled from (make sure there is a "/" at the end!)
timezone = "" # Enter Timezone (Example: "-0400" for EST)
showPoster = ""
channelName = ""
########################################################
########################################################
# END #
########################################################
########################################################
cartoonsLeft = cartoons.copy()
showDirectory = []
commercialList = []
commercialSpecific = {}
previousRandomShow = 999
playlistDuration = 0
showDurations = []
showName = ""
showDesc = ""
showCounter = 0
showLength = 0
blockCounter = 0
t = tvdb_api.Tvdb()
extensions = ['.264', '.3g2', '.3gp', '.3gp2', '.3gpp', '.3gpp2', '.3mm', '.3p2', '.60d', '.787', '.89', '.aaf', '.aec', '.aep', '.aepx',
'.aet', '.aetx', '.ajp', '.ale', '.am', '.amc', '.amv', '.amx', '.anim', '.aqt', '.arcut', '.arf', '.asf', '.asx', '.avb',
'.avc', '.avd', '.avi', '.avp', '.avs', '.avs', '.avv', '.axm', '.bdm', '.bdmv', '.bdt2', '.bdt3', '.bik', '.bin', '.bix',
'.bmk', '.bnp', '.box', '.bs4', '.bsf', '.bvr', '.byu', '.camproj', '.camrec', '.camv', '.ced', '.cel', '.cine', '.cip',
'.clpi', '.cmmp', '.cmmtpl', '.cmproj', '.cmrec', '.cpi', '.cst', '.cvc', '.cx3', '.d2v', '.d3v', '.dat', '.dav', '.dce',
'.dck', '.dcr', '.dcr', '.ddat', '.dif', '.dir', '.divx', '.dlx', '.dmb', '.dmsd', '.dmsd3d', '.dmsm', '.dmsm3d', '.dmss',
'.dmx', '.dnc', '.dpa', '.dpg', '.dream', '.dsy', '.dv', '.dv-avi', '.dv4', '.dvdmedia', '.dvr', '.dvr-ms', '.dvx', '.dxr',
'.dzm', '.dzp', '.dzt', '.edl', '.evo', '.eye', '.ezt', '.f4p', '.f4v', '.fbr', '.fbr', '.fbz', '.fcp', '.fcproject', '.ffd',
'.flc', '.flh', '.fli', '.flv', '.flx', '.gfp', '.gl', '.gom', '.grasp', '.gts', '.gvi', '.gvp', '.h264', '.hdmov', '.hkm',
'.ifo', '.imovieproj', '.imovieproject', '.ircp', '.irf', '.ism', '.ismc', '.ismv', '.iva', '.ivf', '.ivr', '.ivs', '.izz',
'.izzy', '.jss', '.jts', '.jtv', '.k3g', '.kmv', '.ktn', '.lrec', '.lsf', '.lsx', '.m15', '.m1pg', '.m1v', '.m21', '.m21',
'.m2a', '.m2p', '.m2t', '.m2ts', '.m2v', '.m4e', '.m4u', '.m4v', '.m75', '.mani', '.meta', '.mgv', '.mj2', '.mjp', '.mjpg',
'.mk3d', '.mkv', '.mmv', '.mnv', '.mob', '.mod', '.modd', '.moff', '.moi', '.moov', '.mov', '.movie', '.mp21', '.mp21',
'.mp2v', '.mp4', '.mp4v', '.mpe', '.mpeg', '.mpeg1', '.mpeg4', '.mpf', '.mpg', '.mpg2', '.mpgindex', '.mpl', '.mpl',
'.mpls', '.mpsub', '.mpv', '.mpv2', '.mqv', '.msdvd', '.mse', '.msh', '.mswmm', '.mts', '.mtv', '.mvb', '.mvc', '.mvd',
'.mve', '.mvex', '.mvp', '.mvp', '.mvy', '.mxf', '.mxv', '.mys', '.ncor', '.nsv', '.nut', '.nuv', '.nvc', '.ogm', '.ogv', '.ogx',
'.osp', '.otrkey', '.pac', '.par', '.pds', '.pgi', '.photoshow', '.piv', '.pjs', '.playlist', '.plproj', '.pmf', '.pmv', '.pns',
'.ppj', '.prel', '.pro', '.prproj', '.prtl', '.psb', '.psh', '.pssd', '.pva', '.pvr', '.pxv', '.qt', '.qtch', '.qtindex', '.qtl',
'.qtm', '.qtz', '.r3d', '.rcd', '.rcproject', '.rdb', '.rec', '.rm', '.rmd', '.rmd', '.rmp', '.rms', '.rmv', '.rmvb', '.roq', '.rp',
'.rsx', '.rts', '.rts', '.rum', '.rv', '.rvid', '.rvl', '.sbk', '.sbt', '.scc', '.scm', '.scm', '.scn', '.screenflow', '.sec',
'.sedprj', '.seq', '.sfd', '.sfvidcap', '.siv', '.smi', '.smi', '.smil', '.smk', '.sml', '.smv', '.spl', '.sqz', '.srt', '.ssf',
'.ssm', '.stl', '.str', '.stx', '.svi', '.swf', '.swi', '.swt', '.tda3mt', '.tdx', '.thp', '.tivo', '.tix', '.tod', '.tp', '.tp0',
'.tpd', '.tpr', '.trp', '.ts', '.tsp', '.ttxt', '.tvs', '.usf', '.usm', '.vc1', '.vcpf', '.vcr', '.vcv', '.vdo', '.vdr', '.vdx',
'.veg', '.vem', '.vep', '.vf', '.vft', '.vfw', '.vfz', '.vgz', '.vid', '.video', '.viewlet', '.viv', '.vivo', '.vlab', '.vob',
'.vp3', '.vp6', '.vp7', '.vpj', '.vro', '.vs4', '.vse', '.vsp', '.w32', '.wcp', '.webm', '.wlmp', '.wm', '.wmd', '.wmmp', '.wmv',
'.wmx', '.wot', '.wp3', '.wpl', '.wtv', '.wve', '.wvx', '.xej', '.xel', '.xesc', '.xfl', '.xlmv', '.xmv', '.xvid', '.y4m', '.yog',
'.yuv', '.zeg', '.zm1', '.zm2', '.zm3', '.zmv']
print("#####GENERATING SHOW LIST#####")
########################################################
# Writes to the show list
########################################################
def writeToArray(path):
global tempShowList
global extensions
ext = os.path.splitext(path)[-1].lower()
if ext in extensions:
tempShowList.append(path)
#print("Added: " + path)
########################################################
########################################################
# Check duration of file
########################################################
def checkDuration(file):
try:
return mp.VideoFileClip(file).duration
except OSError:
print("Could not get duration for: " + file)
return 1799 # default value if it fails
########################################################
########################################################
# Generate episdoe list that fit into 40min -1hr block
########################################################
def generateBlock(episodes):
global playlistDuration
returnList = []
durationCheck = False
totalDuration = 0
while not durationCheck:
randomChoice = random.choice(episodes)
currentDuration = checkDuration(randomChoice)
totalDuration += currentDuration
playlistDuration += currentDuration
if totalDuration < 3600:
returnList.append(randomChoice)
else:
durationCheck = True
return returnList
########################################################
########################################################
# Generate a random commercial
########################################################
def getRandomCommercial(listOfCommercials):
randomNumber = random.randint(0, len(listOfCommercials)-1)
return listOfCommercials[randomNumber]
########################################################
########################################################
# Get Poster for show
########################################################
def getShowPoster(show_name):
global showPoster
global t
try:
show = t[show_name] # Get Show
poster = show.data['poster'] # Get poster for show
return poster
except: #If not found return channel poster
return showPoster
########################################################
########################################################
# Get Description for show
########################################################
def getShowDescription(show_name):
global t
try:
show = t[show_name] # Get Show
desc = show.data['overview'] # Get description for show
return desc
except: #If not found return channel poster
return ""
########################################################
# Loops into individual directories to get directly to files
while len(cartoonsLeft) != 0:
tempShowList = []
randomNumber = random.randint(0, len(cartoonsLeft)-1)
currentShow = cartoonsLeft[randomNumber]
cartoonsLeft.remove(currentShow)
for file in os.listdir(dir + currentShow):
if "Specials" in file or "Subs" in file: #omit specials, extras, subtitles and deleted scenes
continue
if os.path.isfile(dir + currentShow + "/" + file):
writeToArray(dir + currentShow + "/" + file)
else:
for seasonFile in os.listdir(dir + currentShow + "/" + file):
writeToArray(dir + currentShow + "/" + file + "/" + seasonFile)
showDirectory.append(tempShowList.copy())
# Clear Temp Show List
tempShowList = []
# Loops into commercials directory
if (commercials):
for file in os.listdir(commercialsDirectory):
if "ignore" in file: #omit commercials you dont want
continue
if os.path.isfile(commercialsDirectory + file):
writeToArray(commercialsDirectory + file)
else:
for subFile in os.listdir(commercialsDirectory + file):
if os.path.isfile(commercialsDirectory + file + "/" + subFile):
writeToArray(commercialsDirectory + file + "/" + subFile)
else:
tempSpecificList = []
tempSpecificShow = ""
for specificCommercial in os.listdir(commercialsDirectory + file + "/" + subFile):
tempSpecificList.append(commercialsDirectory + file + "/" + subFile + "/" + specificCommercial)
tempSpecificShow = specificCommercial
# After Specific Show list is generated, insert into dictionary
commercialSpecific[tempSpecificShow] = tempSpecificList.copy()
commercialList = tempShowList.copy()
# Open Files
if not backup:
tvList = open(tvDirectory + "showList.txt", "w")
m3u = open(tvDirectory + "playlist.m3u", "w")
else:
tvList = open(tvDirectory + "showList1.txt", "w")
m3u = open(tvDirectory + "playlist1.m3u", "w")
xmltv = open(tvDirectory + "temp_xmltv.xml", "w")
# Initial line in M3U file
m3u.write("#EXTM3U\n")
# Initial line for xmltv
xmltv.write("<tv generator-info-name='Todd' source-info-name='Todds Generator'>\n")
xmltv.write("<channel id='1'>\n")
xmltv.write("<display-name>" + channelName + "</display-name>\n")
xmltv.write("<icon src='" + showPoster + "'/>\n")
xmltv.write("</channel>\n")
# Generate Blocks of Episodes & puts back into directory
while blockCounter < len(showDirectory):
print ("Generating Blocks..." + str(len(showDirectory) - blockCounter) + " left.")
showDirectory[blockCounter] = generateBlock(showDirectory[blockCounter])
blockCounter += 1
# Loops through show directory and generate random schedule
while len(showDirectory) > 0:
randomShow = random.randint(0, len(showDirectory)-1) # already a random show because populated in random order
specificCommercialFound = False # Initial Specific Commercial
initialRunThrough = True
while len(showDirectory[randomShow]) > 0:
randomEpisode = random.choice(showDirectory[randomShow])
randomEpisodeWrite = randomEpisode.encode('utf-8').strip().decode()
# Get Name of Show
showName = randomEpisode.split(dir)[1].split('/')[0]
showName = showName.encode('utf-8').strip().decode()
# Get Description of Show (Episode Name)
showDesc = os.path.basename(randomEpisode)
showDesc = os.path.splitext(showDesc)[0]
if (commercials):
# Only check for specific commercial on initial run-through
if initialRunThrough:
initialRunThrough = False # No longer initial run-through
# Check to see if there is specific commercial
for show in commercialSpecific.keys():
if show.lower() in showName.lower():
randomCommercial = random.choice(commercialSpecific[show])
specificCommercialFound = True # Found a specific commercial
print("Writing: " + showName)
# Write episode to txt file
tvList.write(randomEpisodeWrite)
tvList.write("\n")
#Need to get duration for XML and array
cur_duration = checkDuration(randomEpisode)
# Write episode to m3u file
m3u.write("#EXTINF: " + str(cur_duration) + ", " + showDesc + "\n")
m3u.write("file://" + randomEpisodeWrite + "\n")
# Add extra guide information
showDesc += " ||\n\n "
showDesc += getShowDescription(showName)
#Unicode Stuff
showName = showName.encode('ascii', 'ignore').decode('ascii')
showDesc = showDesc.encode('ascii', 'ignore').decode('ascii')
# Write episode to xmltv file
xmltv.write("<programme channel='1' start='{tempStartTime} " + timezone + "' stop='{tempEndTime} " + timezone + "'>\n")
xmltv.write("<title lang='en'>" + showName + "</title>\n")
xmltv.write("<desc lang='en'>" + showDesc + "</desc>\n")
xmltv.write("<icon height='' src='" + getShowPoster(showName) + "' width=''/>\n")
xmltv.write("<video/>\n<date/>\n<new/>\n</programme>\n")
# If commercials
if (commercials):
# If there is no specific commercial
if not specificCommercialFound:
randomCommercial = getRandomCommercial(commercialList).encode('utf-8').strip().decode()
commercial_duration = checkDuration(randomCommercial)
m3u.write("#EXTINF: " + str(commercial_duration) + ", Commercial\n")
m3u.write("file://" + randomCommercial + "\n")
# Round duration to the nearest second
cur_duration = math.ceil(cur_duration)
# Add Episode and Duration to dictionary
showDurations.append(cur_duration)
# Remove the episode
#print ("Removing: " + randomEpisode)
showDirectory[randomShow].remove(randomEpisode)
specificCommercialFound = False
# Remove show
del showDirectory[randomShow]
# Close xmltv
xmltv.write("</tv>")
# Close files
tvList.close()
m3u.close()
xmltv.close()
# store variables to file for later xmltv generation
temp_variables = open(tvDirectory + "temp_variables.py", "w")
temp_variables.write("tvDirectory='" + tvDirectory +"'")
temp_variables.write("\n")
if backup:
temp_variables.write("backup=True")
else:
temp_variables.write("backup=False")
temp_variables.write("\n")
temp_variables.write("showDurations=" + str(showDurations))
temp_variables.close()
print ("Playlist Duration in seconds: " + str(playlistDuration))
print("Finished! Happy Streaming!")