-
Notifications
You must be signed in to change notification settings - Fork 8
/
addon.py
249 lines (214 loc) · 7.79 KB
/
addon.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2012 Tristan Fischer (sphere@dersphere.de)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
from xbmcswift2 import Plugin
from resources.lib.api import ShoutcastApi, NetworkError
plugin = Plugin()
api = ShoutcastApi('sh1t7hyn3Kh0jhlV')
my_stations = plugin.get_storage('my_stations.json', file_format='json')
STRINGS = {
'all': 30000,
'top500_stations': 30001,
'browse_by_genre': 30002,
'my_stations': 30003,
'search_station': 30004,
'search_current_track': 30005,
'add_to_my_stations': 30010,
'remove_from_my_stations': 30011,
'network_error': 30200
}
@plugin.route('/')
def show_root_menu():
items = (
{'label': _('top500_stations'),
'path': plugin.url_for('show_top500_stations')},
{'label': _('browse_by_genre'),
'path': plugin.url_for('show_genre')},
{'label': _('search_station'),
'path': plugin.url_for('search_station')},
{'label': _('search_current_track'),
'path': plugin.url_for('search_current_track')},
{'label': _('my_stations'),
'path': plugin.url_for('show_my_stations')},
)
return plugin.finish(items)
@plugin.route('/top500/')
def show_top500_stations():
items = get_cached(api.get_top500_stations, TTL=60)
return __add_stations(items)
@plugin.route('/genres/', name='show_genre')
@plugin.route('/genres/<parent_genre_id>/', name='show_subgenre')
def show_genre(parent_genre_id=None):
show_subgenres = plugin.get_setting('show_subgenres', bool)
genres = get_cached(api.get_genres, parent_genre_id, TTL=1440)
items = []
if show_subgenres and parent_genre_id:
items.append({
'label': '[[ %s ]]' % _('all'),
'path': plugin.url_for(
endpoint='show_stations',
genre_id=str(parent_genre_id)
)
})
for genre in genres:
item = {'label': genre.get('name')}
if show_subgenres and genre.get('has_childs'):
item['path'] = plugin.url_for(
endpoint='show_subgenre',
parent_genre_id=str(genre['id'])
)
else:
item['path'] = plugin.url_for(
endpoint='show_stations',
genre_id=str(genre['id'])
)
items.append(item)
return plugin.finish(items)
@plugin.route('/stations/<genre_id>/')
def show_stations(genre_id):
items = get_cached(api.get_stations, genre_id, TTL=60)
return __add_stations(items)
@plugin.route('/resolve/<station_id>')
def resolve_play_url(station_id):
stream_url = api.resolve(station_id)
if stream_url:
plugin.set_resolved_url(stream_url)
@plugin.route('/search/station/')
def search_station():
search_string = plugin.keyboard(heading=_('search_station'))
if search_string:
url = plugin.url_for(
endpoint='search_station_result',
search_string=search_string
)
plugin.redirect(url)
@plugin.route('/search/station/<search_string>/')
def search_station_result(search_string):
stations = api.search_stations(search_string)
return __add_stations(stations)
@plugin.route('/search/current_track/')
def search_current_track():
search_string = plugin.keyboard(heading=_('search_current_track'))
if search_string:
url = plugin.url_for(
endpoint='search_current_track_result',
search_string=search_string
)
plugin.redirect(url)
@plugin.route('/search/current_track/<search_string>/')
def search_current_track_result(search_string):
stations = api.search_current_track(search_string)
return __add_stations(stations)
@plugin.route('/my/')
def show_my_stations():
stations = my_stations.values()
return __add_stations(stations)
@plugin.route('/my/add/<station_id>/<station_name>')
def add_to_my_stations(station_id, station_name):
station = api.get_station(station_id, station_name)
my_stations[station_id] = station
my_stations.sync()
@plugin.route('/my/del/<station_id>')
def del_from_my_stations(station_id):
if station_id in my_stations:
del my_stations[station_id]
my_stations.sync()
def __add_stations(stations):
addon_id = plugin._addon.getAddonInfo('id')
icon = 'special://home/addons/%s/icon.png' % addon_id
my_stations_ids = my_stations.keys()
items = []
show_bitrate = plugin.get_setting('show_bitrate_in_title', bool)
if plugin.get_setting('bitrate_filter_enabled', bool):
bitrates = (96, 128, 160, 192)
min_bitrate = plugin.get_setting('bitrate_filter', choices=bitrates)
else:
min_bitrate = None
for i, station in enumerate(stations):
if min_bitrate and int(station.get('bitrate', 0)) < min_bitrate:
continue
station_id = str(station['id'])
if not station_id in my_stations_ids:
context_menu = [(
_('add_to_my_stations'),
'XBMC.RunPlugin(%s)' % plugin.url_for(
endpoint='add_to_my_stations',
station_id=station_id,
station_name=station['name'].encode('utf-8')
)
)]
else:
context_menu = [(
_('remove_from_my_stations'),
'XBMC.RunPlugin(%s)' % plugin.url_for(
endpoint='del_from_my_stations',
station_id=station_id
)
)]
if show_bitrate and station.get('bitrate'):
name = '%s [%s kbps]' % (station['name'], station['bitrate'])
else:
name = station['name']
item = {
'label': name,
'thumbnail': icon,
'info': {
'count': i,
'genre': station.get('genre') or '',
'size': station.get('bitrate') or 0,
'listeners': station.get('listeners') or 0,
'artist': station.get('current_track') or '',
'tracknumber': station['id'],
'comment': station.get('media_type') or ''
},
'context_menu': context_menu,
'is_playable': True,
'path': plugin.url_for(
endpoint='resolve_play_url',
station_id=station['id'],
)
}
items.append(item)
finish_kwargs = {
'sort_methods': [
'LISTENERS',
'BITRATE',
('LABEL', '%X'),
],
}
return plugin.finish(items, **finish_kwargs)
def get_cached(func, *args, **kwargs):
'''Return the result of func with the given args and kwargs
from cache or execute it if needed'''
@plugin.cached(kwargs.pop('TTL', 1440))
def wrap(func_name, *args, **kwargs):
return func(*args, **kwargs)
return wrap(func.__name__, *args, **kwargs)
def _(string_id):
if string_id in STRINGS:
return plugin.get_string(STRINGS[string_id])
else:
plugin.log.warning('String is missing: %s' % string_id)
return string_id
if __name__ == '__main__':
limit = plugin.get_setting('limit', int)
api.set_limit(limit)
try:
plugin.run()
except NetworkError:
plugin.notify(msg=_('network_error'))