Some new featured selections (top tags, etc)
authorKristoffer Grönlund <kristoffer.gronlund@purplescout.se>
Sun, 3 Jan 2010 14:30:32 +0000 (15:30 +0100)
committerKristoffer Grönlund <kristoffer.gronlund@purplescout.se>
Sun, 3 Jan 2010 14:30:32 +0000 (15:30 +0100)
jamaendo/__init__.py
jamaendo/api.py
jamaui/featured.py

index e0b69b0..342be29 100644 (file)
@@ -28,7 +28,7 @@
 
 from api import set_cache_dir, \
     JamendoAPIException, \
-    Artist, Album, Track, Radio, \
+    Artist, Album, Track, Radio, Tag, \
     get_album_cover, \
     get_album_cover_async, \
     get_images_async, \
@@ -41,9 +41,15 @@ from api import set_cache_dir, \
     search_artists, \
     search_albums, \
     search_tracks, \
+    top_artists, \
+    top_albums, \
+    top_tracks, \
+    top_tags, \
     albums_of_the_week, \
     new_releases, \
     tracks_of_the_week, \
     get_radio, \
     starred_radios, \
-    favorite_albums
+    favorite_albums, \
+    get_tag_tracks
+
index 5fccc25..f7cbc0e 100644 (file)
@@ -48,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):
@@ -140,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
@@ -160,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):
@@ -177,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()
@@ -476,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):
@@ -505,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
@@ -701,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')
@@ -755,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))
@@ -773,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)
index 010ce23..eec493b 100644 (file)
@@ -37,7 +37,10 @@ def _alist(l, match):
 class FeaturedWindow(hildon.StackableWindow):
     features = (("Albums of the week",jamaendo.albums_of_the_week),
                 ("Tracks of the week",jamaendo.tracks_of_the_week),
-                ("New releases",jamaendo.new_releases)
+                ("New releases",jamaendo.new_releases),
+                ("Top 50 tags", lambda: jamaendo.top_tags(count=50)),
+                ("Top 50 albums", lambda: jamaendo.top_albums(count=50)),
+                ("Top 50 tracks", lambda: jamaendo.top_tracks(count=50)),
                 )
 
     def __init__(self, feature):
@@ -122,3 +125,6 @@ class FeaturedWindow(hildon.StackableWindow):
         elif isinstance(item, jamaendo.Track):
             wnd = open_playerwindow()
             wnd.play_tracks([item])
+        elif isinstance(item, jamaendo.Tag):
+            wnd = open_playerwindow()
+            wnd.play_tracks(jamaendo.get_tag_tracks(item.ID))