- cache = {}
- timestamp = datetime.utcnow()
-
- def __init__(self, cache=True):
- self.artist = None
- self._ressource = None
- self.current_element = None
- self.caching = cache
- self.purge_cache()
-
- def _fetch(self, payload):
- """Use cached elements or proceed http request"""
- url = Request('GET', self._ressource, params=payload,).prepare().url
- if url in SimaEch.cache:
- self.current_element = SimaEch.cache.get(url).elem
- return
- try:
- self._fetch_lfm(payload)
- except Timeout:
- raise EchoTimeout('Failed to reach server within {0}s'.format(
- SOCKET_TIMEOUT))
- except ConnectionError as err:
- raise EchoError(err)
-
- @Throttle(WAIT_BETWEEN_REQUESTS)
- def _fetch_lfm(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))
- if req.status_code is not 200:
- raise EchoHTTPError(req.status_code)
- self.current_element = req.json()
- self._controls_answer()
- if self.caching:
- SimaEch.cache.update({req.url:
- Cache(self.current_element)})
-
- def _controls_answer(self):
- """Controls last.fm answer.