class BaseCache:
def get(self, key):
- raise NotImplemented()
+ """Get cache value"""
+ raise NotImplementedError
def set(self, key, value):
- raise NotImplemented()
+ """Set cache value"""
+ raise NotImplementedError
def delete(self, key):
- raise NotImplemented()
+ """Remove cache value"""
+ raise NotImplementedError
class DictCache(BaseCache):
if not os.path.isdir(self.directory):
os.mkdir(self.directory)
- def encode(self, x):
- return md5(x.encode('utf-8')).hexdigest()
+ def encode(self, val):
+ return md5(val.encode('utf-8')).hexdigest()
def _fn(self, name):
return os.path.join(self.directory, self.encode(name))
def set(self, key, value):
name = self._fn(key)
with FileLock(name):
- with codecs.open(name, 'w+b') as fh:
- dump(value, fh)
+ with codecs.open(name, 'w+b') as flh:
+ dump(value, flh)
def delete(self, key):
if not self.forever:
os.remove(self._fn(key))
def __iter__(self):
- for dirpath, dirnames, filenames in os.walk(self.directory):
+ for dirpath, _, filenames in os.walk(self.directory):
for item in filenames:
name = os.path.join(dirpath, item)
yield load(codecs.open(name, 'rb'))
from sima import LFM, SOCKET_TIMEOUT, WAIT_BETWEEN_REQUESTS
from sima.lib.meta import Artist
+from sima.lib.track import Track
from sima.lib.http import CacheController
from sima.utils.utils import WSError, WSNotFound, WSTimeout, WSHTTPError
for art in ans.get('similarartists').get('artist'):
yield Artist(name=art.get('name'), mbid=art.get('mbid', None))
+ def get_toptrack(self, artist=None):
+ """Fetch artist top tracks
+ """
+ payload = self._forge_payload(artist, method='top')
+ ans = self._fetch(payload)
+ tops = ans.get('toptracks').get('track')
+ art = {
+ 'artist': artist.name,
+ 'musicbrainz_artistid': artist.mbid,
+ }
+ for song in tops:
+ for key in ['artist', 'streamable', 'listeners',
+ 'url', 'image', '@attr']:
+ if key in song:
+ song.pop(key)
+ song.update(art)
+ song.update(title=song.pop('name'))
+ song.update(time=song.pop('duration'))
+ yield Track(**song)
+ #return tops
# VIM MODLINE
# vim: ai ts=4 sw=4 sts=4 expandtab