-
Notifications
You must be signed in to change notification settings - Fork 8
/
addon.py
379 lines (301 loc) · 13.1 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
# -*- coding: utf-8 -*-
#
# Copyright (C) 2014 Thomas Amland
#
# 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 __future__ import unicode_literals
import traceback
import xbmc
import xbmcgui
import xbmcaddon
import xbmcplugin
from xbmcgui import ListItem
from requests import HTTPError
from lib import tidalapi
from lib.tidalapi.models import Album, Artist
from lib.tidalapi import Quality
from routing import Plugin
addon = xbmcaddon.Addon()
plugin = Plugin()
plugin.name = addon.getAddonInfo('name')
_addon_id = addon.getAddonInfo('id')
config = tidalapi.Config(quality=[Quality.lossless, Quality.high, Quality.low][int('0' + addon.getSetting('quality'))])
session = tidalapi.Session(config=config)
is_logged_in = False
_session_id = addon.getSetting('session_id')
_country_code = addon.getSetting('country_code')
_user_id = addon.getSetting('user_id')
if _session_id and _country_code and _user_id:
session.load_session(session_id=_session_id, country_code=_country_code, user_id=_user_id)
is_logged_in = True
def log(msg):
xbmc.log(("[%s] %s" % (_addon_id, msg)).encode('utf-8'), level=xbmc.LOGDEBUG)
def view(data_items, urls, end=True):
list_items = []
for item, url in zip(data_items, urls):
li = ListItem(item.name)
info = {'title': item.name}
if isinstance(item, Album):
info.update({'album': item.name, 'artist': item.artist.name})
if item.release_date:
info['year'] = item.release_date.year
elif isinstance(item, Artist):
info.update({'artist': item.name})
li.setInfo('music', info)
if getattr(item, 'image', None):
li.setThumbnailImage(item.image)
list_items.append((url, li, True))
xbmcplugin.addDirectoryItems(plugin.handle, list_items)
if end:
xbmcplugin.endOfDirectory(plugin.handle)
def track_list(tracks):
xbmcplugin.setContent(plugin.handle, 'songs')
list_items = []
for track in tracks:
if not track.available:
continue
url = plugin.url_for(play, track_id=track.id)
li = ListItem(track.name)
li.setProperty('isplayable', 'true')
li.setInfo('music', {
'title': track.name,
'tracknumber': track.track_num,
'discnumber': track.disc_num,
'artist': track.artist.name,
'album': track.album.name,
'year': track.album.release_date.year if track.album.release_date else None,
})
if track.album:
li.setThumbnailImage(track.album.image)
radio_url = plugin.url_for(track_radio, track_id=track.id)
li.addContextMenuItems(
[('Track Radio', 'XBMC.Container.Update(%s)' % radio_url,)])
list_items.append((url, li, False))
xbmcplugin.addDirectoryItems(plugin.handle, list_items)
xbmcplugin.endOfDirectory(plugin.handle)
def add_directory(title, endpoint,):
if callable(endpoint):
endpoint = plugin.url_for(endpoint)
xbmcplugin.addDirectoryItem(plugin.handle, endpoint, ListItem(title), True)
def urls_from_id(view_func, items):
return [plugin.url_for(view_func, item.id) for item in items]
@plugin.route('/')
def root():
if is_logged_in:
add_directory('My Music', my_music)
add_directory('Featured Playlists', featured_playlists)
add_directory("What's New", whats_new)
add_directory('Genres', genres)
add_directory('Moods', moods)
add_directory('Search', search)
add_directory('Logout', logout)
else:
add_directory('Login', login)
xbmcplugin.endOfDirectory(plugin.handle)
@plugin.route('/track_radio/<track_id>')
def track_radio(track_id):
track_list(session.get_track_radio(track_id))
@plugin.route('/moods')
def moods():
items = session.get_moods()
view(items, urls_from_id(moods_playlists, items))
@plugin.route('/moods/<mood>')
def moods_playlists(mood):
items = session.get_mood_playlists(mood)
view(items, urls_from_id(playlist_view, items))
@plugin.route('/genres')
def genres():
items = session.get_genres()
view(items, urls_from_id(genre_view, items))
@plugin.route('/genre/<genre_id>')
def genre_view(genre_id):
add_directory('Playlists', plugin.url_for(genre_playlists, genre_id=genre_id))
add_directory('Albums', plugin.url_for(genre_albums, genre_id=genre_id))
add_directory('Tracks', plugin.url_for(genre_tracks, genre_id=genre_id))
xbmcplugin.endOfDirectory(plugin.handle)
@plugin.route('/genre/<genre_id>/playlists')
def genre_playlists(genre_id):
items = session.get_genre_items(genre_id, 'playlists')
view(items, urls_from_id(playlist_view, items))
@plugin.route('/genre/<genre_id>/albums')
def genre_albums(genre_id):
xbmcplugin.setContent(plugin.handle, 'albums')
items = session.get_genre_items(genre_id, 'albums')
view(items, urls_from_id(album_view, items))
@plugin.route('/genre/<genre_id>/tracks')
def genre_tracks(genre_id):
items = session.get_genre_items(genre_id, 'tracks')
track_list(items)
@plugin.route('/featured_playlists')
def featured_playlists():
items = session.get_featured()
view(items, urls_from_id(playlist_view, items))
@plugin.route('/whats_new')
def whats_new():
add_directory('Recommended Playlists', plugin.url_for(featured, group='recommended', content_type='playlists'))
add_directory('Recommended Albums', plugin.url_for(featured, group='recommended', content_type='albums'))
add_directory('Recommended Tracks', plugin.url_for(featured, group='recommended', content_type='tracks'))
add_directory('New Playlists', plugin.url_for(featured, group='new', content_type='playlists'))
add_directory('New Albums', plugin.url_for(featured, group='new', content_type='albums'))
add_directory('New Tracks', plugin.url_for(featured, group='new', content_type='tracks'))
add_directory('Top Albums', plugin.url_for(featured, group='top', content_type='albums'))
add_directory('Top Tracks', plugin.url_for(featured, group='top', content_type='tracks'))
if session.country_code != 'US':
add_directory('Local Playlists', plugin.url_for(featured, group='local', content_type='playlists'))
add_directory('Local Albums', plugin.url_for(featured, group='local', content_type='albums'))
add_directory('Local Tracks', plugin.url_for(featured, group='local', content_type='tracks'))
xbmcplugin.endOfDirectory(plugin.handle)
@plugin.route('/featured/<group>/<content_type>')
def featured(group=None, content_type=None):
items = session.get_featured_items(content_type, group)
if content_type == 'tracks':
track_list(items)
elif content_type == 'albums':
xbmcplugin.setContent(plugin.handle, 'albums')
view(items, urls_from_id(album_view, items))
elif content_type == 'playlists':
view(items, urls_from_id(playlist_view, items))
@plugin.route('/my_music')
def my_music():
add_directory('My Playlists', my_playlists)
add_directory('Favourite Playlists', favourite_playlists)
add_directory('Favourite Artists', favourite_artists)
add_directory('Favourite Albums', favourite_albums)
add_directory('Favourite Tracks', favourite_tracks)
xbmcplugin.endOfDirectory(plugin.handle)
@plugin.route('/album/<album_id>')
def album_view(album_id):
xbmcplugin.addSortMethod(plugin.handle, xbmcplugin.SORT_METHOD_TRACKNUM)
track_list(session.get_album_tracks(album_id))
@plugin.route('/artist/<artist_id>')
def artist_view(artist_id):
xbmcplugin.setContent(plugin.handle, 'albums')
xbmcplugin.addDirectoryItem(
plugin.handle, plugin.url_for(top_tracks, artist_id),
ListItem('Top Tracks'), True
)
xbmcplugin.addDirectoryItem(
plugin.handle, plugin.url_for(artist_radio, artist_id),
ListItem('Artist Radio'), True
)
xbmcplugin.addDirectoryItem(
plugin.handle, plugin.url_for(similar_artists, artist_id),
ListItem('Similar Artists'), True
)
albums = session.get_artist_albums(artist_id) + \
session.get_artist_albums_ep_singles(artist_id) + \
session.get_artist_albums_other(artist_id)
view(albums, urls_from_id(album_view, albums))
@plugin.route('/artist/<artist_id>/radio')
def artist_radio(artist_id):
track_list(session.get_artist_radio(artist_id))
@plugin.route('/artist/<artist_id>/top')
def top_tracks(artist_id):
track_list(session.get_artist_top_tracks(artist_id))
@plugin.route('/artist/<artist_id>/similar')
def similar_artists(artist_id):
xbmcplugin.setContent(plugin.handle, 'artists')
artists = session.get_artist_similar(artist_id)
view(artists, urls_from_id(artist_view, artists))
@plugin.route('/playlist/<playlist_id>')
def playlist_view(playlist_id):
track_list(session.get_playlist_tracks(playlist_id))
@plugin.route('/user_playlists')
def my_playlists():
items = session.user.playlists()
view(items, urls_from_id(playlist_view, items))
@plugin.route('/favourite_playlists')
def favourite_playlists():
items = session.user.favorites.playlists()
view(items, urls_from_id(playlist_view, items))
@plugin.route('/favourite_artists')
def favourite_artists():
xbmcplugin.setContent(plugin.handle, 'artists')
items = session.user.favorites.artists()
view(items, urls_from_id(artist_view, items))
@plugin.route('/favourite_albums')
def favourite_albums():
xbmcplugin.setContent(plugin.handle, 'albums')
items = session.user.favorites.albums()
view(items, urls_from_id(album_view, items))
@plugin.route('/favourite_tracks')
def favourite_tracks():
track_list(session.user.favorites.tracks())
@plugin.route('/search')
def search():
add_directory('Artist', plugin.url_for(search_type, field='artist'))
add_directory('Album', plugin.url_for(search_type, field='album'))
add_directory('Playlist', plugin.url_for(search_type, field='playlist'))
add_directory('Track', plugin.url_for(search_type, field='track'))
xbmcplugin.endOfDirectory(plugin.handle)
@plugin.route('/search_type/<field>')
def search_type(field):
keyboard = xbmc.Keyboard('', 'Search')
keyboard.doModal()
if keyboard.isConfirmed():
keyboardinput = keyboard.getText()
if keyboardinput:
searchresults = session.search(field, keyboardinput)
view(searchresults.artists, urls_from_id(artist_view, searchresults.artists), end=False)
view(searchresults.albums, urls_from_id(album_view, searchresults.albums), end=False)
view(searchresults.playlists, urls_from_id(playlist_view, searchresults.playlists), end=False)
track_list(searchresults.tracks)
@plugin.route('/login')
def login():
username = addon.getSetting('username')
password = addon.getSetting('password')
if not username or not password:
# Ask for username/password
dialog = xbmcgui.Dialog()
username = dialog.input('Username')
if not username:
return
password = dialog.input('Password', option=xbmcgui.ALPHANUM_HIDE_INPUT)
if not password:
return
if session.login(username, password):
addon.setSetting('session_id', session.session_id)
addon.setSetting('country_code', session.country_code)
addon.setSetting('user_id', unicode(session.user.id))
if not addon.getSetting('username') or not addon.getSetting('password'):
# Ask about remembering username/password
dialog = xbmcgui.Dialog()
if dialog.yesno(plugin.name, 'Remember login details?'):
addon.setSetting('username', username)
addon.setSetting('password', password)
@plugin.route('/logout')
def logout():
addon.setSetting('session_id', '')
addon.setSetting('country_code', '')
addon.setSetting('user_id', '')
@plugin.route('/play/<track_id>')
def play(track_id):
media_url = session.get_media_url(track_id)
if not media_url.startswith('http://') and not media_url.startswith('https://'):
log("media url: %s" % media_url)
host, tail = media_url.split('/', 1)
app, playpath = tail.split('/mp4:', 1)
media_url = 'rtmp://%s app=%s playpath=mp4:%s' % (host, app, playpath)
li = ListItem(path=media_url)
mimetype = 'audio/flac' if config.quality == Quality.lossless else 'audio/mpeg'
li.setProperty('mimetype', mimetype)
xbmcplugin.setResolvedUrl(plugin.handle, True, li)
if __name__ == '__main__':
try:
plugin.run()
except HTTPError as e:
if e.response.status_code in [401, 403]:
dialog = xbmcgui.Dialog()
dialog.notification(plugin.name, "Authorization problem", xbmcgui.NOTIFICATION_ERROR)
traceback.print_exc()