Oops, my rate limiting was all broken...
[jamaendo] / jamaendo / api.py
index 3de54e9..44cec73 100644 (file)
@@ -129,6 +129,9 @@ class Artist(LazyQuery):
     def _set_from(self, other):
         return self._set_from_impl(other, 'name', 'image', 'albums')
 
+    def get_data(self):
+        return {'name':self.name, 'image':self.image}
+
 class Album(LazyQuery):
     def __init__(self, ID, json=None):
         self.ID = int(ID)
@@ -151,6 +154,12 @@ class Album(LazyQuery):
     def _set_from(self, other):
         return self._set_from_impl(other, 'name', 'image', 'artist_name', 'artist_id', 'license_url', 'tracks')
 
+    def get_data(self):
+        return {'name':self.name, 'image':self.image,
+                'artist_name':self.artist_name,
+                'artist_id':self.artist_id,
+                'license_url':self.license_url}
+
 class Track(LazyQuery):
     def __init__(self, ID, json=None):
         self.ID = int(ID)
@@ -171,6 +180,16 @@ class Track(LazyQuery):
     def ogg_url(self):
        return _OGGURL%(self.ID)
 
+    def get_data(self):
+        return {'name':self.name,
+                'artist_id':self.artist_id,
+                'artist_name':self.artist_name,
+                'album_image':self.album_image,
+                'album_name':self.album_name,
+                'album_id':self.album_id,
+                'numalbum':self.numalbum,
+                'duration':self.duration}
+
     def _needs_load(self):
         return self._needs_load_impl('name', 'artist_name', 'artist_id', 'album_name', 'album_id', 'numalbum', 'duration')
 
@@ -221,23 +240,26 @@ _CACHED_COVERS = 2048
 
 # TODO: cache queries?
 
-class Query(object):
-    rate_limit = 1.1 # seconds between queries
+class Ratelimit(object):
+    rate_limit = 1.0 # seconds between queries
     last_query = time.time() - 1.5
 
     @classmethod
-    def _ratelimit(cls):
+    def ratelimit(cls):
         now = time.time()
-        if now - cls.last_query < cls.rate_limit:
+        if (now - cls.last_query) < cls.rate_limit:
             time.sleep(cls.rate_limit - (now - cls.last_query))
-        cls.last_query = now
+        cls.last_query = time.time()
+
+_ratelimit = Ratelimit.ratelimit
 
+class Query(object):
     def __init__(self):
         pass
 
     def _geturl(self, url):
+        _ratelimit()
         log.info("%s", url)
-        Query._ratelimit()
         try:
             ret = simplejson.loads(curlGET(url))
         except Exception, e:
@@ -258,6 +280,9 @@ class CoverFetcher(threading.Thread):
         self.work = []
 
     def _retrieve(self, url, fname):
+        BROKEN = 'http://imgjam.com/radios/default/default.100.png'
+        if url == BROKEN:
+            return None
         f = open(fname, 'wb')
         c = pycurl.Curl()
         c.setopt(pycurl.FOLLOWLOCATION, 1)
@@ -670,7 +695,7 @@ def get_albums(artist_id):
     a = q.execute()
     if not a:
         raise JamendoAPIException(str(q))
-    _update_cache(_artists, a)
+    _update_cache(_albums, a)
     return a
 
 def get_album(album_id):