hmm, asynchronous api is nontrivial
[jamaendo] / jamaendo / api.py
index 8bd7d91..d026770 100644 (file)
@@ -1,14 +1,37 @@
-# An improved, structured jamendo API for the N900 with cacheing
+#!/usr/bin/env python
+#
+# This file is part of Jamaendo.
+# Copyright (c) 2010, Kristoffer Gronlund
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are met:
+#     * Redistributions of source code must retain the above copyright
+#       notice, this list of conditions and the following disclaimer.
+#     * Redistributions in binary form must reproduce the above copyright
+#       notice, this list of conditions and the following disclaimer in the
+#       documentation and/or other materials provided with the distribution.
+#     * Neither the name of Jamaendo nor the
+#       names of its contributors may be used to endorse or promote products
+#       derived from this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+# DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
+# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+# An improved, structured jamendo API wrapper for the N900 with cacheing
 # Image / cover downloads.. and more?
 import urllib, threading, os, gzip, time, simplejson, re
-#import util
-#if util.platform == 'maemo':
-#    _CACHEDIR = os.path.expanduser('''~/MyDocs/.jamaendo''')
-#else:
-#    _CACHEDIR = os.path.expanduser('''~/.cache/jamaendo''')
-
-_CACHEDIR = None#'/tmp/jamaendo'
-_COVERDIR = None#os.path.join(_CACHEDIR, 'covers')
+
+_CACHEDIR = None
+_COVERDIR = None
 _GET2 = '''http://api.jamendo.com/get2/'''
 _MP3URL = _GET2+'stream/track/redirect/?id=%d&streamencoding=mp31'
 _OGGURL = _GET2+'stream/track/redirect/?id=%d&streamencoding=ogg2'
@@ -35,7 +58,7 @@ def set_cache_dir(cachedir):
 # makes a query internally to get the full story
 
 _ARTIST_FIELDS = ['id', 'name', 'image']
-_ALBUM_FIELDS = ['id', 'name', 'image', 'artist_name', 'artist_id']
+_ALBUM_FIELDS = ['id', 'name', 'image', 'artist_name', 'artist_id', 'license_url']
 _TRACK_FIELDS = ['id', 'name', 'image', 'artist_name', 'album_name', 'album_id', 'numalbum', 'duration']
 _RADIO_FIELDS = ['id', 'name', 'idstr', 'image']
 
@@ -97,7 +120,7 @@ class Artist(LazyQuery):
             self.set_from_json(json)
 
     def _needs_load(self):
-        return self._needs_load_impl('name', 'image', 'albums')
+        return self._needs_load_impl('name', 'albums')
 
     def _set_from(self, other):
         return self._set_from_impl(other, 'name', 'image', 'albums')
@@ -109,15 +132,16 @@ class Album(LazyQuery):
         self.image = None
         self.artist_name = None
         self.artist_id = None
+        self.license_url = None
         self.tracks = None # None means not downloaded
         if json:
             self.set_from_json(json)
 
     def _needs_load(self):
-        return self._needs_load_impl('name', 'image', 'artist_name', 'artist_id', 'tracks')
+        return self._needs_load_impl('name', 'image', 'artist_name', 'artist_id', 'license_url', 'tracks')
 
     def _set_from(self, other):
-        return self._set_from_impl(other, 'name', 'image', 'artist_name', 'artist_id', 'tracks')
+        return self._set_from_impl(other, 'name', 'image', 'artist_name', 'artist_id', 'license_url', 'tracks')
 
 class Track(LazyQuery):
     def __init__(self, ID, json=None):
@@ -139,7 +163,7 @@ class Track(LazyQuery):
        return _OGGURL%(self.ID)
 
     def _needs_load(self):
-        return self._needs_load_impl('name', 'image', 'artist_name', 'album_name', 'album_id', 'numalbum', 'duration')
+        return self._needs_load_impl('name', 'artist_name', 'album_name', 'album_id', 'numalbum', 'duration')
 
     def _set_from(self, other):
         return self._set_from_impl(other, 'name', 'image', 'artist_name', 'album_name', 'album_id', 'numalbum', 'duration')
@@ -175,8 +199,8 @@ _CACHED_RADIOS = 10
 # TODO: cache queries?
 
 class Query(object):
-    last_query = time.time()
-    rate_limit = 1.0 # max queries per second
+    rate_limit = 1.1 # seconds between queries
+    last_query = time.time() - 1.5
 
     @classmethod
     def _ratelimit(cls):
@@ -189,7 +213,8 @@ class Query(object):
         pass
 
     def _geturl(self, url):
-        print "geturl: %s" % (url)
+        print "*** %s" % (url)
+        Query._ratelimit()
         f = urllib.urlopen(url)
         ret = simplejson.load(f)
         f.close()
@@ -201,6 +226,45 @@ class Query(object):
     def execute(self):
         raise NotImplemented
 
+import threading
+
+class CoverFetcher(threading.Thread):
+    def __init__(self):
+        threading.Thread.__init__(self)
+        self.setDaemon(True)
+        self.cond = threading.Condition()
+        self.work = []
+
+    def _fetch_cover(self, albumid, size):
+        coverdir = _COVERDIR if _COVERDIR else '/tmp'
+        to = os.path.join(coverdir, '%d-%d.jpg'%(albumid, size))
+        if not os.path.isfile(to):
+            url = _GET2+'image/album/redirect/?id=%d&imagesize=%d'%(albumid, size)
+            urllib.urlretrieve(url, to)
+        return to
+
+    def request_cover(self, albumid, size, cb):
+        self.cond.acquire()
+        self.work.insert(0, (albumid, size, cb))
+        self.cond.notify()
+        self.cond.release()
+
+    def run(self):
+        while True:
+            work = []
+            self.cond.acquire()
+            while True:
+                work = self.work
+                if work:
+                    self.work = []
+                    break
+                self.cond.wait()
+            self.cond.release()
+
+            for albumid, size, cb in work:
+                cover = self._fetch_cover(albumid, size)
+                cb(albumid, size, cover)
+
 class CoverCache(object):
     """
     cache and fetch covers
@@ -218,9 +282,10 @@ class CoverCache(object):
                 m = covermatch.match(fil)
                 if m and os.path.isfile(fl):
                     self._covers[(int(m.group(1)), int(m.group(2)))] = fl
+        self._fetcher = CoverFetcher()
+        self._fetcher.start()
 
     def fetch_cover(self, albumid, size):
-        Query._ratelimit() # ratelimit cover fetching too?
         coverdir = _COVERDIR if _COVERDIR else '/tmp'
         to = os.path.join(coverdir, '%d-%d.jpg'%(albumid, size))
         if not os.path.isfile(to):
@@ -240,9 +305,7 @@ class CoverCache(object):
         if cover:
             cb(cover)
         else:
-            # TODO
-            cover = self.fetch_cover(albumid, size)
-            cb(cover)
+            self._fetcher.request_cover(albumid, size, cb)
 
 _cover_cache = CoverCache()
 
@@ -287,12 +350,12 @@ class GetQuery(Query):
             },
         'tracks' : {
             'url' : _GET2+'+'.join(_TRACK_FIELDS)+'/track/json/track_album+album_artist?',
-            'params' : 'album_id=%d',
+            '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=numradio_asc&radio_id=%d',
+            'params' : 'order=random_asc&radio_id=%d',
             'constructor' : [Track]
             },
         'favorite_albums' : {
@@ -361,7 +424,7 @@ class SearchQuery(GetQuery):
 
 class JamendoAPIException(Exception):
     def __init__(self, url):
-        Exception.__init__(url)
+        Exception.__init__(self, url)
 
 def _update_cache(cache, new_items):
     if not isinstance(new_items, list):
@@ -382,6 +445,8 @@ def get_artist(artist_id):
         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):
@@ -402,6 +467,8 @@ def get_album(album_id):
         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):
@@ -422,6 +489,8 @@ def get_track(track_id):
         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):
@@ -494,9 +563,8 @@ def get_radio(radio_id):
     if not js:
         raise JamendoAPIException(str(q))
     if isinstance(js, list):
-        return [Radio(x['id'], json=x) for x in js]
-    else:
-        return Radio(radio_id, json=js)
+        ks = js[0]
+    return Radio(radio_id, json=js)
 
 def starred_radios():
     """Returns: [Radio]"""
@@ -526,6 +594,7 @@ 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