From: kaliko Date: Sat, 8 Feb 2014 13:18:26 +0000 (+0100) Subject: Factoring utilities object X-Git-Tag: mpd-sima/0.12.0pr3~1 X-Git-Url: http://git.kaliko.me/?a=commitdiff_plain;h=774f4b39de0b2ad7aa0e6ed3d7cc739a5456e8c5;p=mpd-sima.git Factoring utilities object --- diff --git a/sima/lib/simaecho.py b/sima/lib/simaecho.py index fbb60b0..716a43e 100644 --- a/sima/lib/simaecho.py +++ b/sima/lib/simaecho.py @@ -34,7 +34,7 @@ from requests import get, Request, Timeout, ConnectionError from sima import ECH from sima.lib.meta import Artist -from sima.utils.utils import getws +from sima.utils.utils import getws, Throttle, Cache, purge_cache if len(ECH.get('apikey')) == 23: # simple hack allowing imp.reload getws(ECH) @@ -55,48 +55,6 @@ class EchoTimeout(EchoError): class EchoHTTPError(EchoError): pass -class Throttle(): - def __init__(self, wait): - self.wait = wait - self.last_called = datetime.now() - - def __call__(self, func): - def wrapper(*args, **kwargs): - while self.last_called + self.wait > datetime.now(): - sleep(0.1) - result = func(*args, **kwargs) - self.last_called = datetime.now() - return result - return wrapper - - -class Cache(): - def __init__(self, elem, last=None): - self.elem = elem - self.requestdate = last - if not last: - self.requestdate = datetime.utcnow() - - def created(self): - return self.requestdate - - def get(self): - return self.elem - - -def purge_cache(age=4): - now = datetime.utcnow() - if now.hour == SimaEch.timestamp.hour: - return - SimaEch.timestamp = datetime.utcnow() - cache = SimaEch.cache - delta = timedelta(hours=age) - for url in list(cache.keys()): - timestamp = cache.get(url).created() - if now - timestamp > delta: - cache.pop(url) - - class SimaEch(): """ """ @@ -110,7 +68,7 @@ class SimaEch(): self._ressource = None self.current_element = None self.caching = cache - purge_cache() + purge_cache(self.__class__) def _fetch(self, payload): """Use cached elements or proceed http request""" diff --git a/sima/plugins/internal/echonest.py b/sima/plugins/internal/echonest.py index d5a5122..b2971db 100644 --- a/sima/plugins/internal/echonest.py +++ b/sima/plugins/internal/echonest.py @@ -180,6 +180,7 @@ class EchoNest(Plugin): as_artists = simaech.get_similar(artist=current) self.log.debug('Requesting EchoNest for "{0}"'.format(current)) try: + # TODO: let's propagate Artist type [as_art.append(str(art)) for art in as_artists] except EchoNotFound as err: self.log.warning(err) @@ -293,7 +294,7 @@ class EchoNest(Plugin): if not album_to_queue: self.log.info('No album found for "%s"' % artist) continue - self.log.info('last.fm album candidate: {0} - {1}'.format( + self.log.info('echonest album candidates: {0} - {1}'.format( artist, album_to_queue)) nb_album_add += 1 self.to_add.extend(self.player.find_album(artist, album_to_queue))