From e9ed5c171c9251ef6ae7765b1406e2f5b2cb1c0d Mon Sep 17 00:00:00 2001 From: kaliko Date: Tue, 9 Dec 2014 14:19:21 +0100 Subject: [PATCH] simaecho get_toptrack set artist mbid if missing --- sima/lib/meta.py | 10 ++++++++-- sima/lib/simaecho.py | 19 ++++++++++++------- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/sima/lib/meta.py b/sima/lib/meta.py index 2d78f2f..78c6e79 100644 --- a/sima/lib/meta.py +++ b/sima/lib/meta.py @@ -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): diff --git a/sima/lib/simaecho.py b/sima/lib/simaecho.py index af557c5..1ee17fd 100644 --- a/sima/lib/simaecho.py +++ b/sima/lib/simaecho.py @@ -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) -- 2.39.2