Commit b3204675 authored by Jean-Francois Dockes's avatar Jean-Francois Dockes
Browse files

Deezer sort of working. favorite albums, playlists, and search. 128kbs mp3 only

parent 29bce280
......@@ -88,10 +88,10 @@ def urls_from_id(view_func, items):
return [plugin.url_for(view_func,
for item in items if str('http') != 0]
def view(data_items, urls, end=True):
for item, url in zip(data_items, urls):
title =
image = item.image if item.image else None
......@@ -114,28 +114,50 @@ def track_list(tracks):
xbmcplugin.objid, tracks)
def root():
if not maybelogin():
return []
items = sess.get_alluserplaylists()
add_directory('Favourites', my_music)
def my_music():
add_directory('Albums', favourite_albums)
add_directory('Playlists', favourite_playlists)
def favourite_playlists():
items = sess.get_favourite_playlists()
view(items, urls_from_id(userplaylist_view, items))
def favourite_albums():
items = sess.get_favourite_albums()
view(items, urls_from_id(album_view, items))
def userplaylist_view(id):
def artist_view(artist_id):
albums = session.get_artist_albums(artist_id)
albums = sess.get_artist_albums(artist_id)
view(albums, urls_from_id(album_view, albums))
def album_view(album_id):
def trackuri(a):
global pathprefix
msgproc.log("trackuri: [%s]" % a)
trackid = trackid_from_urlpath(pathprefix, a)
url = sess.get_media_url(trackid) or ""
#msgproc.log("%s" % media_url)
return {'media_url' : url}
def browse(a):
......@@ -72,7 +72,7 @@ class DeezerAPI(object):
def __init__(self):
self.apiUrl = "{service}/{id}/{method}"
self.streamUrl = ""
self.streamingUrl = ""
self.authUrl = ""
self.username = None
self.passmd5 = None
......@@ -108,7 +108,7 @@ class DeezerAPI(object):
return None
time2 = time.time()'Request took {:.3f} ms'.format((time2-time1)*1000.0))
#log.debug("DeezerAPI: response: %s" % r.text)
log.debug("DeezerAPI: response: %s" % r.text)
self.status_code = int(r.status_code)
if self.status_code != 200:
log.warn("status code !200. data: %s" % r.content)
......@@ -125,6 +125,16 @@ class DeezerAPI(object):
return None
return js
def request_stream(self, id='', type='track'):
response = self.session.get(self.streamingUrl, params={
'access_token': self.access_token,
"{}_id".format(type): id,
'device': 'panasonic'
if type.startswith('radio') or type.startswith('artist'):
return response.json()
return response.text
def isloggedin(self):
log.debug("isloggedin(): username %s" % self.username)
......@@ -171,18 +181,23 @@ class DeezerAPI(object):
return False
def getAllUserPlaylists(self, **ka):
def getUserPlaylists(self, **ka):
if not self.isloggedin():
return {}
data = self._api_request('user', self.user_id, 'playlists')
return data
def getUserPlaylist(self, **ka):
def getUserAlbums(self, **ka):
if not self.isloggedin():
return {}
data = self._api_request('user', self.user_id, 'albums')
return data
def getUserPlaylist(self, id):
if not self.isloggedin():
return None
self._check_ka(ka, ['playlist_id'])
data = self._api_request('playlist', ka['playlist_id'], 'tracks')
return None
data = self._api_request('playlist', id, 'tracks')
return data
def search(self, query, filter, offset=0):
if not self.isloggedin():
......@@ -190,3 +205,7 @@ class DeezerAPI(object):
data = self._api_request('search', method=filter,
params={'q': query, 'index': offset})
return data
def getAlbum(self, id):
data = self._api_request('album', id)
return data
......@@ -29,19 +29,35 @@ class Session(object):
def isloggedin(self):
return self.api.isloggedin()
def get_media_url(self, trackid):
return self.api.request_stream(id=trackid)
def get_alluserplaylists(self):
data = self.api.getAllUserPlaylists()
def get_favourite_playlists(self):
data = self.api.getUserPlaylists()
if not data:
return []
return [_parse_playlist(p) for p in data['data']]
def get_favourite_albums(self):
data = self.api.getUserAlbums()
return [_parse_album(p) for p in data['data']]
def get_user_playlist(self, id):
data = self.api.getUserPlaylist(playlist_id=id)
data = self.api.getUserPlaylist(id)
if not data:
return []
return [_parse_track(t) for t in data]
return [_parse_track(t) for t in data['data']]
def get_album_tracks(self, albumid):
data = self.api.getAlbum(albumid)
album = _parse_album(data)
if 'tracks' in data:
tracks = [_parse_track(t, album) for t in data['tracks']['data']]
for i in range(len(tracks)):
if tracks[i].track_num == 0:
tracks[i].track_num = i+1
return tracks
def _search1(self, query, tp):
uplog("_search1: query [%s] tp [%s]" % (query, tp))
......@@ -96,7 +112,10 @@ class Session(object):
res = self._search1(query, 'track')
cplt.tracks = res.tracks
return cplt
def _parse_playlist(data, artist=None, artists=None):
kwargs = {
......@@ -108,19 +127,6 @@ def _parse_playlist(data, artist=None, artists=None):
return Playlist(**kwargs)
def encode_prefix(p):
return base64.urlsafe_b64encode(p.encode('utf-8')).decode('utf-8')
def decode_prefix(p):
return base64.urlsafe_b64decode(p.encode('utf-8')).decode('utf-8')
def _parse_category(data):
return Category(id=encode_prefix(data['prefix']), name=data['title'], iid = data['id'])
def _parse_genre(data):
return Genre(id=encode_prefix(data['prefix']), name=data['title'], iid = data['id'])
def _parse_artist(data):
return Artist(id=data['id'], name=data['name'])
......@@ -152,7 +158,7 @@ def _parse_track(data, albumarg = None):
'name': data['title'],
'duration': data['duration'],
'artist': artist,
'available': True
'available': data['readable']
if 'track_position' in data:
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment