]> kaliko git repositories - mpd-sima.git/commitdiff
simaecho get_toptrack set artist mbid if missing
authorkaliko <kaliko@azylum.org>
Tue, 9 Dec 2014 13:19:21 +0000 (14:19 +0100)
committerkaliko <kaliko@azylum.org>
Tue, 9 Dec 2014 13:19:21 +0000 (14:19 +0100)
sima/lib/meta.py
sima/lib/simaecho.py

index 2d78f2f67269cfedfb2898a2aa3f3e1e025bc21d..78c6e790da198bc351e0c58c79a7307b5b5db2c0 100644 (file)
@@ -21,6 +21,7 @@
 Defines some object to handle audio file metadata
 """
 
+import logging
 import re
 
 UUID_RE = r'^[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89aAbB][a-f0-9]{3}-[a-f0-9]{12}$'
@@ -59,14 +60,19 @@ class Meta:
         self.__name = None #TODO: should be immutable
         self.__mbid = None
         self.__aliases = set()
+        self.log = logging.getLogger(__name__)
         if 'name' not in kwargs or not kwargs.get('name'):
             raise MetaException('Need a "name" argument')
         else:
             self.__name = kwargs.pop('name')
         if 'mbid' in kwargs and kwargs.get('mbid'):
-            is_uuid4(kwargs.get('mbid'))
+            try:
+                is_uuid4(kwargs.get('mbid'))
+                self.__mbid = kwargs.pop('mbid')
+            except WrongUUID4:
+                self.log.warning('Wrong mbid {}:{}'.format(self.__name,
+                                                         kwargs.get('mbid')))
             # mbid immutable as hash rests on
-            self.__mbid = kwargs.pop('mbid')
         self.__dict__.update(**kwargs)
 
     def __repr__(self):
index af557c5089b726ee482645540ff9c3cf09bdd437..1ee17fd6e2dbdd60decbf1c4a95c36dc8eaa952d 100644 (file)
@@ -21,7 +21,7 @@
 Consume EchoNest web service
 """
 
-__version__ = '0.0.4'
+__version__ = '0.0.5'
 __author__ = 'Jack Kaliko'
 
 
@@ -36,6 +36,14 @@ if len(ECH.get('apikey')) == 23:  # simple hack allowing imp.reload
     getws(ECH)
 
 
+def get_mbid(obj, foreign='foreign_ids'):
+    if foreign in obj:
+        for frgnid in obj.get(foreign):
+            if frgnid.get('catalog') == 'musicbrainz':
+                mbid = frgnid.get('foreign_id').split(':')[2]
+    return None
+
+
 class SimaEch:
     """EchoNest http client
     """
@@ -97,12 +105,7 @@ class SimaEch:
         ans = self.http(ressource, payload)
         self._controls_answer(ans.json())
         for art in ans.json().get('response').get('artists'):
-            mbid = None
-            if 'foreign_ids' in art:
-                for frgnid in art.get('foreign_ids'):
-                    if frgnid.get('catalog') == 'musicbrainz':
-                        mbid = frgnid.get('foreign_id'
-                                ).split(':')[2]
+            mbid = get_mbid(art)
             yield Artist(mbid=mbid, name=art.get('name'))
 
     def get_toptrack(self, artist=None):
@@ -120,6 +123,8 @@ class SimaEch:
                 }
         for song in ans.json().get('response').get('songs'):
             title = song.get('title')
+            if not art.get('musicbrainz_artistid'):
+                art['musicbrainz_artistid'] = get_mbid(song, 'artist_foreign_ids')
             if title not in titles:
                 titles.append(title)
                 yield Track(title=title, **art)