X-Git-Url: https://git.kaliko.me/?a=blobdiff_plain;f=sima%2Flib%2Fsimaecho.py;h=14623031df6ab1f46d0d991572975171f0908812;hb=78a694ddcd2a6ecc8b2b1fd3c74ee2d938707305;hp=716a43eb169094bf7e8d0c67963484f4197a436d;hpb=774f4b39de0b2ad7aa0e6ed3d7cc739a5456e8c5;p=mpd-sima.git diff --git a/sima/lib/simaecho.py b/sima/lib/simaecho.py index 716a43e..1462303 100644 --- a/sima/lib/simaecho.py +++ b/sima/lib/simaecho.py @@ -25,15 +25,13 @@ __version__ = '0.0.1' __author__ = 'Jack Kaliko' -import logging - from datetime import datetime, timedelta -from time import sleep from requests import get, Request, Timeout, ConnectionError from sima import ECH from sima.lib.meta import Artist +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) @@ -43,25 +41,14 @@ 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 SimaEch(): - """ +class SimaEch: + """EchoNest http client """ root_url = 'http://{host}/api/{version}'.format(**ECH) cache = {} timestamp = datetime.utcnow() ratelimit = None + name = 'EchoNest' def __init__(self, cache=True): self.artist = None @@ -77,21 +64,21 @@ class SimaEch(): self.current_element = SimaEch.cache.get(url).elem return try: - self._fetch_ech(payload) + self._fetch_ws(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_ech(self, payload): + def _fetch_ws(self, payload): """fetch from web service""" req = get(self._ressource, params=payload, timeout=SOCKET_TIMEOUT) 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: @@ -106,11 +93,11 @@ class SimaEch(): 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): - """ + """Build payload """ payload = {'api_key': ECH.get('apikey')} if not isinstance(artist, Artist): @@ -120,13 +107,13 @@ class SimaEch(): payload.update( id='musicbrainz:artist:{0}'.format(artist.mbid)) else: - payload.update(name=artist.name) + payload.update(name=artist.name) payload.update(bucket='id:musicbrainz') payload.update(results=100) return payload def get_similar(self, artist=None): - """ + """Fetch similar artists """ payload = self._forge_payload(artist) # Construct URL