-
Notifications
You must be signed in to change notification settings - Fork 0
/
playlist_generator.py
47 lines (37 loc) · 2.08 KB
/
playlist_generator.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
#!/usr/bin/env python
'''
Contracted by Thomas Piltoff via Upwork
Playlist generator for gui module
'''
import os
import random
__author__ = 'Michael Genson'
__copyright__ = 'Copyright (C) 2021 Michael Genson'
__license__ = 'GPL'
def build_playlist(playlist_path, episode_dirs, commercial_dir, commercial_min, commercial_max, __appname__, __version__):
def file_to_str(file):
return str(file)
def get_shuffled_commercials_list(commercial_dir, previous_commercial):
commercials_list = os.listdir(commercial_dir)
random.shuffle(commercials_list)
while previous_commercial == commercials_list[0]: random.shuffle(commercials_list) # make sure the first commercial is not the same as the previous one
return commercials_list
if type(episode_dirs) != list: episode_dirs = [episode_dirs] # correction for single directories
playlist_data = [f'# Playlist generated by {__appname__} version {__version__}'] # commented header
commercials = []
next_commercial = ''
for episode_dir in episode_dirs:
episode_parts = sorted(os.listdir(episode_dir))
for episode_part in episode_parts:
path = os.path.join(episode_dir, episode_part)
if os.path.isfile(path):
playlist_data.append(f'{path}') # add next episode part to playlist
if episode_dir != episode_dirs[-1] or episode_part != episode_parts[-1]: # add commercials if this isn't the last part of the last episode
for x in range(0, random.randint(commercial_min, commercial_max)):
if not commercials: commercials = get_shuffled_commercials_list(commercial_dir, next_commercial) # get new shuffled list of commercials
next_commercial = commercials.pop()
playlist_data.append(f'{os.path.join(commercial_dir, next_commercial)}')
with open(playlist_path, 'w+') as f:
for i, video_link in enumerate(playlist_data):
if i: f.write('\n') # print newline if it's not the first item
f.write(video_link)