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.
12 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
13 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
14 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
15 # DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
16 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
17 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
18 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
19 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
20 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
21 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 # An improved, structured jamendo API wrapper for the N900 with cacheing
24 # Image / cover downloads.. and more?
25 import urllib, threading, os, time, simplejson, re
26 import logging, hashlib
27 import pycurl, StringIO
31 _GET2 = '''http://api.jamendo.com/get2/'''
32 _MP3URL = _GET2+'stream/track/redirect/?id=%d&streamencoding=mp31'
33 _OGGURL = _GET2+'stream/track/redirect/?id=%d&streamencoding=ogg2'
34 _TORRENTURL = _GET2+'bittorrent/file/redirect/?album_id=%d&type=archive&class=mp32'
37 log = logging.getLogger(__name__)
39 class StdoutLogger(object):
40 def info(self, s, *args):
42 def debug(self, s, *args):
46 # These classes can be partially constructed,
47 # and if asked for a property they don't know,
48 # makes a query internally to get the full story
50 _ARTIST_FIELDS = ['id', 'name', 'image']
51 _ALBUM_FIELDS = ['id', 'name', 'image', 'artist_name', 'artist_id', 'license_url']
52 _TRACK_FIELDS = ['id', 'name', 'album_image', 'artist_id', 'artist_name', 'album_name', 'album_id', 'numalbum', 'duration']
53 _RADIO_FIELDS = ['id', 'name', 'idstr', 'image']
54 _TAG_FIELDS = ['id', 'name']
58 s = StringIO.StringIO()
59 c.setopt(pycurl.FOLLOWLOCATION, 1)
60 c.setopt(pycurl.URL, url)
61 c.setopt(pycurl.WRITEFUNCTION, s.write)
69 class LazyQuery(object):
70 def set_from_json(self, json):
71 for key, value in json.iteritems():
73 assert(self.ID == int(value))
75 if key.endswith('_id'):
77 setattr(self, key, value)
80 """Not automatic for now,
81 will have to do artist.load()
83 This is filled in further down
88 def _needs_load(self):
91 def _set_from(self, other):
94 def _needs_load_impl(self, *attrs):
96 if getattr(self, attr) is None:
100 def _set_from_impl(self, other, *attrs):
102 self._set_if(other, attr)
104 def _set_if(self, other, attrname):
105 if getattr(self, attrname) is None and getattr(other, attrname) is not None:
106 setattr(self, attrname, getattr(other, attrname))
110 return u"%s(%s)"%(self.__class__.__name__,
111 u", ".join(("%s:%s"%(k,repr(v))) for k,v in self.__dict__.iteritems() if not k.startswith('_')))
112 except UnicodeEncodeError:
114 #traceback.print_exc()
115 return u"%s(?)"%(self.__class__.__name__)
117 class Artist(LazyQuery):
118 def __init__(self, ID, json=None):
122 self.albums = None # None means not downloaded
124 self.set_from_json(json)
126 def _needs_load(self):
127 return self._needs_load_impl('name', 'albums')
129 def _set_from(self, other):
130 return self._set_from_impl(other, 'name', 'image', 'albums')
133 return {'name':self.name, 'image':self.image}
135 class Album(LazyQuery):
136 def __init__(self, ID, json=None):
140 self.artist_name = None
141 self.artist_id = None
142 self.license_url = None
143 self.tracks = None # None means not downloaded
145 self.set_from_json(json)
147 def torrent_url(self):
148 return _TORRENTURL%(self.ID)
151 def _needs_load(self):
152 return self._needs_load_impl('name', 'image', 'artist_name', 'artist_id', 'license_url', 'tracks')
154 def _set_from(self, other):
155 return self._set_from_impl(other, 'name', 'image', 'artist_name', 'artist_id', 'license_url', 'tracks')
158 return {'name':self.name, 'image':self.image,
159 'artist_name':self.artist_name,
160 'artist_id':self.artist_id,
161 'license_url':self.license_url}
163 class Track(LazyQuery):
164 def __init__(self, ID, json=None):
167 self.artist_id = None
168 self.artist_name = None
169 self.album_image = None
170 self.album_name = None
175 self.set_from_json(json)
178 return _MP3URL%(self.ID)
181 return _OGGURL%(self.ID)
184 return {'name':self.name,
185 'artist_id':self.artist_id,
186 'artist_name':self.artist_name,
187 'album_image':self.album_image,
188 'album_name':self.album_name,
189 'album_id':self.album_id,
190 'numalbum':self.numalbum,
191 'duration':self.duration}
193 def _needs_load(self):
194 return self._needs_load_impl('name', 'artist_name', 'artist_id', 'album_name', 'album_id', 'numalbum', 'duration')
196 def _set_from(self, other):
197 return self._set_from_impl(other, 'name', 'album_image', 'artist_name', 'artist_id', 'album_name', 'album_id', 'numalbum', 'duration')
199 class Radio(LazyQuery):
200 def __init__(self, ID, json=None):
206 self.set_from_json(json)
208 def _needs_load(self):
209 return self._needs_load_impl('name', 'idstr', 'image')
211 def _set_from(self, other):
212 return self._set_from_impl(other, 'name', 'idstr', 'image')
214 class Tag(LazyQuery):
215 def __init__(self, ID, json=None):
219 self.set_from_json(json)
221 def _needs_load(self):
222 return self._needs_load_impl('name')
224 def _set_from(self, other):
225 return self._set_from_impl(other, 'name')
227 _artists = {} # id -> Artist()
228 _albums = {} # id -> Album()
229 _tracks = {} # id -> Track()
230 _radios = {} # id -> Radio()
233 # cache sizes per session (TODO)
234 _CACHED_ARTISTS = 100
238 # cache sizes, persistant
239 _CACHED_COVERS = 2048
241 # TODO: cache queries?
243 class Ratelimit(object):
244 rate_limit = 1.0 # seconds between queries
245 last_query = time.time() - 1.5
250 if (now - cls.last_query) < cls.rate_limit:
251 time.sleep(cls.rate_limit - (now - cls.last_query))
252 cls.last_query = time.time()
254 _ratelimit = Ratelimit.ratelimit
260 def _geturl(self, url):
264 ret = simplejson.loads(curlGET(url))
270 return "#%s" % (self.__class__.__name__)
275 class CoverFetcher(threading.Thread):
277 threading.Thread.__init__(self)
279 self.cond = threading.Condition()
282 def _retrieve(self, url, fname):
283 BROKEN = 'http://imgjam.com/radios/default/default.100.png'
286 f = open(fname, 'wb')
288 c.setopt(pycurl.FOLLOWLOCATION, 1)
289 c.setopt(pycurl.URL, str(url))
290 c.setopt(pycurl.WRITEFUNCTION, f.write)
298 log.debug("Coverfetch: %s -> %s", url, fname)
301 def _fetch_cover(self, albumid, size):
303 coverdir = _COVERDIR if _COVERDIR else '/tmp'
304 to = os.path.join(coverdir, '%d-%d.jpg'%(albumid, size))
305 if not os.path.isfile(to):
306 url = _GET2+'image/album/redirect/?id=%d&imagesize=%d'%(albumid, size)
307 to = self._retrieve(url, to)
312 def _fetch_image(self, url):
314 h = hashlib.md5(url).hexdigest()
315 coverdir = _COVERDIR if _COVERDIR else '/tmp'
316 to = os.path.join(coverdir, h+'.jpg')
317 if not os.path.isfile(to):
318 to = self._retrieve(url, to)
323 def request_cover(self, albumid, size, cb):
325 self.work.insert(0, (albumid, size, cb))
329 def request_images(self, urls, cb):
330 """cb([(url, image)])"""
332 self.work = [('image', url, cb) for url in urls] + self.work
348 multi = len(work) > 1
350 if job[0] == 'image':
351 self.process_image(job[1], job[2])
353 self.process_cover(*job)
355 def process_cover(self, albumid, size, cb):
356 cover = self._fetch_cover(albumid, size)
358 cb(albumid, size, cover)
360 def process_image(self, url, cb):
361 image = self._fetch_image(url)
365 class CoverCache(object):
367 cache and fetch covers
368 TODO: background thread that
369 fetches and returns covers,
373 self._covers = {} # (albumid, size) -> file
375 self._fetcher = CoverFetcher()
376 self._fetcher.start()
377 if _COVERDIR and os.path.isdir(_COVERDIR):
380 def prime_cache(self):
382 covermatch = re.compile(r'(\d+)\-(\d+)\.jpg')
384 prev_covers = os.listdir(coverdir)
386 if len(prev_covers) > _CACHED_COVERS:
388 dropn = len(prev_covers) - _CACHED_COVERS
389 todrop = random.sample(prev_covers, dropn)
390 log.warning("Deleting from cache: %s", todrop)
392 m = covermatch.match(d)
395 os.unlink(os.path.join(coverdir, d))
397 log.exception('unlinking failed')
399 for fil in os.listdir(coverdir):
400 fl = os.path.join(coverdir, fil)
401 m = covermatch.match(fil)
402 if m and os.path.isfile(fl):
403 self._covers[(int(m.group(1)), int(m.group(2)))] = fl
405 def fetch_cover(self, albumid, size):
408 to = os.path.join(coverdir, '%d-%d.jpg'%(albumid, size))
409 if not os.path.isfile(to):
410 url = _GET2+'image/album/redirect/?id=%d&imagesize=%d'%(albumid, size)
411 urllib.urlretrieve(url, to)
412 self._covers[(albumid, size)] = to
416 def get_cover(self, albumid, size):
417 cover = self._covers.get((albumid, size), None)
419 cover = self.fetch_cover(albumid, size)
422 def get_async(self, albumid, size, cb):
423 cover = self._covers.get((albumid, size), None)
425 cb(albumid, size, cover)
427 def cb2(albumid, size, cover):
428 self._covers[(albumid, size)] = cover
429 cb(albumid, size, cover)
430 self._fetcher.request_cover(albumid, size, cb2)
432 def get_images_async(self, url_list, cb):
436 image = self._images.get(url, None)
438 found.append((url, image))
446 for url, image in results:
447 self._images[url] = image
449 self._fetcher.request_images(lookup, cb2)
451 _cover_cache = CoverCache()
453 def set_cache_dir(cachedir):
457 _COVERDIR = os.path.join(_CACHEDIR, 'covers')
460 os.makedirs(_CACHEDIR)
465 os.makedirs(_COVERDIR)
469 _cover_cache.prime_cache()
471 def get_album_cover(albumid, size=100):
472 return _cover_cache.get_cover(albumid, size)
474 def get_album_cover_async(cb, albumid, size=100):
475 _cover_cache.get_async(albumid, size, cb)
477 def get_images_async(cb, url_list):
478 _cover_cache.get_images_async(url_list, cb)
480 class CustomQuery(Query):
481 def __init__(self, url):
486 return self._geturl(self.url)
491 class GetQuery(Query):
494 'url' : _GET2+'+'.join(_ARTIST_FIELDS)+'/artist/json/?',
495 'params' : 'artist_id=%d',
496 'constructor' : Artist
499 'url' : _GET2+'+'.join(_ALBUM_FIELDS)+'/artist/json/?',
500 'params' : 'artist_id=%s',
501 'constructor' : Album
504 'url' : _GET2+'+'.join(_ALBUM_FIELDS)+'/album/json/?',
505 'params' : 'album_id=%d',
506 'constructor' : Album
509 'url' : _GET2+'+'.join(_ALBUM_FIELDS)+'/album/json/?',
510 'params' : 'album_id=%s',
511 'constructor' : Album
514 'url' : _GET2+'+'.join(_ALBUM_FIELDS)+'/album/json/?',
515 'params' : 'artist_id=%d',
516 'constructor' : [Album]
519 'url' : _GET2+'+'.join(_TRACK_FIELDS)+'/track/json/track_album+album_artist?',
521 'constructor' : Track
524 'url' : _GET2+'+'.join(_TRACK_FIELDS)+'/track/json/track_album+album_artist?',
526 'constructor' : Track
529 'url' : _GET2+'+'.join(_TRACK_FIELDS)+'/track/json/track_album+album_artist?',
530 'params' : 'order=numalbum_asc&album_id=%d',
531 'constructor' : [Track]
534 'url' : _GET2+'+'.join(_TRACK_FIELDS)+'/track/json/radio_track_inradioplaylist+track_album+album_artist/?',
535 'params' : 'order=random_asc&radio_id=%d&n=16',
536 'constructor' : [Track]
538 'favorite_albums' : {
539 'url' : _GET2+'+'.join(_ALBUM_FIELDS)+'/album/json/album_user_starred/?',
540 'params' : 'user_idstr=%s',
541 'constructor' : [Album]
544 'url' : _GET2+'+'.join(_TRACK_FIELDS)+'/track/json/track_album+album_artist?',
545 'params' : 'tag_id=%d&n=50&order=rating_desc',
546 'constructor' : [Track]
550 def __init__(self, what, ID):
553 info = GetQuery.queries[what]
554 self.url = info['url']
555 self.params = info['params']
556 self.constructor = info['constructor']
558 def construct(self, data):
559 constructor = self.constructor
560 if isinstance(constructor, list):
561 constructor = constructor[0]
562 if isinstance(data, list):
563 return [constructor(int(x['id']), json=x) for x in data]
565 return constructor(int(data['id']), json=data)
568 js = self._geturl(self.url + self.params % (self.ID))
571 return self.construct(js)
574 return self.url + self.params % (self.ID)
576 class SearchQuery(GetQuery):
577 def __init__(self, what, query=None, order=None, user=None, count=20):
578 GetQuery.__init__(self, what, None)
587 params['searchquery'] = self.query
589 params['order'] = self.order
591 params['n'] = self.count
593 params['user_idstr'] = self.user
594 js = self._geturl(self.url + urllib.urlencode(params))
597 return self.construct(js)
600 params = {'searchquery':self.query, 'order':self.order, 'n':self.count}
601 return self.url + urllib.urlencode(params)
603 class JamendoAPIException(Exception):
604 def __init__(self, url):
605 Exception.__init__(self, url)
607 def _update_cache(cache, new_items):
608 if not isinstance(new_items, list):
609 new_items = [new_items]
610 for item in new_items:
611 old = cache.get(item.ID)
615 cache[item.ID] = item
616 if isinstance(item, Artist) and item.albums:
617 for album in item.albums:
618 _update_cache(_albums, album)
619 elif isinstance(item, Album) and item.tracks:
620 for track in item.tracks:
621 _update_cache(_tracks, track)
622 # enforce cache limits here!
623 # also, TODO: save/load cache between sessions
624 # that will require storing a timestamp with
625 # each item, though..
627 # artists: 1 day - changes often
628 # albums: 2-5 days - changes less often (?)
629 # tracks: 1 week - changes rarely, queried often
631 def get_artist(artist_id):
632 """Returns: Artist"""
633 a = _artists.get(artist_id, None)
635 q = GetQuery('artist', artist_id)
638 raise JamendoAPIException(str(q))
639 _update_cache(_artists, a)
640 if isinstance(a, list):
644 def get_artists(artist_ids):
645 """Returns: [Artist]"""
646 assert(isinstance(artist_ids, list))
649 for artist_id in artist_ids:
650 a = _artists.get(artist_id, None)
652 lookup.append(artist_id)
656 q = GetQuery('artist_list', '+'.join(str(x) for x in lookup))
659 raise JamendoAPIException(str(q))
660 _update_cache(_artists, a)
662 return found + lookup
664 def get_album_list(album_ids):
665 """Returns: [Album]"""
666 assert(isinstance(album_ids, list))
669 for album_id in album_ids:
670 a = _albums.get(album_id, None)
672 lookup.append(album_id)
676 q = GetQuery('album_list', '+'.join(str(x) for x in lookup))
679 raise JamendoAPIException(str(q))
680 _update_cache(_albums, a)
682 return found + lookup
684 def get_albums(artist_id):
686 Parameter can either be an artist_id or a list of album ids.
688 if isinstance(artist_id, list):
689 return get_album_list(artist_id)
690 a = _artists.get(artist_id, None)
694 q = GetQuery('albums', artist_id)
697 raise JamendoAPIException(str(q))
698 _update_cache(_albums, a)
701 def get_album(album_id):
703 a = _albums.get(album_id, None)
705 q = GetQuery('album', album_id)
708 raise JamendoAPIException(str(q))
709 _update_cache(_albums, a)
710 if isinstance(a, list):
714 def get_track_list(track_ids):
715 """Returns: [Track]"""
716 assert(isinstance(track_ids, list))
719 for track_id in track_ids:
720 a = _tracks.get(track_id, None)
722 lookup.append(track_id)
726 q = GetQuery('track_list', '+'.join(str(x) for x in lookup))
729 raise JamendoAPIException(str(q))
730 _update_cache(_tracks, a)
732 return found + lookup
734 def get_tracks(album_id):
736 Parameter can either be an album_id or a list of track ids.
738 if isinstance(album_id, list):
739 return get_track_list(album_id)
740 a = _albums.get(album_id, None)
744 q = GetQuery('tracks', album_id)
747 raise JamendoAPIException(str(q))
748 _update_cache(_tracks, a)
751 def get_track(track_id):
753 a = _tracks.get(track_id, None)
755 q = GetQuery('track', track_id)
758 raise JamendoAPIException(str(q))
759 _update_cache(_tracks, a)
760 if isinstance(a, list):
764 def get_radio_tracks(radio_id):
765 """Returns: [Track]"""
766 q = GetQuery('radio', radio_id)
769 raise JamendoAPIException(str(q))
770 _update_cache(_tracks, a)
773 #http://api.jamendo.com/get2/id+name/track/plain/?tag_id=327&n=50&order=rating_desc
774 def get_tag_tracks(tag_id):
775 """Returns: [Track]"""
776 q = GetQuery('tag', tag_id)
779 raise JamendoAPIException(str(q))
780 _update_cache(_tracks, a)
783 def search_artists(query):
784 """Returns: [Artist]"""
785 q = SearchQuery('artist', query, 'searchweight_desc')
788 raise JamendoAPIException(str(q))
789 _update_cache(_artists, a)
792 def search_albums(query):
793 """Returns: [Album]"""
794 q = SearchQuery('album', query, 'searchweight_desc')
797 raise JamendoAPIException(str(q))
798 _update_cache(_albums, a)
801 def search_tracks(query):
802 """Returns: [Track]"""
803 q = SearchQuery('track', query=query, order='searchweight_desc')
806 raise JamendoAPIException(str(q))
807 _update_cache(_tracks, a)
810 def albums_of_the_week():
811 """Returns: [Album]"""
812 q = SearchQuery('album', order='ratingweek_desc')
815 raise JamendoAPIException(str(q))
816 _update_cache(_albums, a)
820 """Returns: [Track] (playlist)"""
821 q = SearchQuery('track', order='releasedate_desc')
824 raise JamendoAPIException(str(q))
825 _update_cache(_tracks, a)
828 def tracks_of_the_week():
829 """Returns: [Track] (playlist)"""
830 q = SearchQuery('track', order='ratingweek_desc')
833 raise JamendoAPIException(str(q))
834 _update_cache(_tracks, a)
837 def top_artists(order='rating_desc', count=20):
838 """Returns: [Artist]"""
839 q = SearchQuery('artist', order=order, count=count)
842 raise JamendoAPIException(str(q))
843 _update_cache(_artists, a)
846 def top_albums(order='rating_desc', count=20):
847 """Returns: [Album]"""
848 q = SearchQuery('album', order=order, count=count)
851 raise JamendoAPIException(str(q))
852 _update_cache(_albums, a)
855 def top_tracks(order='rating_desc', count=20):
856 """Returns: [Track]"""
857 q = SearchQuery('track', order=order, count=count)
860 raise JamendoAPIException(str(q))
861 _update_cache(_tracks, a)
864 def get_radio(radio_id):
866 q = CustomQuery(_GET2+"id+name+idstr+image/radio/json?id=%d"%(radio_id))
869 raise JamendoAPIException(str(q))
870 if isinstance(js, list):
872 return Radio(radio_id, json=js)
874 def starred_radios():
875 """Returns: [Radio]"""
876 q = CustomQuery(_GET2+"id+name+idstr+image/radio/json?order=starred_desc")
879 raise JamendoAPIException(str(q))
880 return [Radio(int(radio['id']), json=radio) for radio in js]
882 def top_tags(count=50, order='rating_desc'):
884 q = CustomQuery(_GET2+"id+name/tag/json?n=%d&order=%s"%(count, order))
887 raise JamendoAPIException(str(q))
888 return [Tag(int(tag['id']), json=tag) for tag in js]
890 def favorite_albums(user):
891 """Returns: [Album]"""
892 q = SearchQuery('favorite_albums', user=user, count=20)
895 raise JamendoAPIException(str(q))
896 _update_cache(_albums, a)
899 ### Set loader functions for classes
901 def _artist_loader(self):
902 if self._needs_load():
903 artist = get_artist(self.ID)
904 artist.albums = get_albums(self.ID)
905 self._set_from(artist)
906 Artist.load = _artist_loader
908 def _album_loader(self):
909 if self._needs_load():
910 album = get_album(self.ID)
911 album.tracks = get_tracks(self.ID)
912 self._set_from(album)
913 Album.load = _album_loader
915 def _track_loader(self):
916 track = get_track(self.ID)
917 self._set_from(track)
918 Track.load = _track_loader
920 def _radio_loader(self):
921 radio = get_radio(self.ID)
922 self._set_from(radio)
923 Radio.load = _radio_loader