-
Notifications
You must be signed in to change notification settings - Fork 0
/
addon.py
454 lines (407 loc) · 16.5 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
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
'''
RockPeaks plugin for XBMC
Copyright 2013 Artem Matsak
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 string import ascii_lowercase
import CommonFunctions
from xbmcswift2 import xbmc, xbmcplugin, actions, Plugin, ListItem
from RockPeaksAPI import RockPeaksAPI
from RockPeaksLogin import RockPeaksLogin
common = CommonFunctions
plugin = Plugin()
api = RockPeaksAPI()
login = RockPeaksLogin(plugin, api)
pageSize = 25
@plugin.route('/')
def index():
do_login = 'login' in plugin.request.args and plugin.request.args['login'][0] == '1'
if do_login:
login.login()
items = [
{
'label': plugin.get_string(30500),
'path': plugin.url_for('search', page='0', update_listing='0'),
},
{
'label': plugin.get_string(30501),
'path': plugin.url_for('latest_videos', page='0', update_listing='0'),
},
{
'label': plugin.get_string(30502),
'path': plugin.url_for('latest_discs', page='0', update_listing='0'),
},
{
'label': plugin.get_string(30503),
'path': plugin.url_for('artist_index'),
},
{
'label': plugin.get_string(30504),
'path': plugin.url_for('show_index'),
},
{
'label': plugin.get_string(30505),
'path': plugin.url_for('genre_index'),
}
]
if len(plugin.get_setting('sessid')) == 0:
items.append({
'label': plugin.get_string(30506),
'path': plugin.url_for('index', login='1'),
})
else:
items.append({
'label': plugin.get_string(30507),
'path': plugin.url_for('tracked_artists', page='0', update_listing='0'),
})
items.append({
'label': plugin.get_string(30508),
'path': plugin.url_for('playlist_index', page='0', update_listing='0'),
})
return plugin.finish(items, update_listing=do_login)
@plugin.route('/genres/index/')
def genre_index():
data = api.request('genre.getMasterGenres', {})
items = [_make_genre_item(genre) for genre in data['genres']['genre']]
return items
@plugin.route('/search/<page>/')
def search(page='0'):
update_listing = 'update_listing' not in plugin.request.args or plugin.request.args['update_listing'][0] == '1'
if not update_listing:
keys = common.getUserInput(plugin.get_string(30509), '')
plugin.set_setting('search_keys', keys)
else:
keys = plugin.get_setting('search_keys')
page = int(page)
data = api.request('search.searchContent', {
'keys': keys,
'page': page
})
items = [_make_search_result_item(search_result) for search_result in data['items']['item']]
# It's possible for _make_search_result_item() to return an empty item.
items = filter(None, items)
_add_pager(items, {'endpoint': 'search'}, page, 10, int(data['items']['count']))
return plugin.finish(items, update_listing=update_listing)
@plugin.route('/discs/latest/<page>/')
def latest_discs(page='0'):
update_listing = 'update_listing' not in plugin.request.args or plugin.request.args['update_listing'][0] == '1'
page = int(page)
data = api.request('disc.getDiscs', {
'page': page,
'limit': pageSize
})
items = [_make_disc_item(disc) for disc in data['discs']['disc']]
_add_pager(items, {'endpoint': 'latest_discs'}, page, pageSize, int(data['discs']['count']))
return plugin.finish(items, update_listing=update_listing)
@plugin.route('/playlists/index/<page>/')
def playlist_index(page='0'):
update_listing = 'update_listing' not in plugin.request.args or plugin.request.args['update_listing'][0] == '1'
page = int(page)
data = api.request('user.getPlaylists', {
'sessid': plugin.get_setting('sessid'),
'page': page,
'limit': pageSize
})
items = [_make_playlist_item(playlist) for playlist in data['playlists']['playlist']]
_add_pager(items, {'endpoint': 'playlist_index'}, page, pageSize, int(data['playlists']['count']))
return plugin.finish(items, update_listing=update_listing)
@plugin.route('/tracked_artists/<page>/')
def tracked_artists(page='0'):
update_listing = 'update_listing' not in plugin.request.args or plugin.request.args['update_listing'][0] == '1'
page = int(page)
data = api.request('user.getTrackedArtists', {
'sessid': plugin.get_setting('sessid'),
'page': page,
'limit': pageSize
})
items = [_make_artist_item(artist) for artist in data['artists']['artist']]
_add_pager(items, {'endpoint': 'tracked_artists'}, page, pageSize, int(data['artists']['count']))
return plugin.finish(items, update_listing=update_listing)
@plugin.route('/videos/latest/<page>/')
def latest_videos(page='0'):
update_listing = 'update_listing' not in plugin.request.args or plugin.request.args['update_listing'][0] == '1'
page = int(page)
data = api.request('video.getVideos', {
'sources': _get_supported_sources(),
'status': 1,
'sort': 'date',
'order': 'desc',
'page': page,
'limit': pageSize
})
items = [_make_video_item(video) for video in data['videos']['video']]
_add_pager(items, {'endpoint': 'latest_videos'}, page, pageSize, int(data['videos']['count']))
return plugin.finish(items, update_listing=update_listing)
@plugin.route('/artists/index', name='artist_index', options={'entity': 'artist'})
@plugin.route('/shows/index', name='show_index', options={'entity': 'show'})
def entity_index(entity):
items = []
alpha_func = ''
if entity == 'artist':
alpha_func = 'artist_alpha'
else:
alpha_func = 'show_alpha'
items = [{
'label': l.upper(),
'path': plugin.url_for(alpha_func, letter=l, page='0', update_listing='0')
} for l in ascii_lowercase]
items.insert(0, {
'label': '#',
'path': plugin.url_for(alpha_func, letter='misc', page='0', update_listing='0')
})
return items
@plugin.route('/artists/alpha/<letter>/<page>/')
def artist_alpha(letter, page='0'):
update_listing = 'update_listing' not in plugin.request.args or plugin.request.args['update_listing'][0] == '1'
page = int(page)
data = api.request('artist.getArtists', {
'letter': letter,
'page': page,
'limit': pageSize
})
items = [_make_artist_item(artist) for artist in data['artists']['artist']]
_add_pager(items, {'endpoint': 'artist_alpha', 'letter': letter}, page, pageSize, int(data['artists']['count']))
return plugin.finish(items, update_listing=update_listing)
@plugin.route('/shows/alpha/<letter>/<page>/')
def show_alpha(letter, page='0'):
update_listing = 'update_listing' not in plugin.request.args or plugin.request.args['update_listing'][0] == '1'
page = int(page)
data = api.request('show.getShows', {
'letter': letter,
'page': page,
'limit': pageSize
})
items = [_make_show_item(show) for show in data['shows']['show']]
_add_pager(items, {'endpoint': 'show_alpha', 'letter': letter}, page, pageSize, int(data['shows']['count']))
return plugin.finish(items, update_listing=update_listing)
@plugin.route('/discs/<disc_id>/')
def disc_videos(disc_id):
data = api.request('disc.getInfo', {
'id': disc_id,
'video_sources': _get_supported_sources(),
'video_status': 1
})
items = [_make_video_item(video) for video in data['disc']['videos']['video']]
return items
@plugin.route('/discs/<disc_id>/play_all/')
def disc_play_all(disc_id):
data = api.request('disc.getInfo', {
'id': disc_id,
'video_sources': _get_supported_sources(),
'video_status': 1
})
items = [_make_video_item(video) for video in data['disc']['videos']['video']]
playlist = xbmc.PlayList(xbmc.PLAYLIST_VIDEO)
playlist.clear()
for item in items:
listitem = ListItem.from_dict(**item)
playlist.add(item['path'], listitem.as_xbmc_listitem())
player = xbmc.Player()
player.playPlaylist(playlist)
@plugin.route('/playlists/<playlist_id>/')
def playlist_videos(playlist_id):
data = api.request('playlist.getInfo', {
'id': playlist_id,
'video_sources': _get_supported_sources(),
'video_status': 1
})
items = [_make_video_item(video) for video in data['playlist']['videos']['video']]
return items
@plugin.route('/playlists/<playlist_id>/play_all/')
def playlist_play_all(playlist_id):
data = api.request('playlist.getInfo', {
'id': playlist_id,
'video_sources': _get_supported_sources(),
'video_status': 1
})
items = [_make_video_item(video) for video in data['playlist']['videos']['video']]
playlist = xbmc.PlayList(xbmc.PLAYLIST_VIDEO)
playlist.clear()
for item in items:
listitem = ListItem.from_dict(**item)
playlist.add(item['path'], listitem.as_xbmc_listitem())
player = xbmc.Player()
player.playPlaylist(playlist)
@plugin.route('/genres/<genre_name>/<page>')
def genre_videos(genre_name, page='0'):
update_listing = 'update_listing' not in plugin.request.args or plugin.request.args['update_listing'][0] == '1'
page = int(page)
data = api.request('video.getVideos', {
'genres': genre_name,
'sources': _get_supported_sources(),
'status': 1,
'sort': 'added',
'order': 'desc',
'page': page,
'limit': pageSize
})
items = [_make_video_item(video) for video in data['videos']['video']]
_add_pager(items, {'endpoint': 'genre_videos', 'genre_name': genre_name}, page, pageSize, int(data['videos']['count']))
return plugin.finish(items, update_listing=update_listing, sort_methods=_get_video_sort_methods())
@plugin.route('/artists/<artist_id>/<page>/')
def artist_videos(artist_id, page='0'):
update_listing = 'update_listing' not in plugin.request.args or plugin.request.args['update_listing'][0] == '1'
page = int(page)
data = api.request('video.getVideos', {
'artist_id': artist_id,
'sources': _get_supported_sources(),
'status': 1,
'sort': 'added',
'order': 'desc',
'page': page,
'limit': pageSize
})
items = [_make_video_item(video, ['artist']) for video in data['videos']['video']]
_add_pager(items, {'endpoint': 'artist_videos', 'artist_id': artist_id}, page, pageSize, int(data['videos']['count']))
return plugin.finish(items, update_listing=update_listing, sort_methods=_get_video_sort_methods())
@plugin.route('/shows/<show_id>/<page>/')
def show_videos(show_id, page='0'):
update_listing = 'update_listing' not in plugin.request.args or plugin.request.args['update_listing'][0] == '1'
page = int(page)
data = api.request('video.getVideos', {
'show_id': show_id,
'sources': _get_supported_sources(),
'status': 1,
'sort': 'added',
'order': 'desc',
'page': page,
'limit': pageSize
})
items = [_make_video_item(video, ['show']) for video in data['videos']['video']]
_add_pager(items, {'endpoint': 'show_videos', 'show_id': show_id}, page, pageSize, int(data['videos']['count']))
return plugin.finish(items, update_listing=update_listing, sort_methods=_get_video_sort_methods())
@plugin.route('/videos/play/<source>/<video_id>')
def play_video(source, video_id):
plugin.set_resolved_url(_get_playback_path(source, video_id))
def _disc_play_all_ctx(disc_id):
label = 'Play all'
url = plugin.url_for('disc_play_all', disc_id=disc_id)
return (label, actions.background(url))
def _playlist_play_all_ctx(playlist_id):
label = 'Play all'
url = plugin.url_for('playlist_play_all', playlist_id=playlist_id)
return (label, actions.background(url))
def _make_disc_item(disc):
item = {
'label': disc['title'],
'path': plugin.url_for('disc_videos', disc_id=disc['id']),
'thumbnail': disc['artwork'],
'context_menu': [
_disc_play_all_ctx(disc['id']),
]
}
return item
def _make_playlist_item(playlist):
item = {
'label': playlist['name'],
'path': plugin.url_for('playlist_videos', playlist_id=playlist['id']),
'thumbnail': playlist['image'],
'context_menu': [
_playlist_play_all_ctx(playlist['id']),
]
}
return item
def _make_genre_item(genre):
item = {
'label': genre['name'],
'path': plugin.url_for('genre_videos', genre_name=genre['name'], page='0', update_listing='0'),
}
return item
def _make_artist_item(artist):
item = {
'label': artist['name'],
'path': plugin.url_for('artist_videos', artist_id=artist['id'], page='0', update_listing='0'),
'thumbnail': artist['publicity_image']
}
return item
def _make_show_item(show):
item = {
'label': show['name'],
'path': plugin.url_for('show_videos', show_id=show['id'], page='0', update_listing='0'),
'thumbnail': show['image']
}
return item
def _make_video_item(video, omit_info=[]):
label = video['title']
if 'artist' not in omit_info:
label += ' | ' + video['artist']
if 'show' not in omit_info:
label += ' | ' + video['show']
if 'date' not in omit_info:
label += ' | ' + video['date_string']
item = {
'label': label,
'path': plugin.url_for('play_video', source=video['source']['name'], video_id=video['source']['id']),
'thumbnail': video['image'],
'info': {
'title': label,
'date': '%(d)02d.%(m)02d.%(y)04d' % {'d': int(video['date']['day'] or '1'), 'm': int(video['date']['month'] or '1'), 'y': int(video['date']['year'] or '0')},
'rating': float(video['rp_ranking'] or "0") / 100.0,
'duration': _format_duration(int(video['duration'])),
'dateadded': video['added']
},
'is_playable': True,
}
return item
def _make_search_result_item(search_result):
item = None
if search_result['type'] == 'Artist':
item = _make_artist_item(search_result)
item['label'] = plugin.get_string(30510) % item['label']
elif search_result['type'] == 'Show':
item = _make_show_item(search_result)
item['label'] = plugin.get_string(30511) % item['label']
elif search_result['type'] == 'Clip':
if 'source' in search_result and 'name' in search_result['source'] and search_result['source']['name'] in ['youtube', 'vimeo', 'dailymotion'] and search_result['source']['status'] == '1':
item = _make_video_item(search_result)
item['label'] = plugin.get_string(30512) % item['label']
return item
def _get_video_sort_methods():
return [
xbmcplugin.SORT_METHOD_UNSORTED,
xbmcplugin.SORT_METHOD_LABEL,
xbmcplugin.SORT_METHOD_DATE,
xbmcplugin.SORT_METHOD_VIDEO_RATING
]
def _get_supported_sources():
return 'youtube,vimeo,dailymotion'
def _get_playback_path(source, video_id):
if source == 'youtube':
return 'plugin://plugin.video.youtube/?action=play_video&videoid=%s' % video_id
elif source == 'vimeo':
return 'plugin://plugin.video.vimeo/?action=play_video&videoid=%s' % video_id
elif source == 'dailymotion':
return 'plugin://plugin.video.dailymotion_com/?mode=playVideo&url=%s' % video_id
def _add_pager(items, params, page, pageSize, count):
if pageSize * (page+1) < count:
params['page'] = str(page + 1)
items.insert(0, {
'label': 'Next page >>',
'path': plugin.url_for(**params)
})
if page > 0:
params['page'] = str(page - 1)
items.insert(0, {
'label': '<< Previous page',
'path': plugin.url_for(**params)
})
def _format_duration(seconds):
h = seconds / 3600
m = (seconds - h*3600) / 60
s = seconds - h*3600 - m*60
if h > 0:
return '%(h)02d:%(m)02d:%(s)02d' % {'h': h, 'm': m, 's': s}
else:
return '%(m)02d:%(s)02d' % {'m': m, 's': s}
if __name__ == '__main__':
login.login(silent=True)
plugin.run()