]> kaliko git repositories - mpd-sima.git/commitdiff
Add toptrack method for Last.fm webservice
authorkaliko <efrim@azylum.org>
Sat, 22 Feb 2014 01:32:44 +0000 (02:32 +0100)
committerkaliko <efrim@azylum.org>
Sat, 22 Feb 2014 01:32:44 +0000 (02:32 +0100)
sima/lib/cache.py
sima/lib/simafm.py

index d7175617c171849dfcef0a4a47a93a8072707f09..c4170de8c1b1f5885642105f67c86fb3b18bf287 100644 (file)
@@ -35,13 +35,16 @@ from ..utils.filelock import FileLock
 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):
@@ -72,8 +75,8 @@ class FileCache:
         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))
@@ -86,15 +89,15 @@ class FileCache:
     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'))
index 04e736f63b1bd4478d35f6f74107720e19d93022..8d82d52ccc90798b4a933d53a7f3737fc4945423 100644 (file)
@@ -30,6 +30,7 @@ from requests import Session, Request, Timeout, ConnectionError
 
 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
@@ -144,6 +145,26 @@ class SimaFM:
         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