def parse_recording_data(data, info=None): track = {} if info is None else info.copy() try: track['title'] = data['title'] except KeyError: track['acoustid_id'] = data['id'] return {}, track if 'duration' in data: track['__length'] = audioinfo.strlength(data['duration']) track['acoustid_id'] = data['id'] track['artist'] = data.get('artists', [{'name': u""}])[0]['name'] if track['artist']: track['mbrainz_artist_id'] = data['artists'][0]['id'] if 'releases' in data: album_info = map(parse_release_data, data['releases']) else: album_info = [] track = dict((k,v) for k,v in track.iteritems() if not isempty(v)) if 'artist' in track: for album in album_info: if 'artist' not in album: album['artist'] = track['artist'] return album_info, track
def parse_track_list(node): tracks = [] for i, t in enumerate(parse_node(node, 'track-list', 'track', 'position')): track = t['recording'] rem_keys = set(track).union(TO_REMOVE) track.update((k,v) for k,v in t.iteritems() if k not in rem_keys) if u'puid-list' in track: track['musicip_puid'] = track['puid-list']['id'] del(track['puid-list']) if not isempty(track.get(u'relation-list')): for r in to_list(track['relation-list']): track.update(parse_track_relation(r)) feat = to_list(track.get('artist-credit', {}).get('name-credit')) if feat: names = [(z['artist']['name'], z.get('joinphrase', u'')) for z in feat] track['artist'] = u''.join('%s%s' % a for a in names) for k, v in track.items(): if not isinstance(track[k], (basestring, list)): del(track[k]) elif isinstance(v, list) and not isinstance(v[0], basestring): del(track[k]) if u'length' in track: track['length'] = strlength(int(track[u'length']) / 1000) tracks.append(convert_dict(track, TRACK_KEYS)) return tracks
def search(self, album, artists=u'', limit=40): if time.time() - self.__lasttime < 1000: time.sleep(1) ret = [] check_matches = False if isempty(artists): artist = None if len(artists) > 1: artist = u'Various Artists' elif artists: if hasattr(artists, 'items'): artist = artists.keys()[0] else: artist = artists[0] if not album and not artist: raise RetrievalError('Album or Artist required.') write_log(u'Searching for %s' % album) if hasattr(artists, "items"): album_id = find_id(chain(*artists.values()), "mbrainz_album_id") if album_id: try: write_log(translate("MusicBrainz", "Found album id %s in tracks. Retrieving") % album_id) return [retrieve_album(album_id)] except RetrievalError, e: msg = translate("MusicBrainz", "<b>Error:</b> While retrieving Album ID %1 (%2)") write_log(msg.arg(album_id).arg(escape(e)))
def parse_recording_data(data, info=None): track = {} if info is None else info.copy() try: track['title'] = data['title'] except KeyError: track['acoustid_id'] = data['id'] return {}, track if 'duration' in data: track['__length'] = audioinfo.strlength(data['duration']) track['acoustid_id'] = data['id'] track['artist'] = data.get('artists', [{'name': u""}])[0]['name'] if track['artist']: track['mbrainz_artist_id'] = data['artists'][0]['id'] if 'releases' in data: album_info = map(parse_release_data, data['releases']) else: album_info = [] track = dict((k, v) for k, v in track.iteritems() if not isempty(v)) if 'artist' in track: for album in album_info: if 'artist' not in album: album['artist'] = track['artist'] return album_info, track
def search(self, album, artists=u'', limit=40): if time.time() - self.__lasttime < 1000: time.sleep(1) ret = [] check_matches = False if isempty(artists): artist = None if len(artists) > 1: artist = u'Various Artists' elif artists: if hasattr(artists, 'items'): artist = artists.keys()[0] else: artist = artists[0] if not album and not artist: raise RetrievalError('Album or Artist required.') write_log(u'Searching for %s' % album) if hasattr(artists, "items"): album_id = find_id(chain(*artists.values()), "mbrainz_album_id") if album_id: try: write_log( translate("MusicBrainz", "Found album id %s in tracks. Retrieving") % album_id) return [retrieve_album(album_id)] except RetrievalError, e: msg = translate( "MusicBrainz", "<b>Error:</b> While retrieving Album ID %1 (%2)") write_log(msg.arg(album_id).arg(escape(e)))
def parse_track_list(node): tracks = [] for i, t in enumerate(parse_node(node, 'track-list', 'track', 'position')): track = t['recording'] rem_keys = set(track).union(TO_REMOVE) track.update((k, v) for k, v in t.iteritems() if k not in rem_keys) if u'puid-list' in track: track['musicip_puid'] = track['puid-list']['id'] del (track['puid-list']) if not isempty(track.get(u'relation-list')): for r in to_list(track['relation-list']): track.update(parse_track_relation(r)) feat = to_list(track.get('artist-credit', {}).get('name-credit')) if feat: names = [(z['artist']['name'], z.get('joinphrase', u'')) for z in feat] track['artist'] = u''.join('%s%s' % a for a in names) for k, v in track.items(): if not isinstance(track[k], (basestring, list)): del (track[k]) elif isinstance(v, list) and not isinstance(v[0], basestring): del (track[k]) if u'length' in track: track['length'] = strlength(int(track[u'length']) / 1000) tracks.append(convert_dict(track, TRACK_KEYS)) return tracks
def display_tag(tag): """Used to display tags in in a human parseable format.""" tag = dict((k,v) for k,v in tag.iteritems() if not k.startswith('#') and not isempty(v)) if not tag: return translate("WebDB", "<b>Nothing to display.</b>") fmt = u"<b>%s</b>: %s<br />" text = pprint_tag(tag, fmt, True) if text.endswith(u'<br />'): text = text[:-len(u'<br />')] return text
def display_tag(tag): """Used to display tags in in a human parseable format.""" tag = dict((k, v) for k, v in tag.iteritems() if not k.startswith('#') and not isempty(v)) if not tag: return translate("WebDB", "<b>Nothing to display.</b>") fmt = u"<b>%s</b>: %s<br />" text = pprint_tag(tag, fmt, True) if text.endswith(u'<br />'): text = text[:-len(u'<br />')] return text
def parse_release_data(rel): info = {} info['__numtracks'] = unicode(rel.get('track_count', '')) info['album'] = rel.get('title', u'') if 'date' in rel: date = rel['date'] info['year'] = u'-'.join(unicode(z).zfill(2) for z in map(date.get, ('year', 'month', 'day')) if z) info['country'] = rel.get('country', u'') info['discs'] = unicode(rel.get('medium_count', '')) info['#album_id'] = rel['id'] info['mbrainz_album_id'] = rel['id'] if 'mediums' in rel: info['track'] = unicode( rel['mediums'][0]['tracks'][0].get('position', "")) return dict((k,v) for k,v in info.iteritems() if not isempty(v))
def parse_release_data(rel): info = {} info['__numtracks'] = str(rel.get('track_count', '')) info['album'] = rel.get('title', '') if 'date' in rel: date = rel['date'] info['year'] = '-'.join( str(z).zfill(2) for z in map(date.get, ('year', 'month', 'day')) if z) info['country'] = rel.get('country', '') info['discs'] = str(rel.get('medium_count', '')) info['#album_id'] = rel['id'] info['mbrainz_album_id'] = rel['id'] if 'mediums' in rel: info['track'] = str(rel['mediums'][0]['tracks'][0].get('position', "")) return dict((k, v) for k, v in info.items() if not isempty(v))
def search(self, album, artists='', limit=40): if time.time() - self.__lasttime < 1000: time.sleep(1) ret = [] check_matches = False if isempty(artists): artist = None if len(artists) > 1: artist = 'Various Artists' elif artists: if hasattr(artists, 'items'): artist = list(artists.keys())[0] else: artist = artists[0] if not album and not artist: raise RetrievalError('Album or Artist required.') write_log('Searching for %s' % album) if hasattr(artists, "items"): album_id = find_id(chain(*list(artists.values())), "mbrainz_album_id") if album_id: try: write_log( translate("MusicBrainz", "Found album id %s in tracks. Retrieving") % album_id) return [retrieve_album(album_id)] except RetrievalError as e: msg = translate( "MusicBrainz", "<b>Error:</b> While retrieving Album ID %1 (%2)") write_log(msg.arg(album_id).arg(escape(e))) try: xml = urlopen(search_album(album, artist, limit)) except urllib.error.URLError as e: write_log('Error: While retrieving search page %s' % str(e)) raise RetrievalError(str(e)) write_log('Retrieved search results.') self.__lasttime = time.time() return parse_album_search(xml)
def convert_dict(d, fm): return dict((fm.get(k, k), v) for k, v in d.iteritems() if not isempty(v))