public async Task<List<LastFmTrack>> Search(string track, string artist) { var parameters = new Dictionary<string, string>(); parameters.Add("track", track); if (!string.IsNullOrEmpty(artist)) parameters.Add("artist", artist); parameters.Add("api_key", _lastFm.ApiKey); var response = await new CoreRequest(new Uri(LastFmConst.MethodBase + "track.search"), parameters).Execute(); LastFmErrorProcessor.ProcessError(response); if (response.SelectToken("results.trackmatches.track") != null) { var trackJson = response.SelectToken("results.trackmatches.track"); if (trackJson is JArray) return (from t in response.SelectToken("results.trackmatches.track") select LastFmTrack.FromJson(t)).ToList(); else return new List<LastFmTrack>() { LastFmTrack.FromJson(trackJson) }; } return null; }
public async Task<LastFmTrack> GetInfo(string title, string artist, bool autoCorrect = true, string mbid = null) { var parameters = new Dictionary<string, string>(); if (!string.IsNullOrEmpty(mbid)) parameters.Add("mbid", mbid); else { parameters.Add("artist", artist); parameters.Add("track", title); } if (autoCorrect) parameters.Add("autocorrect", "1"); parameters.Add("api_key", _lastFm.ApiKey); var response = await new CoreRequest(new Uri(LastFmConst.MethodBase + "track.getInfo"), parameters).Execute(); LastFmErrorProcessor.ProcessError(response); if (response["track"] != null) { return LastFmTrack.FromJson(response["track"]); } return null; }
internal static LastFmTrack FromJson(JToken json) { if (json == null) throw new ArgumentException("Json can not be null."); var result = new LastFmTrack(); if (json["name"] != null) result.Title = json["name"].Value<string>(); if (json["artist"] != null) { if (json.SelectToken("artist.name") != null) result.Artist = json["artist"]["name"].Value<string>(); else result.Artist = json["artist"].Value<string>(); } if (json["duration"] != null && !string.IsNullOrEmpty(json["duration"].Value<string>())) { result.Duration = json["duration"].Value<int>(); } if (json["album"] != null) { result.Album = json["album"]["title"].Value<string>(); var imageToken = json["album"]["image"]; if (imageToken != null) { foreach (var image in imageToken.Children()) { 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; } } } } var imgToken = json["image"]; if (imgToken != null) { foreach (var image in imgToken.Children()) { 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; } } } return result; }
internal static LastFmTrack FromJson(JToken json) { if (json == null) { throw new ArgumentException("Json can not be null."); } var result = new LastFmTrack(); if (json["name"] != null) { result.Title = json["name"].Value <string>(); } if (json["artist"] != null) { if (json.SelectToken("artist.name") != null) { result.Artist = json["artist"]["name"].Value <string>(); } else { result.Artist = json["artist"].Value <string>(); } } if (json["duration"] != null && !string.IsNullOrEmpty(json["duration"].Value <string>())) { result.Duration = json["duration"].Value <int>(); } if (json["album"] != null) { result.Album = json["album"]["title"].Value <string>(); var imageToken = json["album"]["image"]; if (imageToken != null) { foreach (var image in imageToken.Children()) { 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; } } } } var imgToken = json["image"]; if (imgToken != null) { foreach (var image in imgToken.Children()) { 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; } } } return(result); }