X-Git-Url: http://git.maemo.org/git/?a=blobdiff_plain;ds=sidebyside;f=jamaendo%2Fapi.py;h=15a61bfd1ef583f3324b11cbfaa9725d88a22c94;hb=084629aec1a02dcc4f639b7c97ae63a2a229f8b5;hp=b3683d85634854b0fcc73a95d35287644807b79e;hpb=e5e103503a4fbbaf38960adb829e5fe0e4f6c86f;p=jamaendo diff --git a/jamaendo/api.py b/jamaendo/api.py index b3683d8..15a61bf 100644 --- a/jamaendo/api.py +++ b/jamaendo/api.py @@ -61,7 +61,7 @@ class Obj(object): return "{%s}" % (", ".join("%s=%s"%(k.encode('utf-8'), printable(v)) \ for k,v in self.__dict__.iteritems() if not k.startswith('_'))) -class DB(object): +class LocalDB(object): def __init__(self): self.fil = None @@ -71,19 +71,73 @@ class DB(object): def close(self): self.fil.close() - def make_obj(self, element): + def make_artist_obj(self, element): if element.text is not None and element.text != "": return element.text else: - ret = Obj() + ret = {} for child in element: - if child.tag in ['name', 'id']: - setattr(ret, child.tag, child.text) + if child.tag in ['name', 'id', 'image']: + ret[child.tag] = child.text return ret - def artist_walker(self): + 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 + + def artist_walker(self, name_match): + for event, element in etree.iterparse(self.fil, tag="artist"): + name = element.xpath('./name')[0].text.lower() + if name and name.find(name_match) > -1: + yield self.make_artist_obj(element) + element.clear() + while element.getprevious() is not None: + del element.getparent()[0] + raise StopIteration + + def album_walker(self, name_match): + for event, element in etree.iterparse(self.fil, tag="album"): + name = element.xpath('./name')[0].text + if name and name.lower().find(name_match) > -1: + yield self.make_album_obj(element) + element.clear() + while element.getprevious() is not None: + del element.getparent()[0] + raise StopIteration + + def artistid_walker(self, artistids): for event, element in etree.iterparse(self.fil, tag="artist"): - yield self.make_obj(element) + _id = element.xpath('./id')[0].text + if _id and int(_id) in artistids: + yield self.make_artist_obj(element) + element.clear() + while element.getprevious() is not None: + del element.getparent()[0] + raise StopIteration + + 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: + yield self.make_album_obj(element) element.clear() while element.getprevious() is not None: del element.getparent()[0] @@ -91,4 +145,76 @@ class DB(object): def search_artists(self, substr): substr = substr.lower() - return [artist for artist in self.artist_walker() if artist.name.lower().find(substr) > -1] + return (artist for artist in self.artist_walker(substr)) + + def search_albums(self, substr): + 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_album(self, albumid): + return (album for album in self.albumid_walker(albumid)) + +_GET2 = '''http://api.jamendo.com/get2/''' + +class Query(object): + last_query = time.time() + caching = True + cache_time = 60*60*24 + rate_limit = 1.0 # max queries per second + + def __init__(self, order, + select=['id', 'name', 'image', 'artist_name'], + request='album', + track=['track_album', 'album_artist'], + count=5): + if request == 'track': + self.url = "%s%s/%s/json/%s?n=%s&order=%s" % (_GET2, '+'.join(select), request, '+'.join(track), count, order) + else: + self.url = "%s%s/%s/json/?n=%s&order=%s" % (_GET2, '+'.join(select), request, count, order) + + + def _ratelimit(self): + now = time.time() + if now - self.last_query < self.rate_limit: + time.sleep(self.rate_limit - (now - self.last_query)) + self.last_query = now + + def __call__(self): + """ratelimited query""" + self._ratelimit() + f = urllib.urlopen(self.url) + ret = json.load(f) + f.close() + return ret + + @staticmethod + def album_cover(albumid, size=200): + to = '~/.cache/jamaendo/cover-%d-%d.jpg'%(albumid, size) + if not os.path.isfile(to): + url = _GET2+'image/album/redirect/?id=%d&imagesize=%d'%(albumid, size) + urllib.urlretrieve(url, to) + return to + + @staticmethod + def track_ogg(trackid): + return _GET2+ 'stream/track/redirect/?id=%d&streamencoding=ogg2'%(trackid) + + @staticmethod + def track_mp3(trackid): + return _GET2+ 'stream/track/redirect/?id=%d&streamencoding=mp31'%(trackid) + +class Queries(object): + albums_this_week = Query(order='ratingweek_desc') + albums_all_time = Query(order='ratingtotal_desc') + albums_this_month = Query(order='ratingmonth_desc') + albums_today = Query(order='ratingday_desc') + playlists_all_time = Query(select=['id','name', 'user_idstr'], request='playlist', order='ratingtotal_desc') + tracks_this_month = Query(select=['id', 'name', + 'stream', + 'album_name', 'artist_name', + 'album_id', 'artist_id'], + request='track', + order='ratingmonth_desc')