From 774f4b39de0b2ad7aa0e6ed3d7cc739a5456e8c5 Mon Sep 17 00:00:00 2001 From: kaliko Date: Sat, 8 Feb 2014 14:18:26 +0100 Subject: [PATCH] Factoring utilities object --- sima/lib/simaecho.py | 46 ++----------------------------- sima/plugins/internal/echonest.py | 3 +- 2 files changed, 4 insertions(+), 45 deletions(-) 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)) -- 2.39.5