def get_object(self, bits): try: # bits = "uri_artist/news/mbid/" mbid = bits[2] artist = get_basic_artist(mbid) self.news_stream, self.news_sources = get_populated_artist_news(artist) except: raise FeedDoesNotExist else: return artist
def get_basic_release(mbid): """ Make sure release and its dependencies are present and contain required data. @param mbid: a string containing a MusicBrainz ID of an artist @return: a CachedReleaseGroup object containing required minimal data set """ release_group = CachedReleaseGroup.view('artists/releases',include_docs=True, key=mbid).one() if not release_group: # TODO: optimize? its just one additional request on rare ocassions tho.. try: t = mmda_logger('mb','request','artist mbid of release',mbid) mb_release = mb_query.getReleaseById(mbid, MB_RELEASE_ARTIST) artist_mbid = extractUuid(mb_release.artist.id) mmda_logger('mb','result','artist mbid',artist_mbid,t) except WebServiceError, e: # TODO: add error handling here mmda_logger('mb-release','ERROR',e) raise e else: get_basic_artist(artist_mbid) release_group = CachedReleaseGroup.view('artists/releases',include_docs=True, key=mbid).one()
def show_artist_refresh(request, uri_artist, mbid): """ Show reset page of an artist specified by mbid. If request contains POST data, perform reset. @param mbid: a string containing a MusicBrainz ID of an artist @param uri_artist: a string containing SEO-friendly artist name @return: a rendered artist page """ if request.POST: mbid = request.POST['mbid'] reset = request.POST.getlist('reset') rc_ip = request.META['REMOTE_ADDR'] rc_token = request.POST['recaptcha_challenge_field'] rc_input = request.POST['recaptcha_response_field'] captcha = rc.submit(rc_token, rc_input, settings.RECAPTCHA_PRIV_KEY, rc_ip) if captcha.is_valid: delete_memcached_keys(get_basic_artist(mbid)) for db in reset: try: del get_db(db)[mbid] except: continue if 'artists' in reset: release_groups = get_db('artists').view('artists/release_groups', key=mbid) for group in release_groups: del get_db('artists')[group['id']] # TODO: remove 'lastfm' from artist cache_state if pictures were removed. artist = get_basic_artist(mbid) artist_cache_state = get_artist_cache_state(mbid) html_for_captcha = get_captcha_html(rc.API_SERVER) return render_to_response('artists/show_artist_refresh.html', locals())
def show_artist_news(request, uri_artist, mbid): """ Show page with news related to a specified artist. @param mbid: a string containing a MusicBrainz ID of an artist @param uri_artist: a string containing SEO-friendly artist name @return: a rendered news page or a redirection to a proper URL """ artist = get_basic_artist(mbid) news_stream, news_sources = get_populated_artist_news(artist) artist.save_any_changes() # basic SEO check artist_seo_name = slugify2(artist.name) if uri_artist == artist_seo_name: return render_to_response('news/show_artist_news.html', locals()) else: return HttpResponsePermanentRedirect(reverse('show-artist-news', args=(artist_seo_name, mbid)))
def get_basic_artist_videos(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 CachedArtistVideos object containing minimal data set """ try: artist_videos = CachedArtistVideos.get(mbid) if 'artist_name' not in artist_videos: raise ResourceNotFound except ResourceNotFound: # overhead, but in most cases artist page # is a place where user will go next anyway artist = get_basic_artist(mbid) # TODO: just an idea: create a view that store only names and aliases? artist_videos = CachedArtistVideos.get_or_create(mbid) artist_videos.artist_name = artist.name if 'aliases' in artist: artist_videos.artist_aliases = list(artist.aliases) artist_videos.save() mmda_logger('db','store', artist_videos) return artist_videos
def populate_artist_videos_youtube(artist_videos): """ Make sure all avaiable youtube meta-data is present in a CachedArtistVideos document. @param artist_videos: a CachedArtistVideos object @return: a validated/updated CachedArtistVideos object """ # TODO: expire in one week? if "youtube" not in artist_videos.cache_state: youtube_videos = [] yt_service = yts.YouTubeService() artist = get_basic_artist(artist_videos._id) try: t = mmda_logger("yt", "request", "artist-videos", artist_videos.artist_name) # check if artist has dedicated Youtube channel if "urls" in artist and "Youtube" in artist.urls: artist_videos.youtube_channel = artist.urls["Youtube"][0] youtube_id = _get_youtube_id(artist) feed = yt_service.GetYouTubeVideoFeed( "http://gdata.youtube.com/feeds/api/users/%s/uploads" % youtube_id ) # if there is no official channel, make a search query else: query = yts.YouTubeVideoQuery() query.orderby = "relevance" query.racy = "exclude" query.max_results = YOUTUBE_MAX_RESULTS query.categories.append("Music") # 'bug' workaround (http://bugs.python.org/issue1712522) query.vq = artist_videos.artist_name.encode("utf-8", "/") # TODO: aliases? atm they seems to lower the result quality # query.vq = u"Múm OR Múm OR mum OR múm".encode('utf-8', '/') feed = yt_service.YouTubeQuery(query) except Exception, e: mmda_logger("yt-search", "ERROR", e) # raise Http500 else: mmda_logger("yt", "result", "artist-videos", len(feed.entry), t) for entry in feed.entry: try: video = { "title": entry.media.title.text, "duration": entry.media.duration.seconds, "url": entry.media.player.url, "player": entry.GetSwfUrl(), "thumb": entry.media.thumbnail[0].url, } # sometimes objects we have are wicked -- we reject them # eg. when official channel contains blocked in some regions videos # example: http://www.youtube.com/user/dreamtheater # TODO: what if there is no videos left? # example: http://127.0.0.1:8000/artist/the-beatles/videos/b10bbbfc-cf9e-42e0-be17-e2c3e1d2600d/ except (NameError, AttributeError): continue else: youtube_videos.append(video) if youtube_videos: artist_videos.youtube = youtube_videos artist_videos.cache_state["youtube"] = [1, datetime.utcnow()] artist_videos.changes_present = True mmda_logger("db", "store", artist_videos)