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)
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():
"""
"""
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"""
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)
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))