private async Task FillSimilarArtistsAsync() { if (this.lfmArtist != null && this.lfmArtist.SimilarArtists != null && this.lfmArtist.SimilarArtists.Count > 0) { await Task.Run(async() => { var localSimilarArtists = new ObservableCollection <SimilarArtistViewModel>(); foreach (LastFmArtist similarArtist in this.lfmArtist.SimilarArtists) { string artistImageUrl = string.Empty; try { // Last.fm was so nice to break their artist image API. So we need to get images from elsewhere. LastFmArtist lfmArtist = await LastfmApi.ArtistGetInfo(similarArtist.Name, true, ResourceUtils.GetString("Language_ISO639-1")); artistImageUrl = await FanartApi.GetArtistThumbnailAsync(lfmArtist.MusicBrainzId); } catch (Exception ex) { LogClient.Warning($"Could not get artist image from Fanart for artist {similarArtist.Name}. Exception: {ex}"); } localSimilarArtists.Add(new SimilarArtistViewModel { Name = similarArtist.Name, Url = similarArtist.Url, ImageUrl = artistImageUrl }); } this.SimilarArtists = localSimilarArtists; }); } RaisePropertyChanged(nameof(this.SimilarArtists)); RaisePropertyChanged(nameof(this.HasSimilarArtists)); }
public async Task SetArtistInformation(LastFmArtist lfmArtist, string artistImageUrl) { this.lfmArtist = lfmArtist; RaisePropertyChanged(nameof(this.ArtistName)); RaisePropertyChanged(nameof(this.Biography)); RaisePropertyChanged(nameof(this.HasBiography)); RaisePropertyChanged(nameof(this.CleanedBiographyContent)); RaisePropertyChanged(nameof(this.Url)); RaisePropertyChanged(nameof(this.UrlText)); await this.FillSimilarArtistsAsync(); await this.FillImageAsync(artistImageUrl); }
public async Task <LastFmTrack> GetTrackInfo(string artistName, string name) { var request = WebRequest.CreateHttp($"{ApiBase}/?method=track.getInfo&api_key={Key}&artist={Uri.EscapeDataString(artistName)}&track={Uri.EscapeDataString(name)}&format=json&username={User}"); request.Timeout = (int)RequestTimeout.TotalMilliseconds; using (var response = (HttpWebResponse)await request.GetResponseAsync()) using (var reader = new StreamReader(response.GetResponseStream())) { var text = await reader.ReadToEndAsync(); var track = JObject.Parse(text)?["track"]; if (track == null) { return(null); } var artist = new LastFmArtist((string)track["artist"]["name"]); var album = new LastFmAlbum((string)track["album"]?["title"], artist, imageUri: GetLargestImage(track["album"]?["image"])); return(new LastFmTrack((string)track["name"], album, (int?)track["userplaycount"])); } }
public async Task LastFmAgentGetArtistByMbid() { LastFmAgent agent = new LastFmAgent(Secret.LastFmAuth); LastFmArtist art = await agent.GetArtistbyMbid("83d91898-7763-47d7-b03b-b92132375c47"); LastFmApiExceptionArgs error = null; try { await agent.GetArtistbyMbid("NULL"); } catch (LastFmApiException exception) { error = exception.Args; } Assert.IsNotNull(error, "Error is null"); Assert.IsNotNull(art, "Artist is null"); Assert.AreEqual(art.Name, "Pink Floyd"); Assert.AreEqual(6, error.ErrorCode); }
private async Task ShowArtistInfoAsync(TrackViewModel track, bool forceReload) { this.previousArtistName = this.artistName; // User doesn't want to download artist info, or no track is selected. if (!SettingsClient.Get <bool>("Lastfm", "DownloadArtistInformation") || track == null) { this.ArtistInfoViewModel = this.container.Resolve <ArtistInfoViewModel>(); this.artistName = string.Empty; return; } // Artist name is unknown if (string.IsNullOrEmpty(track.ArtistName)) { ArtistInfoViewModel localArtistInfoViewModel = this.container.Resolve <ArtistInfoViewModel>(); await localArtistInfoViewModel.SetArtistInformation(new LastFmArtist { Name = string.Empty }, string.Empty); this.ArtistInfoViewModel = localArtistInfoViewModel; this.artistName = string.Empty; return; } this.artistName = track.ArtistName; // The artist didn't change: leave the previous artist info. if (this.artistName.Equals(this.previousArtistName) & !forceReload) { return; } // The artist changed: we need to show new artist info. string artworkPath = string.Empty; this.IsBusy = true; try { LastFmArtist lfmArtist = await LastfmApi.ArtistGetInfo(track.ArtistName, true, ResourceUtils.GetString("Language_ISO639-1")); if (lfmArtist != null) { if (string.IsNullOrEmpty(lfmArtist.Biography.Content)) { // In case there is no localized Biography, get the English one. lfmArtist = await LastfmApi.ArtistGetInfo(track.ArtistName, true, "EN"); } if (lfmArtist != null) { string artistImageUrl = string.Empty; try { // Last.fm was so nice to break their artist image API. So we need to get images from elsewhere. artistImageUrl = await FanartApi.GetArtistThumbnailAsync(lfmArtist.MusicBrainzId); } catch (Exception ex) { LogClient.Warning($"Could not get artist image from Fanart for artist {track.ArtistName}. Exception: {ex}"); } ArtistInfoViewModel localArtistInfoViewModel = this.container.Resolve <ArtistInfoViewModel>(); await localArtistInfoViewModel.SetArtistInformation(lfmArtist, artistImageUrl); this.ArtistInfoViewModel = localArtistInfoViewModel; } else { throw new Exception("lfmArtist == null"); } } } catch (Exception ex) { LogClient.Error("Could not show artist information for Track {0}. Exception: {1}", track.Path, ex.Message); this.ArtistInfoViewModel = this.container.Resolve <ArtistInfoViewModel>(); this.artistName = string.Empty; } this.IsBusy = false; }
public async Task <IEnumerable <LastFmArtist> > GetRecommendedArtists(int count = 0) { var parameters = new Dictionary <string, string>(); if (count > 0) { parameters.Add("limit", count.ToString()); } parameters.Add("api_key", _lastFm.ApiKey); parameters.Add("sk", _lastFm.SessionKey); parameters.Add("api_sig", LastFmUtils.BuildSig(_lastFm.ApiSecret, "user.getRecommendedArtists", parameters)); var response = await new CoreRequest(new Uri(LastFmConst.UrlBase), parameters, "POST").Execute(); LastFmErrorProcessor.ProcessError(response); if (response.SelectToken("recommendations.artist") != null) { return(from a in response.SelectToken("recommendations.artist") select LastFmArtist.FromJson(a)); } return(null); }
private string FormatArtistLink(LastFmArtist artist, bool trim = false) => FormatLink(artist?.Name ?? "Unknown", artist?.Url, trim);
public async Task ArtistGetInfoTest() { LastFmArtist lfmArtist = await Core.Api.Lastfm.LastfmApi.ArtistGetInfo("Coldplay", false, string.Empty); Assert.IsTrue(!string.IsNullOrEmpty(lfmArtist.Name) & !string.IsNullOrEmpty(lfmArtist.Url)); }