forked from nullpointer08/nullpointer-server
-
Notifications
You must be signed in to change notification settings - Fork 1
/
playlist_json_generator.py
150 lines (124 loc) · 3.43 KB
/
playlist_json_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
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
import json
QUIT_STRING = '\\q'
playlist = []
def save():
filename = read_input('Save to file name: ')
json_file = open(filename, mode='w')
json_string = json.dumps(playlist).replace('"', '\'')
json_file.write(json_string.encode('utf-8'))
json_file.close()
print 'Saved playlist JSON to file %s' % filename
def save_and_quit():
save()
quit()
def read_input(message):
line = raw_input(message)
if line.strip() == QUIT_STRING:
quit()
return line
def read_media():
display_time = read_input('Display time (in seconds, positive int): ')
if not display_time.isdigit():
print 'Display time must be a digit'
return None
elif int(display_time) < 0:
print 'Display time must be positive'
return None
content_url = read_input('Content URL: ')
content_type = read_input('Content type (web_page/image/video): ')
valid_types = ('web_page', 'image', 'video')
if content_type not in valid_types:
print 'Content type must be one of: %s, %s, %s' % valid_types
return None
media = {
'time': display_time,
'uri': content_url,
'type': content_type
}
return media
def load_json_file():
global playlist
filepath = read_input('Write relative filepath of JSON file: ')
json_file = open(filepath, mode='r')
json_string = json_file.read()
playlist = json.loads(json_string)
print 'Loaded playlist:'
show_media()
def show_media():
if len(playlist) == 0:
print 'No media in playlist'
return
index = 1
for media in playlist:
print '%s) %s' % (index, repr(media))
index += 1
def remove_media():
if len(playlist) == 0:
print 'No media to remove'
return
print 'Choose index of media to remove:'
index = 1
for media in playlist:
print '%s) %s' % (index, repr(media))
del_index = read_input('Index of media to be removed: ')
if not del_index.isdigit():
print 'The choice must be one of the numbers provided'
return
del playlist[int(del_index)-1]
def add_media():
media = read_media()
if media is not None:
playlist.append(media)
print 'Added media %s' % repr(media)
else:
print 'Did not add media'
MENU_OPTIONS = (
{
'name': 'Show media',
'handler': show_media
},
{
'name': 'Add media',
'handler': add_media
},
{
'name': 'Remove media',
'handler': remove_media
},
{
'name': 'Save JSON to file',
'handler': save
},
{
'name': 'Load existing JSON file',
'handler': load_json_file
},
{
'name': 'Save and quit',
'handler': save_and_quit
},
{
'name': 'Quit without saving',
'handler': quit
}
)
def show_menu():
while True:
print '\nSelect an option:'
index = 1
for option in MENU_OPTIONS:
print '%s) %s' % (index, option['name'])
index += 1
choice = read_input('Selection: ')
if not choice.isdigit():
print 'Choice must be a number'
continue
print '\n'
handler = MENU_OPTIONS[int(choice)-1]['handler']
handler()
def run():
print 'This program will generate an ordered JSON playlist.'
print 'Write %s and press enter to quit.\n' % QUIT_STRING
show_menu()
if __name__ == '__main__':
run()