def get_basic_artist_pictures(mbid): """ Make sure document and its dependencies are present and contain required data. @param mbid: a string containing a MusicBrainz ID of an artist @return: a CachedArtistPictures object containing minimal data set """ try: artist_pictures = CachedArtistPictures.get(mbid) if 'artist_name' not in artist_pictures: raise ResourceNotFound mmda_logger('db','present',artist_pictures._doc_type, artist_pictures.get_id) except ResourceNotFound: # overhead, but in most cases artist page # is a place where user will go next anyway artist_pictures = CachedArtistPictures.get_or_create(mbid) artist = get_basic_artist(mbid) artist_pictures.artist_name = artist.name if 'aliases' in artist: artist_pictures.artist_aliases = list(artist.aliases) artist_pictures.save() mmda_logger('db','store', artist_pictures) return artist_pictures
def populate_artist_lastfm(artist): """ Make sure all required and avaiable last.fm data is present in a CachedArtist document. @param artist: a CachedArtist object @return: a validated/updated CachedArtist object """ if 'lastfm' not in artist.cache_state: lastfm = pylast.get_lastfm_network(api_key = settings.LASTFM_API_KEY) lastfm.enable_caching() try: t = mmda_logger('last','request','artist-data',artist._id) lastfm_artist = lastfm.get_artist_by_mbid(artist._id) # TODO: run there in parallel (?) lastfm_images = Future(lastfm_artist.get_images,pylast.IMAGES_ORDER_POPULARITY,5) lastfm_url = Future(lastfm_artist.get_url) # we get similar artists from lastfm database, but only those with mbid (omg, omg) # TODO: think about numbers of fetched things lastfm_similar = Future(_lastfm_get_similar_optimized,lastfm_artist,10) lastfm_tags = Future(lastfm_artist.get_top_tags,10) lastfm_abstract = None if 'abstract' not in artist: lastfm_abstract = lastfm_artist.get_bio_summary() # wait for all Future to come ;-) lastfm_url() lastfm_tags() lastfm_images() lastfm_similar() mmda_logger('last','result','artist-data',artist._id,t) except Exception, e: mmda_logger('pylast','ERROR',e) else: # TODO: make it compatible with tags imported from mb (TODO2: add tags from MusicBrainz) # TODO: remove random? import random random.shuffle(lastfm_tags()) if lastfm_abstract: artist.abstract = {'content':strip_tags(lastfm_abstract), 'lang':'en', 'provider':'Last.fm', 'url':lastfm_url()} artist.tags = [(t.item.name.lower(), int( float(t.weight)/(float(100)/float(4)) ) ) for t in lastfm_tags()] artist.similar = lastfm_similar() artist.urls['Last.fm'] = [lastfm_url()] # TODO: optimize if lastfm_images(): artist_pictures = CachedArtistPictures.get_or_create(artist._id) if 'lastfm' not in artist_pictures: artist_pictures.artist_name = artist.name artist_pictures.lastfm = [ {'sq':i.sizes.largesquare, 'big':i.sizes.original, 'url':i.url,'title':i.title} for i in lastfm_images()] artist_pictures.cache_state['lastfm'] = [1,datetime.utcnow()] artist_pictures.save() mmda_logger('db','store',artist_pictures) # if fail, store state too -- to avoid future attempts artist.cache_state['lastfm'] = [1,datetime.utcnow()] artist.changes_present = True