From: Kristoffer Grönlund Date: Tue, 29 Dec 2009 13:56:12 +0000 (+0100) Subject: Improved queries X-Git-Url: http://git.maemo.org/git/?p=jamaendo;a=commitdiff_plain;h=084629aec1a02dcc4f639b7c97ae63a2a229f8b5;hp=91f0f639cd4244e40171959a16112026a8c9db87 Improved queries --- diff --git a/jamaendo/api.py b/jamaendo/api.py index 9913fcc..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 @@ -161,44 +161,60 @@ _GET2 = '''http://api.jamendo.com/get2/''' class Query(object): last_query = time.time() - - def __init__(self, order, select=['id', 'name', 'image', 'artist_name'], request='album', track=None, n=8): + 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), n, order) + 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, n, order) + self.url = "%s%s/%s/json/?n=%s&order=%s" % (_GET2, '+'.join(select), request, count, order) - def emit(self): - """ratelimited query""" + + def _ratelimit(self): now = time.time() - if now - self.last_query < 1.0: - time.sleep(1.0 - (now - self.last_query)) + 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', 'url', 'stream', 'album_name', 'album_url', 'album_id', 'artist_id', 'artist_name'], + tracks_this_month = Query(select=['id', 'name', + 'stream', + 'album_name', 'artist_name', + 'album_id', 'artist_id'], request='track', - track=['track_album', 'album_artist'], order='ratingmonth_desc') - -def get_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 - -def get_ogg_url(trackid): - return _GET2+ 'stream/track/redirect/?id=%d&streamencoding=ogg2'%(trackid) - -def get_mp3_url(trackid): - return _GET2+ 'stream/track/redirect/?id=%d&streamencoding=mp31'%(trackid) diff --git a/scripts/query b/scripts/query index 707b48e..ebf97d6 100755 --- a/scripts/query +++ b/scripts/query @@ -1,27 +1,50 @@ #!/usr/bin/env python -from jamaendo.api import DB, Queries -import sys +from jamaendo.api import LocalDB, Queries, refresh_dump +import sys, time + +class Refresher(object): + def __init__(self): + self.done = False + self.last_percent = 0 + print "..." + def complete(self): + print "Done." + self.done = True + def progress(self, percent): + if percent - self.last_percent >= 5: + print "\r%d%%" % (percent), + self.last_percent = percent + + def run(self): + refresh_dump(self.complete, self.progress, force=False) + while not self.done: + time.sleep(1) + def pprint(x): import json print json.dumps(x, sort_keys=True, indent=4) def main(): + Refresher().run() query = sys.argv[1] if query == 'today': - result = Queries.albums_today.emit() + result = Queries.albums_today() + pprint(result) + elif query == 'tracks_this_month': + result = Queries.tracks_this_month() pprint(result) elif query == 'artist': q = sys.argv[2] - db = DB() + db = LocalDB() db.connect() for artist in db.search_artists(q): pprint(artist) elif query == 'album': q = sys.argv[2] - db = DB() + db = LocalDB() db.connect() for album in db.search_albums(q): pprint(album) diff --git a/scripts/refresh_db b/scripts/refresh_db index e8d84b8..b7b0412 100755 --- a/scripts/refresh_db +++ b/scripts/refresh_db @@ -1,9 +1,6 @@ #!/usr/bin/env python from jamaendo.api import refresh_dump -import locale, time - -locale.setlocale(locale.LC_ALL, '') -code = locale.getpreferredencoding() +import time class Monitor(object): def __init__(self):