X-Git-Url: http://git.kaliko.me/?a=blobdiff_plain;f=sima%2Flib%2Fsimaecho.py;h=716a43eb169094bf7e8d0c67963484f4197a436d;hb=774f4b39de0b2ad7aa0e6ed3d7cc739a5456e8c5;hp=fbb60b07fb973305beec2aa2a5a168ca7db8f0ca;hpb=4240fefd9d393365932ff04c063cb31fc5dce85c;p=mpd-sima.git 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"""