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}$'
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):
Consume EchoNest web service
"""
-__version__ = '0.0.4'
+__version__ = '0.0.5'
__author__ = 'Jack Kaliko'
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
"""
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):
}
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)