Some new featured selections (top tags, etc)
[jamaendo] / jamaendo / api.py
index cd3d454..f7cbc0e 100644 (file)
@@ -8,12 +8,6 @@
 # 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
@@ -29,7 +23,7 @@
 # An improved, structured jamendo API wrapper for the N900 with cacheing
 # Image / cover downloads.. and more?
 import urllib, threading, os, time, simplejson, re
-import logging
+import logging, hashlib
 
 _CACHEDIR = None
 _COVERDIR = None
@@ -54,8 +48,9 @@ except:
 
 _ARTIST_FIELDS = ['id', 'name', 'image']
 _ALBUM_FIELDS = ['id', 'name', 'image', 'artist_name', 'artist_id', 'license_url']
-_TRACK_FIELDS = ['id', 'name', 'image', 'artist_id', 'artist_name', 'album_name', 'album_id', 'numalbum', 'duration']
+_TRACK_FIELDS = ['id', 'name', 'album_image', 'artist_id', 'artist_name', 'album_name', 'album_id', 'numalbum', 'duration']
 _RADIO_FIELDS = ['id', 'name', 'idstr', 'image']
+_TAG_FIELDS = ['id', 'name']
 
 class LazyQuery(object):
     def set_from_json(self, json):
@@ -146,9 +141,9 @@ class Track(LazyQuery):
     def __init__(self, ID, json=None):
         self.ID = int(ID)
         self.name = None
-        self.image = None
         self.artist_id = None
         self.artist_name = None
+        self.album_image = None
         self.album_name = None
         self.album_id = None
         self.numalbum = None
@@ -166,7 +161,7 @@ class Track(LazyQuery):
         return self._needs_load_impl('name', 'artist_name', 'artist_id', 'album_name', 'album_id', 'numalbum', 'duration')
 
     def _set_from(self, other):
-        return self._set_from_impl(other, 'name', 'image', 'artist_name', 'artist_id', 'album_name', 'album_id', 'numalbum', 'duration')
+        return self._set_from_impl(other, 'name', 'album_image', 'artist_name', 'artist_id', 'album_name', 'album_id', 'numalbum', 'duration')
 
 class Radio(LazyQuery):
     def __init__(self, ID, json=None):
@@ -183,6 +178,18 @@ class Radio(LazyQuery):
     def _set_from(self, other):
         return self._set_from_impl(other, 'name', 'idstr', 'image')
 
+class Tag(LazyQuery):
+    def __init__(self, ID, json=None):
+        self.ID = int(ID)
+        self.name = None
+        if json:
+            self.set_from_json(json)
+
+    def _needs_load(self):
+        return self._needs_load_impl('name')
+
+    def _set_from(self, other):
+        return self._set_from_impl(other, 'name')
 
 _artists = {} # id -> Artist()
 _albums = {} # id -> Album()
@@ -249,12 +256,30 @@ class CoverFetcher(threading.Thread):
         except Exception, e:
             return None
 
+    def _fetch_image(self, url):
+        try:
+            h = hashlib.md5(url).hexdigest()
+            coverdir = _COVERDIR if _COVERDIR else '/tmp'
+            to = os.path.join(coverdir, h+'.jpg')
+            if not os.path.isfile(to):
+                urllib.urlretrieve(url, to)
+            return to
+        except Exception, e:
+            return None
+
     def request_cover(self, albumid, size, cb):
         self.cond.acquire()
         self.work.insert(0, (albumid, size, cb))
         self.cond.notify()
         self.cond.release()
 
+    def request_images(self, urls, cb):
+        """cb([(url, image)])"""
+        self.cond.acquire()
+        self.work.insert(0, ('images', urls, cb))
+        self.cond.notify()
+        self.cond.release()
+
     def run(self):
         while True:
             work = []
@@ -268,12 +293,23 @@ class CoverFetcher(threading.Thread):
             self.cond.release()
 
             multi = len(work) > 1
-            for albumid, size, cb in work:
-                cover = self._fetch_cover(albumid, size)
-                if cover:
-                    cb(albumid, size, cover)
-                    if multi:
-                        time.sleep(1.0)
+            for job in work:
+                if job[0] == 'images':
+                    self.process_images(job[1], job[2])
+                else:
+                    self.process_cover(*job)
+                if multi:
+                    time.sleep(1.0)
+
+    def process_cover(self, albumid, size, cb):
+        cover = self._fetch_cover(albumid, size)
+        if cover:
+            cb(albumid, size, cover)
+
+    def process_images(self, urls, cb):
+        results = [(url, image) for url, image in ((url, self._fetch_image(url)) for url in urls) if image is not None]
+        if results:
+            cb(results)
 
 class CoverCache(object):
     """
@@ -284,6 +320,7 @@ class CoverCache(object):
     """
     def __init__(self):
         self._covers = {} # (albumid, size) -> file
+        self._images = {}
         self._fetcher = CoverFetcher()
         self._fetcher.start()
         if _COVERDIR and os.path.isdir(_COVERDIR):
@@ -336,7 +373,29 @@ class CoverCache(object):
         if cover:
             cb(albumid, size, cover)
         else:
-            self._fetcher.request_cover(albumid, size, cb)
+            def cb2(albumid, size, cover):
+                self._covers[(albumid, size)] = cover
+                cb(albumid, size, cover)
+            self._fetcher.request_cover(albumid, size, cb2)
+
+    def get_images_async(self, url_list, cb):
+        found = []
+        lookup = []
+        for url in url_list:
+            image = self._images.get(url, None)
+            if image:
+                found.append((url, image))
+            else:
+                lookup.append(url)
+        if found:
+            cb(found)
+
+        if lookup:
+            def cb2(results):
+                for url, image in results:
+                    self._images[url] = image
+                cb(results)
+            self._fetcher.request_images(lookup, cb2)
 
 _cover_cache = CoverCache()
 
@@ -364,6 +423,9 @@ def get_album_cover(albumid, size=100):
 def get_album_cover_async(cb, albumid, size=100):
     _cover_cache.get_async(albumid, size, cb)
 
+def get_images_async(cb, url_list):
+    _cover_cache.get_images_async(url_list, cb)
+
 class CustomQuery(Query):
     def __init__(self, url):
         Query.__init__(self)
@@ -419,7 +481,7 @@ class GetQuery(Query):
             },
         'radio' : {
             'url' : _GET2+'+'.join(_TRACK_FIELDS)+'/track/json/radio_track_inradioplaylist+track_album+album_artist/?',
-            'params' : 'order=random_asc&radio_id=%d',
+            'params' : 'order=random_asc&radio_id=%d&n=16',
             'constructor' : [Track]
             },
         'favorite_albums' : {
@@ -427,6 +489,11 @@ class GetQuery(Query):
             'params' : 'user_idstr=%s',
             'constructor' : [Album]
             },
+        'tag' : {
+            'url' : _GET2+'+'.join(_TRACK_FIELDS)+'/track/json/track_album+album_artist?',
+            'params' : 'tag_id=%d&n=50&order=rating_desc',
+            'constructor' : [Track]
+            },
         }
 
     def __init__(self, what, ID):
@@ -456,7 +523,7 @@ class GetQuery(Query):
         return self.url + self.params % (self.ID)
 
 class SearchQuery(GetQuery):
-    def __init__(self, what, query=None, order=None, user=None, count=10):
+    def __init__(self, what, query=None, order=None, user=None, count=20):
         GetQuery.__init__(self, what, None)
         self.query = query
         self.order = order
@@ -652,6 +719,16 @@ def get_radio_tracks(radio_id):
     _update_cache(_tracks, a)
     return a
 
+#http://api.jamendo.com/get2/id+name/track/plain/?tag_id=327&n=50&order=rating_desc
+def get_tag_tracks(tag_id):
+    """Returns: [Track]"""
+    q = GetQuery('tag', tag_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')
@@ -706,6 +783,33 @@ def tracks_of_the_week():
     _update_cache(_tracks, a)
     return a
 
+def top_artists(order='rating_desc', count=20):
+    """Returns: [Artist]"""
+    q = SearchQuery('artist', order=order, count=count)
+    a = q.execute()
+    if not a:
+        raise JamendoAPIException(str(q))
+    _update_cache(_artists, a)
+    return a
+
+def top_albums(order='rating_desc', count=20):
+    """Returns: [Album]"""
+    q = SearchQuery('album', order=order, count=count)
+    a = q.execute()
+    if not a:
+        raise JamendoAPIException(str(q))
+    _update_cache(_albums, a)
+    return a
+
+def top_tracks(order='rating_desc', count=20):
+    """Returns: [Track]"""
+    q = SearchQuery('track', order=order, count=count)
+    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))
@@ -724,6 +828,14 @@ def starred_radios():
         raise JamendoAPIException(str(q))
     return [Radio(int(radio['id']), json=radio) for radio in js]
 
+def top_tags(count=50, order='rating_desc'):
+    """Returns: [Tag]"""
+    q = CustomQuery(_GET2+"id+name/tag/json?n=%d&order=%s"%(count, order))
+    js = q.execute()
+    if not js:
+        raise JamendoAPIException(str(q))
+    return [Tag(int(tag['id']), json=tag) for tag in js]
+
 def favorite_albums(user):
     """Returns: [Album]"""
     q = SearchQuery('favorite_albums', user=user, count=20)