From: kaliko Date: Sat, 22 Feb 2014 00:36:08 +0000 (+0100) Subject: Add ETag support for simafm X-Git-Tag: mpd-sima/0.12.0pr4~9 X-Git-Url: https://git.kaliko.me/?a=commitdiff_plain;h=63563d000f2646724c1ff91eb6fcca01f5201d23;p=mpd-sima.git Add ETag support for simafm --- diff --git a/sima/__init__.py b/sima/__init__.py index 1ceac62..267899c 100644 --- a/sima/__init__.py +++ b/sima/__init__.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- """WebServices API credentials and ressources """ +from datetime import timedelta LFM = {'apikey': 'NG4xcDlxcXJwMjk4MTZycTgwM3E3b3I5MTEzb240cG8', 'host':'ws.audioscrobbler.com', @@ -12,5 +13,7 @@ ECH = {'apikey': 'WlRKQkhTS0JHWFVDUEZZRFA', 'version': 'v4', } +WAIT_BETWEEN_REQUESTS = timedelta(0, 2) +SOCKET_TIMEOUT = 6 # vim: ai ts=4 sw=4 sts=4 expandtab diff --git a/sima/lib/simaecho.py b/sima/lib/simaecho.py index 2706513..e5fe485 100644 --- a/sima/lib/simaecho.py +++ b/sima/lib/simaecho.py @@ -25,11 +25,9 @@ __version__ = '0.0.2' __author__ = 'Jack Kaliko' -from datetime import timedelta - from requests import Session, Request, Timeout, ConnectionError -from sima import ECH +from sima import ECH, SOCKET_TIMEOUT, WAIT_BETWEEN_REQUESTS from sima.lib.meta import Artist from sima.lib.track import Track from sima.lib.http import CacheController @@ -38,10 +36,6 @@ from sima.utils.utils import getws, Throttle if len(ECH.get('apikey')) == 23: # simple hack allowing imp.reload getws(ECH) -# Some definitions -WAIT_BETWEEN_REQUESTS = timedelta(0, 2) -SOCKET_TIMEOUT = 6 - class SimaEch: """EchoNest http client diff --git a/sima/lib/simafm.py b/sima/lib/simafm.py index cc70cf7..04e736f 100644 --- a/sima/lib/simafm.py +++ b/sima/lib/simafm.py @@ -25,11 +25,10 @@ __version__ = '0.5.1' __author__ = 'Jack Kaliko' -from datetime import timedelta from requests import Session, Request, Timeout, ConnectionError -from sima import LFM +from sima import LFM, SOCKET_TIMEOUT, WAIT_BETWEEN_REQUESTS from sima.lib.meta import Artist from sima.lib.http import CacheController @@ -38,10 +37,6 @@ from sima.utils.utils import getws, Throttle if len(LFM.get('apikey')) == 43: # simple hack allowing imp.reload getws(LFM) -# Some definitions -WAIT_BETWEEN_REQUESTS = timedelta(0, 2) -SOCKET_TIMEOUT = 6 - class SimaFM: """Last.fm http client @@ -49,9 +44,12 @@ class SimaFM: root_url = 'http://{host}/{version}/'.format(**LFM) ratelimit = None name = 'Last.fm' - cache = {} + cache = False + stats = {'etag':0, + 'ccontrol':0, + 'total':0} - def __init__(self, cache=True): + def __init__(self): self.controller = CacheController(self.cache) self.artist = None @@ -62,9 +60,11 @@ class SimaFM: """ req = Request('GET', SimaFM.root_url, params=payload, ).prepare() + SimaFM.stats.update(total=SimaFM.stats.get('total')+1) if self.cache: cached_response = self.controller.cached_request(req.url, req.headers) if cached_response: + SimaFM.stats.update(ccontrol=SimaFM.stats.get('ccontrol')+1) return cached_response.json() try: return self._fetch_ws(req) @@ -79,8 +79,10 @@ class SimaFM: """fetch from web service""" sess = Session() resp = sess.send(prepreq, timeout=SOCKET_TIMEOUT) - #self.__class__.ratelimit = resp.headers.get('x-ratelimit-remaining', None) - if resp.status_code is not 200: + if resp.status_code == 304: + SimaFM.stats.update(etag=SimaFM.stats.get('etag')+1) + resp = self.controller.update_cached_response(prepreq, resp) + elif resp.status_code != 200: raise WSHTTPError('{0.status_code}: {0.reason}'.format(resp)) ans = resp.json() self._controls_answer(ans) diff --git a/sima/lib/webserv.py b/sima/lib/webserv.py index 438637a..c4118c0 100644 --- a/sima/lib/webserv.py +++ b/sima/lib/webserv.py @@ -195,8 +195,6 @@ class WebService(Plugin): self.log.warning('{0}: {1}'.format(self.ws.name, err)) if as_art: self.log.debug('Fetched {0} artist(s)'.format(len(as_art))) - if self.ws.ratelimit: - self.log.info('{0.name} ratelimit: {0.ratelimit}'.format(self.ws)) return as_art def get_recursive_similar_artist(self): @@ -383,6 +381,7 @@ class WebService(Plugin): self.log.debug(repr(self.player.current)) return None self.queue_mode() + self.log.debug(self.ws.stats) candidates = self.to_add self.to_add = list() if self.plugin_conf.get('queue_mode') != 'album': diff --git a/sima/plugins/internal/echonest.py b/sima/plugins/internal/echonest.py index 1c0b1f7..c9a02e8 100644 --- a/sima/plugins/internal/echonest.py +++ b/sima/plugins/internal/echonest.py @@ -40,15 +40,8 @@ class EchoNest(WebService): WebService.__init__(self, daemon) # Set persitent cache vardir = daemon.config['sima']['var_dir'] - SimaEch.cache = FileCache(join(vardir, 'http')) + SimaEch.cache = FileCache(join(vardir, 'http', 'EchoNest')) self.ws = SimaEch - def callback_playlist(self): - if self.player.state != 'play': - return - msg = 'ETag:{etag:>3d}, Cache-Control:{ccontrol:>3d},' - msg += 'total:{total:>3d}, min(rate-limit):{minrl:>3d}' - self.log.debug(msg.format(**SimaEch.stats)) - # VIM MODLINE # vim: ai ts=4 sw=4 sts=4 expandtab diff --git a/sima/plugins/internal/lastfm.py b/sima/plugins/internal/lastfm.py index 2e6fca6..ba10a48 100644 --- a/sima/plugins/internal/lastfm.py +++ b/sima/plugins/internal/lastfm.py @@ -22,12 +22,14 @@ Fetching similar artists from last.fm web services """ # standard library import +from os.path import join # third parties components # local import from ...lib.simafm import SimaFM from ...lib.webserv import WebService +from ...lib.cache import FileCache class Lastfm(WebService): @@ -37,6 +39,9 @@ class Lastfm(WebService): def __init__(self, daemon): WebService.__init__(self, daemon) self.ws = SimaFM + # Set persitent cache + vardir = daemon.config['sima']['var_dir'] + SimaFM.cache = FileCache(join(vardir, 'http', 'LastFM')) # VIM MODLINE