def get(self): """ Search for artists and tracks on MusicBrainz and return JSON. TODO: Search for tracks as well as combine queries ("artist album") """ mb = MusicbrainzClient() query = self.get_argument('q') artists, tracks = yield [mb.search_artists(query), mb.search_tracks(query)] data = { 'artists': [ { 'id': artist['id'], 'artist': artist['name'], 'note': artist.get('disambiguation', '') } for artist in artists['artist-list'] ], 'tracks': [ { 'id': track['id'], 'title': track['title'], 'artist': track['artist-credit-phrase'] } for track in tracks['recording-list'] ] } self.finish(data)
def get(self, mbid): """Return an album track list as JSON. :param mbid: the album's MusicBrainz ID """ mb = MusicbrainzClient() response = yield mb.lookup_release(mbid) release = response['release'] track_list = release['medium-list'][0]['track-list'] tracks = [] for number, track in enumerate(track_list, start=1): recording = track['recording'] tracks.append({ 'id': recording['id'], 'number': number, 'title': recording['title'], 'album': release['title'] }) self.finish(json.dumps(tracks))
def get(self, mbid): """Return artist's info and its discography as JSON. :param mbid: the artist's MusicBrainz ID """ cache_key = 'cache:artist:%s' % mbid cached = yield Task(async.redis.get, cache_key) if cached: self.finish(cached) return mb = MusicbrainzClient() lf = AsyncLastfmClient() mb_artist = yield mb.lookup_artist(mbid) mb_artist = mb_artist['artist'] lf_artist, lf_similar, lf_albums = yield [ lf.artist.get_info(artist=mb_artist['name'], mbid=mbid), lf.artist.get_similar(artist=mb_artist['name'], mbid=mbid), lf.artist.get_top_albums(artist=mb_artist['name'], mbid=mbid) ] # FIXME: the following sometimes fails. # assert lf_artist['mbid'] == mbid similar = [] if not isinstance(lf_similar['artist'], list) else [ serialise_lastfm_artist(similar_artist) for similar_artist in lf_similar['artist'] if similar_artist['mbid'] ] albums = lf_albums.get('album', []) if isinstance(albums, dict): albums = [albums] data = { 'id': mbid, 'name': mb_artist['name'], 'albums': [ { 'id': album['mbid'], 'name': album['name'], 'year': '(year)', #parse_year(album['first-release-date']), 'type': '(type)', #album.get('type'), 'subtypes': [], #album.get('secondary-type-list'), 'images': { img['size']: img['#text'] for img in album['image'] } } for album in albums if album['mbid'] #if 'secondary-type-list' not in album ], 'bio': lf_artist['bio']['content'].strip(), 'images': { img['size']: img['#text'] for img in lf_artist['image'] }, 'similar': similar } data['albums'].sort(key=itemgetter('year'), reverse=True) data_json = json.dumps(data) self.finish(data) yield Task(async.redis.set, cache_key, data_json)