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

Deezer: factorise looping code

parent ae0e732e
...@@ -122,7 +122,6 @@ def root(): ...@@ -122,7 +122,6 @@ def root():
add_directory('My Favourites Playlists', my_favourite_playlists) add_directory('My Favourites Playlists', my_favourite_playlists)
add_directory('My Favourites Tracks', my_favourite_tracks) add_directory('My Favourites Tracks', my_favourite_tracks)
add_directory('My Favourites Artists', my_favourite_artists) add_directory('My Favourites Artists', my_favourite_artists)
add_directory('My Favourites Playlists', my_favourite_playlists)
add_directory('Family', my_family) add_directory('Family', my_family)
@plugin.route('/my_family') @plugin.route('/my_family')
...@@ -218,7 +217,7 @@ def browse(a): ...@@ -218,7 +217,7 @@ def browse(a):
plugin.run([idpath]) plugin.run([idpath])
#msgproc.log("%s" % xbmcplugin.entries) #msgproc.log("%s" % xbmcplugin.entries)
encoded = json.dumps(xbmcplugin.entries) encoded = json.dumps(xbmcplugin.entries)
return {"entries" : encoded} return {"entries" : encoded, "total" : str(len(xbmcplugin.entries))}
@dispatcher.record('search') @dispatcher.record('search')
def search(a): def search(a):
...@@ -256,7 +255,7 @@ def search(a): ...@@ -256,7 +255,7 @@ def search(a):
#msgproc.log("search results: %s" % xbmcplugin.entries) #msgproc.log("search results: %s" % xbmcplugin.entries)
encoded = json.dumps(xbmcplugin.entries) encoded = json.dumps(xbmcplugin.entries)
return {"entries" : encoded} return {"entries" : encoded, "total" : str(len(xbmcplugin.entries))}
msgproc.log("Deezer running") msgproc.log("Deezer running")
......
...@@ -52,7 +52,7 @@ from upmplgutils import * ...@@ -52,7 +52,7 @@ from upmplgutils import *
class MLog(object): class MLog(object):
def __init__(self): def __init__(self):
self.f = sys.stderr self.f = sys.stderr
self.level = 2 self.level = 1
def _doprint(self, msg): def _doprint(self, msg):
print("DeezerAPI: %s" % msg, file=self.f) print("DeezerAPI: %s" % msg, file=self.f)
def debug(self, msg): def debug(self, msg):
...@@ -185,48 +185,54 @@ class DeezerAPI(object): ...@@ -185,48 +185,54 @@ class DeezerAPI(object):
return {} return {}
return self._api_request('user', self.user_id, 'followings') return self._api_request('user', self.user_id, 'followings')
def getUserPlaylists(self, id='me'): def getUserPlaylists(self, id='me', offset=0):
if not self.isloggedin(): if not self.isloggedin():
return {} return {}
if id == 'me': if id == 'me':
id = self.user_id id = self.user_id
data = self._api_request('user', id, 'playlists') data = self._api_request('user', id, 'playlists',
params={'index': offset})
return data return data
def getUserAlbums(self, id='me'): def getUserAlbums(self, id='me', offset=0):
if not self.isloggedin(): if not self.isloggedin():
return {} return {}
if id == 'me': if id == 'me':
id = self.user_id id = self.user_id
data = self._api_request('user', id, 'albums') data = self._api_request('user', id, 'albums',
params={'index': offset})
return data return data
def getUserArtists(self, id='me'): def getUserArtists(self, id='me', offset=0):
if not self.isloggedin(): if not self.isloggedin():
return {} return {}
if id == 'me': if id == 'me':
id = self.user_id id = self.user_id
data = self._api_request('user', id, 'artists') data = self._api_request('user', id, 'artists',
params={'index': offset})
return data return data
def getUserTracks(self, id='me'): def getUserTracks(self, id='me', offset=0):
if not self.isloggedin(): if not self.isloggedin():
return {} return {}
if id == 'me': if id == 'me':
id = self.user_id id = self.user_id
data = self._api_request('user', id, 'tracks') data = self._api_request('user', id, 'tracks',
params={'index': offset})
return data return data
def getArtistAlbums(self, artid): def getArtistAlbums(self, artid, offset=0):
if not self.isloggedin(): if not self.isloggedin():
return {} return {}
data = self._api_request('artist', artid, 'albums') data = self._api_request('artist', artid, 'albums',
params={'index': offset})
return data return data
def getUserPlaylist(self, id): def getUserPlaylist(self, id, offset=0):
if not self.isloggedin(): if not self.isloggedin():
return None return None
data = self._api_request('playlist', id, 'tracks') data = self._api_request('playlist', id, 'tracks',
params={'index': offset})
return data return data
def search(self, query, filter, offset=0): def search(self, query, filter, offset=0):
......
...@@ -56,33 +56,36 @@ class Session(object): ...@@ -56,33 +56,36 @@ class Session(object):
data = self.api.getUserFollowings() data = self.api.getUserFollowings()
return [_parse_user(p) for p in data['data']] return [_parse_user(p) for p in data['data']]
def _loopit(self, id, getfunc, parsefunc, limit):
offset = 0
all = []
while offset < limit:
data = getfunc(id, offset=offset)
ncnt = len(data['data'])
ndata = [parsefunc(i) for i in data['data']]
all.extend(ndata)
if ncnt == 0:
break
offset += ncnt
return all
def get_favourite_playlists(self, id='me'): def get_favourite_playlists(self, id='me'):
data = self.api.getUserPlaylists(id=id) return self._loopit(id, self.api.getUserPlaylists, _parse_playlist, 1000)
if not data:
return []
return [_parse_playlist(p) for p in data['data']]
def get_favourite_albums(self, id='me'): def get_favourite_albums(self, id='me'):
data = self.api.getUserAlbums(id=id) return self._loopit(id, self.api.getUserAlbums, _parse_album, 1000)
return [_parse_album(p) for p in data['data']]
def get_favourite_artists(self, id='me'): def get_favourite_artists(self, id='me'):
data = self.api.getUserArtists(id=id) return self._loopit(id, self.api.getUserArtists, _parse_artist, 1000)
return [_parse_artist(p) for p in data['data']]
def get_favourite_tracks(self, id='me'):
data = self.api.getUserTracks(id=id)
return [_parse_track(p) for p in data['data']]
def get_artist_albums(self, artid): def get_artist_albums(self, artid):
data = self.api.getArtistAlbums(artid) return self._loopit(artid, self.api.getArtistAlbums, _parse_album, 500)
return [_parse_album(p) for p in data['data']]
def get_favourite_tracks(self, id='me'):
return self._loopit(id, self.api.getUserTracks, _parse_track, 2000)
def get_user_playlist(self, id): def get_user_playlist(self, id):
data = self.api.getUserPlaylist(id) return self._loopit(id, self.api.getUserPlaylist, _parse_track, 2000)
if not data:
return []
return [_parse_track(t) for t in data['data']]
def get_album_tracks(self, albumid): def get_album_tracks(self, albumid):
data = self.api.getAlbum(albumid) data = self.api.getAlbum(albumid)
...@@ -100,7 +103,7 @@ class Session(object): ...@@ -100,7 +103,7 @@ class Session(object):
limit = 200 limit = 200
if tp == 'artist': if tp == 'artist':
limit = 20 limit = 30
elif tp == 'album' or tp == 'playlist': elif tp == 'album' or tp == 'playlist':
limit = 50 limit = 50
offset = 0 offset = 0
...@@ -187,13 +190,34 @@ def _parse_album(data): ...@@ -187,13 +190,34 @@ def _parse_album(data):
def _parse_track(data, albumarg = None): def _parse_track(data, albumarg = None):
artist = Artist(id=data['artist']['id'], name=data['artist']['name'])
if not 'readable' in data or not data['readable'] and \
'alternative' in data:
data = data['alternative']
artid = None
artname = "Unknown"
if 'artist' in data:
dt = data['artist']
if 'id' in dt:
artid = dt['id']
else:
# Happens for not e.g. the beatles in RS 500 playlist
#uplog("_parse_track: artist has no id: %s" % dt)
pass
if 'name' in dt:
artname = dt['name']
artist = Artist(id=artid, name=artname)
kwargs = { kwargs = {
'id': data['id'], 'id': data['id'],
'name': data['title'], 'name': data['title'],
'duration': data['duration'], 'duration': data['duration'],
'artist': artist, 'artist': artist,
# There is a 'readable' attribute in the track data, it's
# sometimes fals,e but this does not seem to actually affect
# the accessibility. Don't know what it means. In any case,
# always set available to true for now.
'available': data['readable'] 'available': data['readable']
} }
...@@ -206,7 +230,11 @@ def _parse_track(data, albumarg = None): ...@@ -206,7 +230,11 @@ def _parse_track(data, albumarg = None):
kwargs['album'] = albumarg kwargs['album'] = albumarg
elif 'album' in data: elif 'album' in data:
alb = data['album'] alb = data['album']
kwargs['album'] = Album(image=alb['cover_big'], name=alb['title']) image = None
for k in ('cover_big', 'cover'):
if k in alb:
image = alb[k]
kwargs['album'] = Album(image=image, name=alb['title'])
# If track has own cover, use it (e.g. for multialbum playlists) # If track has own cover, use it (e.g. for multialbum playlists)
if 'picture_big' in data: if 'picture_big' 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