# -*- coding: utf-8 -*-
"""WebServices API credentials and ressources
"""
+from datetime import timedelta
LFM = {'apikey': 'NG4xcDlxcXJwMjk4MTZycTgwM3E3b3I5MTEzb240cG8',
'host':'ws.audioscrobbler.com',
'version': 'v4',
}
+WAIT_BETWEEN_REQUESTS = timedelta(0, 2)
+SOCKET_TIMEOUT = 6
# vim: ai ts=4 sw=4 sts=4 expandtab
__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
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
__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
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
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
"""
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)
"""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)
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):
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':
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
"""
# 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):
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