forked from m0ngr31/kanzi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_custom_slots.py
131 lines (99 loc) · 3.21 KB
/
generate_custom_slots.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
import re
import string
import random
import os
from kodi_voice import KodiConfigParser, Kodi
config_file = os.path.join(os.path.dirname(__file__), "kodi.config")
config = KodiConfigParser(config_file)
kodi = Kodi(config)
def most_words(l=[]):
longest = 0
for s in l:
if len(s.split()) > longest:
longest = len(s.split())
return longest
def sort_by_words(l, longest):
distributed = []
for i in range(1, longest + 1):
dl = [s for s in l if len(s.split()) == i]
if len(dl) > 0:
distributed.append(dl)
return distributed
def clean_results(resp, cat, key, limit=100):
cleaned = []
if 'result' in resp and cat in resp['result']:
for v in retrieved['result'][cat]:
name = kodi.sanitize_name(v[key], normalize=False)
# omit titles with digits, as Amazon never passes numbers as digits
if not re.search(r'\d', name):
cleaned.append(name)
cleaned = list(set(cleaned))
cleaned = filter(None, cleaned)
random.shuffle(cleaned)
# distribute strings evenly by number of words
if len(cleaned) > limit:
longest = most_words(cleaned)
distributed = sort_by_words(cleaned, longest)
if len(distributed) > 0:
total = 0
cleaned = []
while total < limit:
for l in distributed:
if len(l) > 0:
total += 1
cleaned.append(l.pop())
# sort by number of words just for visibility
if len(cleaned) > 0:
longest = most_words(cleaned)
distributed = sort_by_words(cleaned, longest)
if len(distributed) > 0:
cleaned = []
for dl in distributed:
cleaned += [l for l in dl]
return cleaned[:limit]
def write_file(filename, items=[]):
print 'Writing: %s' % (filename)
f = open(filename, 'w')
for a in items:
f.write("%s\n" % a.encode("utf-8"))
f.close()
# Generate MUSICPLAYLISTS Slot
retrieved = kodi.GetMusicPlaylists()
cl = clean_results(retrieved, 'files', 'label')
write_file('MUSICPLAYLISTS', cl)
# Generate MUSICARTISTS Slot
retrieved = kodi.GetMusicArtists()
cl = clean_results(retrieved, 'artists', 'artist')
write_file('MUSICARTISTS', cl)
# Generate MUSICALBUMS Slot
retrieved = kodi.GetAlbums()
cl = clean_results(retrieved, 'albums', 'label')
write_file('MUSICALBUMS', cl)
# Generate MUSICSONGS Slot
retrieved = kodi.GetSongs()
cl = clean_results(retrieved, 'songs', 'label')
write_file('MUSICSONGS', cl)
# Generate VIDEOPLAYLISTS Slot
retrieved = kodi.GetVideoPlaylists()
cl = clean_results(retrieved, 'files', 'label')
write_file('VIDEOPLAYLISTS', cl)
# Generate MOVIEGENRES Slot
retrieved = kodi.GetVideoGenres()
cl = clean_results(retrieved, 'genres', 'label')
write_file('MOVIEGENRES', cl)
# Generate MOVIES Slot
retrieved = kodi.GetMovies()
cl = clean_results(retrieved, 'movies', 'label')
write_file('MOVIES', cl)
# Generate SHOWS Slot
retrieved = kodi.GetTvShows()
cl = clean_results(retrieved, 'tvshows', 'label')
write_file('SHOWS', cl)
# Generate ADDONS Slot
retrieved = {'result': {'addons': []}}
for content in ['video', 'audio', 'image', 'executable']:
r = kodi.GetAddons(content)
if 'result' in r and 'addons' in r['result']:
retrieved['result']['addons'] += r['result']['addons']
cl = clean_results(retrieved, 'addons', 'name')
write_file('ADDONS', cl)