public async Task <LastFmArtist> GetInfo(string mbid, string artist) { var parameters = new Dictionary <string, string>(); if (!string.IsNullOrEmpty(mbid)) { parameters.Add("mbid", mbid); } else { parameters.Add("artist", artist); } if (!string.IsNullOrEmpty(_lastFm.Lang)) { parameters.Add("lang", _lastFm.Lang); } parameters.Add("api_key", _lastFm.ApiKey); var response = await(new CoreRequest(new Uri(LastFmConst.MethodBase + "artist.getInfo"), parameters).Execute()); LastFmErrorProcessor.ProcessError(response); if (response["artist"] != null) { return(LastFmArtist.FromJson(response["artist"])); } return(null); }
public async Task <List <LastFmArtist> > Search(string artist, int limit = 0) { var parameters = new Dictionary <string, string>(); parameters.Add("artist", artist); if (limit > 0) { parameters.Add("limit", limit.ToString()); } parameters.Add("api_key", _lastFm.ApiKey); var response = await(new CoreRequest(new Uri(LastFmConst.MethodBase + "artist.search"), parameters).Execute()); LastFmErrorProcessor.ProcessError(response); if (response.SelectToken("results.artistmatches.artist") != null) { var artistJson = response.SelectToken("results.artistmatches.artist"); if (artistJson is JArray) { return((from a in response.SelectToken("results.artistmatches.artist") select LastFmArtist.FromJson(a)).ToList()); } else { return new List <LastFmArtist>() { LastFmArtist.FromJson(artistJson) } }; } return(null); }
public async Task <List <LastFmArtist> > GetSimilar(string artist, int count = 0) { var parameters = new Dictionary <string, string>(); parameters.Add("artist", artist); if (count > 0) { parameters.Add("limit", count.ToString()); } parameters.Add("api_key", _lastFm.ApiKey); var response = await(new CoreRequest(new Uri(LastFmConst.MethodBase + "artist.getSimilar"), parameters).Execute()); LastFmErrorProcessor.ProcessError(response); if (response.SelectToken("similarartists.artist") != null) { return((from a in response.SelectToken("similarartists.artist") select LastFmArtist.FromJson(a)).ToList()); } return(null); }
public static LastFmArtist FromJson(JToken json) { if (json == null) { throw new ArgumentException("Json can not be null."); } var result = new LastFmArtist(); result.Name = json["name"].Value <string>(); if (json["mbid"] != null) { result.Mbid = json["mbid"].Value <string>(); } var imageToken = json["image"]; if (imageToken != null) { foreach (var image in imageToken.Children()) { if (image["#text"] == null) { continue; } switch (image["size"].Value <string>()) { case "small": result.ImageSmall = image["#text"].Value <string>(); break; case "medium": result.ImageMedium = image["#text"].Value <string>(); break; case "large": result.ImageLarge = image["#text"].Value <string>(); break; case "extralarge": result.ImageExtraLarge = image["#text"].Value <string>(); break; case "mega": result.ImageMega = image["#text"].Value <string>(); break; } } } if (json.SelectToken("tags.tag") != null) { result.TopTags = new List <string>(); var tagToken = json.SelectToken("tags.tag"); if (tagToken is JArray) { foreach (var t in tagToken) { result.TopTags.Add(t["name"].Value <string>()); } } else { result.TopTags.Add(tagToken["name"].Value <string>()); } } if (json.SelectToken("similar.artist") != null) { result.SimilarArtists = new List <LastFmArtist>(); var similarToken = json.SelectToken("similar.artist"); if (similarToken is JArray) { foreach (var artistToken in similarToken) { result.SimilarArtists.Add(FromJson(artistToken)); } } else { result.SimilarArtists.Add(FromJson(similarToken)); } } if (json.SelectToken("bio.summary") != null) { result.Bio = json.SelectToken("bio.summary").Value <string>().Trim(); } return(result); }