beginnings of a maemo ui
[jamaendo] / jamaendo / api.py
index 15a61bf..994288d 100644 (file)
@@ -1,4 +1,4 @@
-import urllib, threading, os, gzip, time, json, re
+import urllib, threading, os, gzip, time, simplejson, re
 _DUMP_URL = '''http://img.jamendo.com/data/dbdump_artistalbumtrack.xml.gz'''
 _DUMP = os.path.expanduser('''~/.cache/jamaendo/dbdump.xml.gz''')
 
@@ -71,37 +71,58 @@ class LocalDB(object):
     def close(self):
         self.fil.close()
 
+    def make_album_brief(self, element):
+        ret = {}
+        for info in element:
+            if info.tag == 'id':
+                ret['id'] = int(info.text)
+            elif info.tag == 'name':
+                ret['name'] = info.text
+        return ret
+
     def make_artist_obj(self, element):
-        if element.text is not None and element.text != "":
-            return element.text
-        else:
-            ret = {}
-            for child in element:
-                if child.tag in ['name', 'id', 'image']:
-                    ret[child.tag] = child.text
-            return ret
+        ret = {}
+        for child in element:
+            if child.tag == 'id':
+                ret['id'] = int(child.text)
+            elif child.tag in ('name', 'image'):
+                ret[child.tag] = child.text
+            elif child.tag == 'Albums':
+                ret['albums'] = [self.make_album_brief(a) for a in child]
+        return ret
+
+    def make_track_obj(self, element):
+        ret = {}
+        for info in element:
+            if info.tag == 'id':
+                _id = int(info.text)
+                ret['id'] = _id
+                ret['mp3'] = Query.track_mp3(_id)
+                ret['ogg'] = Query.track_ogg(_id)
+            elif info.tag in ('name', 'numalbum'):
+                ret[info.tag] = info.text
+        return ret
 
     def make_album_obj(self, element):
-        if element.text is not None and element.text != "":
-            return element.text
-        else:
-            ret = {}
-            for child in element:
-                if child.tag in ['name', 'id', 'image']:
-                    if child.text:
-                        ret[child.tag] = child.text
-                    else:
-                        ret[child.tag] = ""
-                if child.tag == 'Tracks':
-                    tracks = []
-                    for track in child:
-                        trackd = {}
-                        for trackinfo in track:
-                            if trackinfo.tag in ['name', 'id', 'numalbum']:
-                                trackd[trackinfo.tag] = trackinfo.text
-                        tracks.append(trackd)
-                    ret['tracks'] = tracks
-            return ret
+        ret = {}
+        artist = element.getparent().getparent()
+        if artist is not None:
+            for child in artist:
+                if child.tag == 'name':
+                    ret['artist'] = child.text
+                elif child.tag == 'id':
+                    ret['artist_id'] = int(child.text)
+        for child in element:
+            if child.tag == 'id':
+                ret['id'] = int(child.text)
+            elif child.tag in ('name', 'image'):
+                if child.text:
+                    ret[child.tag] = child.text
+                else:
+                    ret[child.tag] = ""
+            elif child.tag == 'Tracks':
+                ret['tracks'] = [self.make_track_obj(t) for t in child]
+        return ret
 
     def artist_walker(self, name_match):
         for event, element in etree.iterparse(self.fil, tag="artist"):
@@ -136,7 +157,7 @@ class LocalDB(object):
     def albumid_walker(self, albumids):
         for event, element in etree.iterparse(self.fil, tag="album"):
             _id = element.xpath('./id')[0].text
-            if _id and int(_id) in albumids:
+            if _id and (int(_id) in albumids):
                 yield self.make_album_obj(element)
             element.clear()
             while element.getprevious() is not None:
@@ -151,11 +172,11 @@ class LocalDB(object):
         substr = substr.lower()
         return (album for album in self.album_walker(substr))
 
-    def get_album(self, artistid):
-        return (artist for artist in self.artistid_walker(artistid))
+    def get_artists(self, artistids):
+        return (artist for artist in self.artistid_walker(artistids))
 
-    def get_album(self, albumid):
-        return (album for album in self.albumid_walker(albumid))
+    def get_albums(self, albumids):
+        return (album for album in self.albumid_walker(albumids))
 
 _GET2 = '''http://api.jamendo.com/get2/'''
 
@@ -186,7 +207,7 @@ class Query(object):
         """ratelimited query"""
         self._ratelimit()
         f = urllib.urlopen(self.url)
-        ret = json.load(f)
+        ret = simplejson.load(f)
         f.close()
         return ret