+ def get_cover(self, albumid, size):
+ cover = self._covers.get((albumid, size), None)
+ if not cover:
+ cover = self.fetch_cover(albumid, size)
+ return cover
+
+ def get_async(self, albumid, size, cb):
+ cover = self._covers.get((albumid, size), None)
+ if cover:
+ cb(cover)
+ else:
+ # TODO
+ cover = self.fetch_cover(albumid, size)
+ cb(cover)
+
+_cover_cache = CoverCache()
+
+def get_album_cover(albumid, size=200):
+ return _cover_cache.get_cover(albumid, size)
+
+def get_album_cover_async(cb, albumid, size=200):
+ _cover_cache.get_async(albumid, size, cb)
+
+class CustomQuery(Query):
+ def __init__(self, url):
+ Query.__init__(self)
+ self.url = url
+
+ def execute(self):
+ return self._geturl(self.url)
+
+ def __str__(self):
+ return self.url
+
+class GetQuery(Query):
+ queries = {
+ 'artist' : {
+ 'url' : _GET2+'+'.join(_ARTIST_FIELDS)+'/artist/json/?',
+ 'params' : 'artist_id=%d',
+ 'constructor' : Artist
+ },
+ 'album' : {
+ 'url' : _GET2+'+'.join(_ALBUM_FIELDS)+'/album/json/?',
+ 'params' : 'album_id=%d',
+ 'constructor' : Album
+ },
+ 'albums' : {
+ 'url' : _GET2+'+'.join(_ALBUM_FIELDS)+'/album/json/?',
+ 'params' : 'artist_id=%d',
+ 'constructor' : [Album]
+ },
+ 'track' : {
+ 'url' : _GET2+'+'.join(_TRACK_FIELDS)+'/track/json/track_album+album_artist?',
+ 'params' : 'id=%d',
+ 'constructor' : Track
+ },
+ 'tracks' : {
+ 'url' : _GET2+'+'.join(_TRACK_FIELDS)+'/track/json/track_album+album_artist?',
+ 'params' : 'order=numalbum_asc&album_id=%d',
+ 'constructor' : [Track]
+ },
+ 'radio' : {
+ 'url' : _GET2+'+'.join(_TRACK_FIELDS)+'/track/json/radio_track_inradioplaylist+track_album+album_artist/?',
+ 'params' : 'order=random_asc&radio_id=%d',
+ 'constructor' : [Track]
+ },
+ 'favorite_albums' : {
+ 'url' : _GET2+'+'.join(_ALBUM_FIELDS)+'/album/json/album_user_starred/?',
+ 'params' : 'user_idstr=%s',
+ 'constructor' : [Album]
+ },
+ #http://api.jamendo.com/get2/id+name+url+image+artist_name/album/jsonpretty/album_user_starred/?user_idstr=sylvinus&n=all
+ #q = SearchQuery('album', user_idstr=user)
+
+ }
+#http://api.jamendo.com/get2/id+name+image+artist_name+album_name+album_id+numalbum+duration/track/json/radio_track_inradioplaylist+track_album+album_artist/?order=numradio_asc&radio_id=283
+
+ def __init__(self, what, ID):
+ Query.__init__(self)
+ self.ID = ID
+ info = GetQuery.queries[what]
+ self.url = info['url']
+ self.params = info['params']
+ self.constructor = info['constructor']
+
+ def construct(self, data):
+ constructor = self.constructor
+ if isinstance(constructor, list):
+ constructor = constructor[0]
+ if isinstance(data, list):
+ return [constructor(int(x['id']), json=x) for x in data]
+ else:
+ return constructor(int(data['id']), json=data)
+
+ def execute(self):
+ js = self._geturl(self.url + self.params % (self.ID))
+ if not js:
+ return None
+ return self.construct(js)
+
+ def __str__(self):
+ return self.url + self.params % (self.ID)
+
+class SearchQuery(GetQuery):
+ def __init__(self, what, query=None, order=None, user=None, count=10):
+ GetQuery.__init__(self, what, None)
+ self.query = query
+ self.order = order
+ self.count = count
+ self.user = user
+
+ def execute(self):
+ params = {}
+ if self.query:
+ params['searchquery'] = self.query
+ if self.order:
+ params['order'] = self.order
+ if self.count:
+ params['n'] = self.count
+ if self.user:
+ params['user_idstr'] = self.user
+ js = self._geturl(self.url + urllib.urlencode(params))
+ if not js:
+ return None
+ return self.construct(js)
+
+ def __str__(self):
+ params = {'searchquery':self.query, 'order':self.order, 'n':self.count}
+ return self.url + urllib.urlencode(params)
+
+class JamendoAPIException(Exception):
+ def __init__(self, url):
+ Exception.__init__(self, url)
+
+def _update_cache(cache, new_items):
+ if not isinstance(new_items, list):
+ new_items = [new_items]
+ for item in new_items:
+ old = cache.get(item.ID)
+ if old:
+ old._set_from(item)
+ else:
+ cache[item.ID] = item
+
+def get_artist(artist_id):
+ """Returns: Artist"""
+ a = _artists.get(artist_id, None)
+ if not a:
+ q = GetQuery('artist', artist_id)
+ a = q.execute()
+ if not a:
+ raise JamendoAPIException(str(q))
+ _update_cache(_artists, a)
+ if isinstance(a, list):
+ a = a[0]
+ return a
+
+def get_albums(artist_id):
+ """Returns: [Album]"""
+ q = GetQuery('albums', artist_id)
+ a = q.execute()
+ if not a:
+ raise JamendoAPIException(str(q))
+ _update_cache(_artists, a)
+ return a
+
+def get_album(album_id):
+ """Returns: Album"""
+ a = _albums.get(album_id, None)
+ if not a:
+ q = GetQuery('album', album_id)
+ a = q.execute()
+ if not a:
+ raise JamendoAPIException(str(q))
+ _update_cache(_albums, a)
+ if isinstance(a, list):
+ a = a[0]
+ return a
+
+def get_tracks(album_id):
+ """Returns: [Track]"""
+ q = GetQuery('tracks', album_id)
+ a = q.execute()
+ if not a:
+ raise JamendoAPIException(str(q))
+ _update_cache(_tracks, a)
+ return a
+
+def get_track(track_id):
+ """Returns: Track"""
+ a = _tracks.get(track_id, None)
+ if not a:
+ q = GetQuery('track', track_id)
+ a = q.execute()
+ if not a:
+ raise JamendoAPIException(str(q))
+ _update_cache(_tracks, a)
+ if isinstance(a, list):
+ a = a[0]
+ return a
+
+def get_radio_tracks(radio_id):
+ """Returns: [Track]"""
+ q = GetQuery('radio', radio_id)
+ a = q.execute()
+ if not a:
+ raise JamendoAPIException(str(q))
+ _update_cache(_tracks, a)
+ return a
+
+def search_artists(query):
+ """Returns: [Artist]"""
+ q = SearchQuery('artist', query, 'searchweight_desc')
+ a = q.execute()
+ if not a:
+ raise JamendoAPIException(str(q))
+ _update_cache(_artists, a)
+ return a
+
+def search_albums(query):
+ """Returns: [Album]"""
+ q = SearchQuery('album', query, 'searchweight_desc')
+ a = q.execute()
+ if not a:
+ raise JamendoAPIException(str(q))
+ _update_cache(_albums, a)
+ return a
+
+def search_tracks(query):
+ """Returns: [Track]"""
+ q = SearchQuery('track', query=query, order='searchweight_desc')
+ a = q.execute()
+ if not a:
+ raise JamendoAPIException(str(q))
+ _update_cache(_tracks, a)
+ return a
+
+def albums_of_the_week():
+ """Returns: [Album]"""
+ q = SearchQuery('album', order='ratingweek_desc')
+ a = q.execute()
+ if not a:
+ raise JamendoAPIException(str(q))
+ _update_cache(_albums, a)
+ return a
+
+def new_releases():
+ """Returns: [Track] (playlist)"""
+ q = SearchQuery('track', order='releasedate_desc')
+ a = q.execute()
+ if not a:
+ raise JamendoAPIException(str(q))
+ _update_cache(_tracks, a)
+ return a
+
+def tracks_of_the_week():
+ """Returns: [Track] (playlist)"""
+ q = SearchQuery('track', order='ratingweek_desc')
+ a = q.execute()
+ if not a:
+ raise JamendoAPIException(str(q))
+ _update_cache(_tracks, a)
+ return a
+
+def get_radio(radio_id):
+ """Returns: Radio"""
+ q = CustomQuery(_GET2+"id+name+idstr+image/radio/json?id=%d"%(radio_id))
+ js = q.execute()
+ if not js:
+ raise JamendoAPIException(str(q))
+ if isinstance(js, list):
+ ks = js[0]
+ return Radio(radio_id, json=js)
+
+def starred_radios():
+ """Returns: [Radio]"""
+ q = CustomQuery(_GET2+"id+name+idstr+image/radio/json?order=starred_desc")
+ js = q.execute()
+ if not js:
+ raise JamendoAPIException(str(q))
+ return [Radio(int(radio['id']), json=radio) for radio in js]
+
+def favorite_albums(user):
+ """Returns: [Album]"""
+ q = SearchQuery('favorite_albums', user=user, count=20)
+ a = q.execute()
+ if not a:
+ raise JamendoAPIException(str(q))
+ _update_cache(_albums, a)
+ return a
+
+### Set loader functions for classes
+
+def _artist_loader(self):
+ if self._needs_load():
+ artist = get_artist(self.ID)
+ self._set_from(artist)
+Artist.load = _artist_loader
+
+def _album_loader(self):
+ if self._needs_load():
+ album = get_album(self.ID)
+ album.tracks = get_tracks(self.ID)
+ self._set_from(album)
+Album.load = _album_loader
+
+def _track_loader(self):
+ track = get_track(self.ID)
+ self._set_from(track)
+Track.load = _track_loader
+
+def _radio_loader(self):
+ radio = get_radio(self.ID)
+ self._set_from(radio)
+Radio.load = _radio_loader