X-Git-Url: http://git.maemo.org/git/?p=jamaendo;a=blobdiff_plain;f=jamaendo%2Fapi.py;h=994288dc7b66d156fbf0f4296eed6025702986f4;hp=5d2bde9f1891abe2f71b8ecb49054358086b02da;hb=3d5388f367ce5c0c6a7c73074b79e0c9b65f0fdd;hpb=ba8351e78b0507818a08587c4d9d7e32fb59bd62;ds=sidebyside diff --git a/jamaendo/api.py b/jamaendo/api.py index 5d2bde9..994288d 100644 --- a/jamaendo/api.py +++ b/jamaendo/api.py @@ -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,53 +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 - if child.tag == 'Albums': - albums = [] - for album in child: - albie = {} - for albumitem in album: - if albumitem.tag in ['name', 'id']: - albie[albumitem.tag] = albumitem.text - albums.append(albie) - ret['albums'] = albums - 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 = {} - 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'] = child.text - 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"): @@ -202,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