3 # This file is part of Jamaendo.
4 # Copyright (c) 2010, Kristoffer Gronlund
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions are met:
9 # * Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # * Redistributions in binary form must reproduce the above copyright
12 # notice, this list of conditions and the following disclaimer in the
13 # documentation and/or other materials provided with the distribution.
14 # * Neither the name of Jamaendo nor the
15 # names of its contributors may be used to endorse or promote products
16 # derived from this software without specific prior written permission.
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 # DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
22 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 # An improved, structured jamendo API wrapper for the N900 with cacheing
30 # Image / cover downloads.. and more?
31 import urllib, threading, os, gzip, time, simplejson, re
35 _GET2 = '''http://api.jamendo.com/get2/'''
36 _MP3URL = _GET2+'stream/track/redirect/?id=%d&streamencoding=mp31'
37 _OGGURL = _GET2+'stream/track/redirect/?id=%d&streamencoding=ogg2'
38 _TORRENTURL = _GET2+'bittorrent/file/redirect/?album_id=%d&type=archive&class=mp32'
40 def set_cache_dir(cachedir):
44 _COVERDIR = os.path.join(_CACHEDIR, 'covers')
47 os.makedirs(_CACHEDIR)
52 os.makedirs(_COVERDIR)
56 # These classes can be partially constructed,
57 # and if asked for a property they don't know,
58 # makes a query internally to get the full story
60 _ARTIST_FIELDS = ['id', 'name', 'image']
61 _ALBUM_FIELDS = ['id', 'name', 'image', 'artist_name', 'artist_id', 'license_url']
62 _TRACK_FIELDS = ['id', 'name', 'image', 'artist_id', 'artist_name', 'album_name', 'album_id', 'numalbum', 'duration']
63 _RADIO_FIELDS = ['id', 'name', 'idstr', 'image']
65 class LazyQuery(object):
66 def set_from_json(self, json):
67 for key, value in json.iteritems():
69 assert(self.ID == int(value))
71 if key.endswith('_id'):
73 setattr(self, key, value)
76 """Not automatic for now,
77 will have to do artist.load()
79 This is filled in further down
84 def _needs_load(self):
87 def _set_from(self, other):
90 def _needs_load_impl(self, *attrs):
92 if getattr(self, attr) is None:
96 def _set_from_impl(self, other, *attrs):
98 self._set_if(other, attr)
100 def _set_if(self, other, attrname):
101 if getattr(self, attrname) is None and getattr(other, attrname) is not None:
102 setattr(self, attrname, getattr(other, attrname))
106 return u"%s(%s)"%(self.__class__.__name__,
107 u", ".join(repr(v) for k,v in self.__dict__.iteritems() if not k.startswith('_')))
108 except UnicodeEncodeError:
110 #traceback.print_exc()
111 return u"%s(?)"%(self.__class__.__name__)
113 class Artist(LazyQuery):
114 def __init__(self, ID, json=None):
118 self.albums = None # None means not downloaded
120 self.set_from_json(json)
122 def _needs_load(self):
123 return self._needs_load_impl('name', 'albums')
125 def _set_from(self, other):
126 return self._set_from_impl(other, 'name', 'image', 'albums')
128 class Album(LazyQuery):
129 def __init__(self, ID, json=None):
133 self.artist_name = None
134 self.artist_id = None
135 self.license_url = None
136 self.tracks = None # None means not downloaded
138 self.set_from_json(json)
140 def torrent_url(self):
141 return _TORRENTURL%(self.ID)
144 def _needs_load(self):
145 return self._needs_load_impl('name', 'image', 'artist_name', 'artist_id', 'license_url', 'tracks')
147 def _set_from(self, other):
148 return self._set_from_impl(other, 'name', 'image', 'artist_name', 'artist_id', 'license_url', 'tracks')
150 class Track(LazyQuery):
151 def __init__(self, ID, json=None):
155 self.artist_id = None
156 self.artist_name = None
157 self.album_name = None
162 self.set_from_json(json)
165 return _MP3URL%(self.ID)
168 return _OGGURL%(self.ID)
170 def _needs_load(self):
171 return self._needs_load_impl('name', 'artist_name', 'artist_id', 'album_name', 'album_id', 'numalbum', 'duration')
173 def _set_from(self, other):
174 return self._set_from_impl(other, 'name', 'image', 'artist_name', 'artist_id', 'album_name', 'album_id', 'numalbum', 'duration')
176 class Radio(LazyQuery):
177 def __init__(self, ID, json=None):
183 self.set_from_json(json)
185 def _needs_load(self):
186 return self._needs_load_impl('name', 'idstr', 'image')
188 def _set_from(self, other):
189 return self._set_from_impl(other, 'name', 'idstr', 'image')
192 _artists = {} # id -> Artist()
193 _albums = {} # id -> Album()
194 _tracks = {} # id -> Track()
195 _radios = {} # id -> Radio()
198 # cache sizes per session (TODO)
199 _CACHED_ARTISTS = 100
204 # TODO: cache queries?
207 rate_limit = 1.1 # seconds between queries
208 last_query = time.time() - 1.5
213 if now - cls.last_query < cls.rate_limit:
214 time.sleep(cls.rate_limit - (now - cls.last_query))
220 def _geturl(self, url):
221 print "*** %s" % (url)
224 f = urllib.urlopen(url)
225 ret = simplejson.load(f)
232 return "#%s" % (self.__class__.__name__)
239 class CoverFetcher(threading.Thread):
241 threading.Thread.__init__(self)
243 self.cond = threading.Condition()
246 def _fetch_cover(self, albumid, size):
248 coverdir = _COVERDIR if _COVERDIR else '/tmp'
249 to = os.path.join(coverdir, '%d-%d.jpg'%(albumid, size))
250 if not os.path.isfile(to):
251 url = _GET2+'image/album/redirect/?id=%d&imagesize=%d'%(albumid, size)
252 urllib.urlretrieve(url, to)
257 def request_cover(self, albumid, size, cb):
259 self.work.insert(0, (albumid, size, cb))
275 multi = len(work) > 1
276 for albumid, size, cb in work:
277 cover = self._fetch_cover(albumid, size)
279 cb(albumid, size, cover)
283 class CoverCache(object):
285 cache and fetch covers
286 TODO: background thread that
287 fetches and returns covers,
291 self._covers = {} # (albumid, size) -> file
292 coverdir = _COVERDIR if _COVERDIR else '/tmp'
293 if os.path.isdir(coverdir):
294 covermatch = re.compile(r'(\d+)\-(\d+)\.jpg')
295 for fil in os.listdir(coverdir):
296 fl = os.path.join(coverdir, fil)
297 m = covermatch.match(fil)
298 if m and os.path.isfile(fl):
299 self._covers[(int(m.group(1)), int(m.group(2)))] = fl
300 self._fetcher = CoverFetcher()
301 self._fetcher.start()
303 def fetch_cover(self, albumid, size):
304 coverdir = _COVERDIR if _COVERDIR else '/tmp'
305 to = os.path.join(coverdir, '%d-%d.jpg'%(albumid, size))
306 if not os.path.isfile(to):
307 url = _GET2+'image/album/redirect/?id=%d&imagesize=%d'%(albumid, size)
308 urllib.urlretrieve(url, to)
309 self._covers[(albumid, size)] = to
312 def get_cover(self, albumid, size):
313 cover = self._covers.get((albumid, size), None)
315 cover = self.fetch_cover(albumid, size)
318 def get_async(self, albumid, size, cb):
319 cover = self._covers.get((albumid, size), None)
321 cb(albumid, size, cover)
323 self._fetcher.request_cover(albumid, size, cb)
325 _cover_cache = CoverCache()
327 def get_album_cover(albumid, size=100):
328 return _cover_cache.get_cover(albumid, size)
330 def get_album_cover_async(cb, albumid, size=100):
331 _cover_cache.get_async(albumid, size, cb)
333 class CustomQuery(Query):
334 def __init__(self, url):
339 return self._geturl(self.url)
344 class GetQuery(Query):
347 'url' : _GET2+'+'.join(_ARTIST_FIELDS)+'/artist/json/?',
348 'params' : 'artist_id=%d',
349 'constructor' : Artist
352 'url' : _GET2+'+'.join(_ALBUM_FIELDS)+'/artist/json/?',
353 'params' : 'artist_id=%s',
354 'constructor' : Album
357 'url' : _GET2+'+'.join(_ALBUM_FIELDS)+'/album/json/?',
358 'params' : 'album_id=%d',
359 'constructor' : Album
362 'url' : _GET2+'+'.join(_ALBUM_FIELDS)+'/album/json/?',
363 'params' : 'album_id=%s',
364 'constructor' : Album
367 'url' : _GET2+'+'.join(_ALBUM_FIELDS)+'/album/json/?',
368 'params' : 'artist_id=%d',
369 'constructor' : [Album]
372 'url' : _GET2+'+'.join(_TRACK_FIELDS)+'/track/json/track_album+album_artist?',
374 'constructor' : Track
377 'url' : _GET2+'+'.join(_TRACK_FIELDS)+'/track/json/track_album+album_artist?',
379 'constructor' : Track
382 'url' : _GET2+'+'.join(_TRACK_FIELDS)+'/track/json/track_album+album_artist?',
383 'params' : 'order=numalbum_asc&album_id=%d',
384 'constructor' : [Track]
387 'url' : _GET2+'+'.join(_TRACK_FIELDS)+'/track/json/radio_track_inradioplaylist+track_album+album_artist/?',
388 'params' : 'order=random_asc&radio_id=%d',
389 'constructor' : [Track]
391 'favorite_albums' : {
392 'url' : _GET2+'+'.join(_ALBUM_FIELDS)+'/album/json/album_user_starred/?',
393 'params' : 'user_idstr=%s',
394 'constructor' : [Album]
396 #http://api.jamendo.com/get2/id+name+url+image+artist_name/album/jsonpretty/album_user_starred/?user_idstr=sylvinus&n=all
397 #q = SearchQuery('album', user_idstr=user)
400 #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
402 def __init__(self, what, ID):
405 info = GetQuery.queries[what]
406 self.url = info['url']
407 self.params = info['params']
408 self.constructor = info['constructor']
410 def construct(self, data):
411 constructor = self.constructor
412 if isinstance(constructor, list):
413 constructor = constructor[0]
414 if isinstance(data, list):
415 return [constructor(int(x['id']), json=x) for x in data]
417 return constructor(int(data['id']), json=data)
420 js = self._geturl(self.url + self.params % (self.ID))
423 return self.construct(js)
426 return self.url + self.params % (self.ID)
428 class SearchQuery(GetQuery):
429 def __init__(self, what, query=None, order=None, user=None, count=10):
430 GetQuery.__init__(self, what, None)
439 params['searchquery'] = self.query
441 params['order'] = self.order
443 params['n'] = self.count
445 params['user_idstr'] = self.user
446 js = self._geturl(self.url + urllib.urlencode(params))
449 return self.construct(js)
452 params = {'searchquery':self.query, 'order':self.order, 'n':self.count}
453 return self.url + urllib.urlencode(params)
455 class JamendoAPIException(Exception):
456 def __init__(self, url):
457 Exception.__init__(self, url)
459 def _update_cache(cache, new_items):
460 if not isinstance(new_items, list):
461 new_items = [new_items]
462 for item in new_items:
463 old = cache.get(item.ID)
467 cache[item.ID] = item
468 if isinstance(item, Artist) and item.albums:
469 for album in item.albums:
470 _update_cache(_albums, album)
471 elif isinstance(item, Album) and item.tracks:
472 for track in item.tracks:
473 _update_cache(_tracks, track)
475 def get_artist(artist_id):
476 """Returns: Artist"""
477 a = _artists.get(artist_id, None)
479 q = GetQuery('artist', artist_id)
482 raise JamendoAPIException(str(q))
483 _update_cache(_artists, a)
484 if isinstance(a, list):
488 def get_artists(artist_ids):
489 """Returns: [Artist]"""
490 assert(isinstance(artist_ids, list))
493 for artist_id in artist_ids:
494 a = _artists.get(artist_id, None)
496 lookup.append(artist_id)
500 q = GetQuery('artist_list', '+'.join(str(x) for x in lookup))
503 raise JamendoAPIException(str(q))
504 _update_cache(_artists, a)
506 return found + lookup
508 def get_album_list(album_ids):
509 """Returns: [Album]"""
510 assert(isinstance(album_ids, list))
513 for album_id in album_ids:
514 a = _albums.get(album_id, None)
516 lookup.append(album_id)
520 q = GetQuery('album_list', '+'.join(str(x) for x in lookup))
523 raise JamendoAPIException(str(q))
524 _update_cache(_albums, a)
526 return found + lookup
528 def get_albums(artist_id):
530 Parameter can either be an artist_id or a list of album ids.
532 if isinstance(artist_id, list):
533 return get_album_list(artist_id)
534 a = _artists.get(artist_id, None)
538 q = GetQuery('albums', artist_id)
541 raise JamendoAPIException(str(q))
542 _update_cache(_artists, a)
545 def get_album(album_id):
547 a = _albums.get(album_id, None)
549 q = GetQuery('album', album_id)
552 raise JamendoAPIException(str(q))
553 _update_cache(_albums, a)
554 if isinstance(a, list):
558 def get_track_list(track_ids):
559 """Returns: [Track]"""
560 assert(isinstance(track_ids, list))
563 for track_id in track_ids:
564 a = _tracks.get(track_id, None)
566 lookup.append(track_id)
570 q = GetQuery('track_list', '+'.join(str(x) for x in lookup))
573 raise JamendoAPIException(str(q))
574 _update_cache(_tracks, a)
576 return found + lookup
578 def get_tracks(album_id):
580 Parameter can either be an album_id or a list of track ids.
582 if isinstance(album_id, list):
583 return get_track_list(album_id)
584 a = _albums.get(album_id, None)
588 q = GetQuery('tracks', album_id)
591 raise JamendoAPIException(str(q))
592 _update_cache(_tracks, a)
595 def get_track(track_id):
597 a = _tracks.get(track_id, None)
599 q = GetQuery('track', track_id)
602 raise JamendoAPIException(str(q))
603 _update_cache(_tracks, a)
604 if isinstance(a, list):
608 def get_radio_tracks(radio_id):
609 """Returns: [Track]"""
610 q = GetQuery('radio', radio_id)
613 raise JamendoAPIException(str(q))
614 _update_cache(_tracks, a)
617 def search_artists(query):
618 """Returns: [Artist]"""
619 q = SearchQuery('artist', query, 'searchweight_desc')
622 raise JamendoAPIException(str(q))
623 _update_cache(_artists, a)
626 def search_albums(query):
627 """Returns: [Album]"""
628 q = SearchQuery('album', query, 'searchweight_desc')
631 raise JamendoAPIException(str(q))
632 _update_cache(_albums, a)
635 def search_tracks(query):
636 """Returns: [Track]"""
637 q = SearchQuery('track', query=query, order='searchweight_desc')
640 raise JamendoAPIException(str(q))
641 _update_cache(_tracks, a)
644 def albums_of_the_week():
645 """Returns: [Album]"""
646 q = SearchQuery('album', order='ratingweek_desc')
649 raise JamendoAPIException(str(q))
650 _update_cache(_albums, a)
654 """Returns: [Track] (playlist)"""
655 q = SearchQuery('track', order='releasedate_desc')
658 raise JamendoAPIException(str(q))
659 _update_cache(_tracks, a)
662 def tracks_of_the_week():
663 """Returns: [Track] (playlist)"""
664 q = SearchQuery('track', order='ratingweek_desc')
667 raise JamendoAPIException(str(q))
668 _update_cache(_tracks, a)
671 def get_radio(radio_id):
673 q = CustomQuery(_GET2+"id+name+idstr+image/radio/json?id=%d"%(radio_id))
676 raise JamendoAPIException(str(q))
677 if isinstance(js, list):
679 return Radio(radio_id, json=js)
681 def starred_radios():
682 """Returns: [Radio]"""
683 q = CustomQuery(_GET2+"id+name+idstr+image/radio/json?order=starred_desc")
686 raise JamendoAPIException(str(q))
687 return [Radio(int(radio['id']), json=radio) for radio in js]
689 def favorite_albums(user):
690 """Returns: [Album]"""
691 q = SearchQuery('favorite_albums', user=user, count=20)
694 raise JamendoAPIException(str(q))
695 _update_cache(_albums, a)
698 ### Set loader functions for classes
700 def _artist_loader(self):
701 if self._needs_load():
702 artist = get_artist(self.ID)
703 artist.albums = get_albums(self.ID)
704 self._set_from(artist)
705 Artist.load = _artist_loader
707 def _album_loader(self):
708 if self._needs_load():
709 album = get_album(self.ID)
710 album.tracks = get_tracks(self.ID)
711 self._set_from(album)
712 Album.load = _album_loader
714 def _track_loader(self):
715 track = get_track(self.ID)
716 self._set_from(track)
717 Track.load = _track_loader
719 def _radio_loader(self):
720 radio = get_radio(self.ID)
721 self._set_from(radio)
722 Radio.load = _radio_loader