public async Task <OperationResult <IEnumerable <ArtistSearchResult> > > PerformArtistSearch(string query, int resultsCount) { try { var cacheKey = $"uri:lastfm:artistsearch:{ query.ToAlphanumericName() }"; var data = await CacheManager.GetAsync <ArtistSearchResult>(cacheKey, async() => { Logger.LogTrace("LastFmHelper:PerformArtistSearch:{0}", query); var auth = new LastAuth(ApiKey.Key, ApiKey.KeySecret); var albumApi = new ArtistApi(auth); var response = await albumApi.GetInfoAsync(query); if (!response.Success) { return(null); } var lastFmArtist = response.Content; var result = new ArtistSearchResult { ArtistName = lastFmArtist.Name, LastFMId = lastFmArtist.Id, MusicBrainzId = lastFmArtist.Mbid, Bio = lastFmArtist.Bio != null ? lastFmArtist.Bio.Content : null }; if (lastFmArtist.Tags != null) { result.Tags = lastFmArtist.Tags.Select(x => x.Name).ToList(); } // No longer fetching/consuming images LastFm says is violation of ToS ; https://getsatisfaction.com/lastfm/topics/api-announcement-dac8oefw5vrxq if (lastFmArtist.Url != null) { result.Urls = new[] { lastFmArtist.Url.ToString() }; } return(result); }, "uri:metadata"); return(new OperationResult <IEnumerable <ArtistSearchResult> > { IsSuccess = data != null, Data = new[] { data } }); } catch (Exception ex) { Logger.LogError(ex, ex.Serialize()); } return(new OperationResult <IEnumerable <ArtistSearchResult> >()); }
public async Task <OperationResult <IEnumerable <ArtistSearchResult> > > PerformArtistSearch(string query, int resultsCount) { try { this.Logger.LogTrace("LastFmHelper:PerformArtistSearch:{0}", query); var auth = new LastAuth(this.ApiKey.Key, this.ApiKey.KeySecret); var albumApi = new ArtistApi(auth); var response = await albumApi.GetInfoAsync(query); if (!response.Success) { return(new OperationResult <IEnumerable <ArtistSearchResult> >()); } var lastFmArtist = response.Content; var result = new ArtistSearchResult { ArtistName = lastFmArtist.Name, LastFMId = lastFmArtist.Id, MusicBrainzId = lastFmArtist.Mbid, Bio = lastFmArtist.Bio != null ? lastFmArtist.Bio.Content : null }; if (lastFmArtist.Tags != null) { result.Tags = lastFmArtist.Tags.Select(x => x.Name).ToList(); } if (lastFmArtist.MainImage != null && (lastFmArtist.MainImage.ExtraLarge != null || lastFmArtist.MainImage.Large != null)) { result.ArtistThumbnailUrl = (lastFmArtist.MainImage.ExtraLarge ?? lastFmArtist.MainImage.Large).ToString(); } if (lastFmArtist.Url != null) { result.Urls = new string[] { lastFmArtist.Url.ToString() }; } return(new OperationResult <IEnumerable <ArtistSearchResult> > { IsSuccess = response.Success, Data = new List <ArtistSearchResult> { result } }); } catch (Exception ex) { this.Logger.LogError(ex, ex.Serialize()); } return(new OperationResult <IEnumerable <ArtistSearchResult> >()); }