]> kaliko git repositories - mpd-sima.git/commitdiff
Factoring utilities object
authorkaliko <efrim@azylum.org>
Sat, 8 Feb 2014 13:18:26 +0000 (14:18 +0100)
committerkaliko <efrim@azylum.org>
Sat, 8 Feb 2014 13:18:26 +0000 (14:18 +0100)
sima/lib/simaecho.py
sima/plugins/internal/echonest.py

index fbb60b07fb973305beec2aa2a5a168ca7db8f0ca..716a43eb169094bf7e8d0c67963484f4197a436d 100644 (file)
@@ -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"""
index d5a5122d8c2c7a12d709fc8e5ac68a3b1a856a2c..b2971db4cbe1eb578ef95d31416cbcc7b338f3e9 100644 (file)
@@ -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))