X-Git-Url: http://git.kaliko.me/?a=blobdiff_plain;f=sima%2Flib%2Fsimaecho.py;h=3e6f28147d29d6669b3178929d9ff6b65411b627;hb=00f3a52f35f709dd4c471cb6ad87dbd09cfd4aaf;hp=03892c0a3a7efd3ce1d41167e1a70ccbcccf3c80;hpb=a70e527205631bf656147c04d2ee022aced877d0;p=mpd-sima.git diff --git a/sima/lib/simaecho.py b/sima/lib/simaecho.py index 03892c0..3e6f281 100644 --- a/sima/lib/simaecho.py +++ b/sima/lib/simaecho.py @@ -30,12 +30,13 @@ import logging from datetime import datetime, timedelta from time import sleep -from requests import get, Timeout, ConnectionError +from requests import get, Request, Timeout, ConnectionError from sima import ECH from sima.lib.meta import Artist -from sima.utils.utils import getws -if len(ECH.get('apikey')) == 23: +from sima.utils.utils import WSError, WSNotFound, WSTimeout, WSHTTPError +from sima.utils.utils import getws, Throttle, Cache, purge_cache +if len(ECH.get('apikey')) == 23: # simple hack allowing imp.reload getws(ECH) # Some definitions @@ -43,60 +44,21 @@ WAIT_BETWEEN_REQUESTS = timedelta(0, 1) SOCKET_TIMEOUT = 4 -class EchoError(Exception): - pass - -class EchoNotFound(EchoError): - pass - -class EchoTimeout(EchoError): - pass - -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 - - class SimaEch(): """ """ root_url = 'http://{host}/api/{version}'.format(**ECH) cache = {} timestamp = datetime.utcnow() + ratelimit = None + name = 'EchoNest' def __init__(self, cache=True): self.artist = None self._ressource = None self.current_element = None self.caching = cache - self.purge_cache() + purge_cache(self.__class__) def _fetch(self, payload): """Use cached elements or proceed http request""" @@ -105,22 +67,21 @@ class SimaEch(): self.current_element = SimaEch.cache.get(url).elem return try: - self._fetch_lfm(payload) + self._fetch_ech(payload) except Timeout: - raise EchoTimeout('Failed to reach server within {0}s'.format( + raise WSTimeout('Failed to reach server within {0}s'.format( SOCKET_TIMEOUT)) except ConnectionError as err: - raise EchoError(err) + raise WSError(err) @Throttle(WAIT_BETWEEN_REQUESTS) - def _fetch_lfm(self, payload): + def _fetch_ech(self, payload): """fetch from web service""" req = get(self._ressource, params=payload, timeout=SOCKET_TIMEOUT) - if 'x-ratelimit-remaining' in req.headers: - logging.debug('x-ratelimit-remaining {x-ratelimit-remaining}'.format(**req.headers)) + self.__class__.ratelimit = req.headers.get('x-ratelimit-remaining', None) if req.status_code is not 200: - raise EchoHTTPError(req.status_code) + raise WSHTTPError(req.status_code) self.current_element = req.json() self._controls_answer() if self.caching: @@ -128,15 +89,15 @@ class SimaEch(): Cache(self.current_element)}) def _controls_answer(self): - """Controls last.fm answer. + """Controls answer. """ status = self.current_element.get('response').get('status') code = status.get('code') if code is 0: return True if code is 5: - raise EchoNotFound('Artist not found: "{0}"'.format(self.artist)) - raise EchoError(status.get('message')) + raise WSNotFound('Artist not found: "{0}"'.format(self.artist)) + raise WSError(status.get('message')) def _forge_payload(self, artist): """ @@ -151,21 +112,9 @@ class SimaEch(): else: payload.update(name=artist.name) payload.update(bucket='id:musicbrainz') - payload.update(results=30) + payload.update(results=100) return payload - def purge_cache(self, 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) - def get_similar(self, artist=None): """ """ @@ -177,9 +126,10 @@ class SimaEch(): artist = {} mbid = None if 'foreign_ids' in art: - for frgnid in art.get('foreign_ids'): - if frgnid.get('catalog') == 'musicbrainz': - mbid = frgnid.get('foreign_id').lstrip('musicbrainz:artist:') + for frgnid in art.get('foreign_ids'): + if frgnid.get('catalog') == 'musicbrainz': + mbid = frgnid.get('foreign_id' + ).lstrip('musicbrainz:artist:') yield Artist(mbid=mbid, name=art.get('name'))