internal static LastArtist ParseJToken(JToken token) { var a = new LastArtist(); a.Id = token.Value <string>("id"); a.Name = token.Value <string>("name"); a.Mbid = token.Value <string>("mbid"); var url = token.Value <string>("url"); // for some stupid reason the api returns the url without http in the get similar method, WHY? if (!url.StartsWith("http")) { url = "http://" + url; } a.Url = new Uri(url, UriKind.Absolute); a.OnTour = Convert.ToBoolean(token.Value <int>("ontour")); var tagsToken = token.SelectToken("tags"); if (tagsToken != null) { var tagToken = tagsToken.SelectToken("tag"); if (tagToken != null) { a.Tags = tagToken.Type == JTokenType.Array ? tagToken.Children().Select(LastTag.ParseJToken) : new List <LastTag> { LastTag.ParseJToken(tagToken) }; } } var images = token.SelectToken("image"); if (images != null && images.HasValues) { var imageCollection = LastImageSet.ParseJToken(images); a.MainImage = imageCollection; } return(a); }
public async Task HandleResponseSingle() { var command = new GetTopArtistsCommand(MAuth.Object, USER, SPAN) { Page = 1, Count = 1 }; command.SetParameters(); var expectedArtist = new LastArtist { Name = "Anathema", PlayCount = 5216, Mbid = "20aa23e3-3532-42ca-acf6-e8c2e9df2688", Url = new Uri("http://www.last.fm/music/Anathema"), MainImage = new LastImageSet("http://userserve-ak.last.fm/serve/34/12571597.jpg", "http://userserve-ak.last.fm/serve/64/12571597.jpg", "http://userserve-ak.last.fm/serve/126/12571597.jpg", "http://userserve-ak.last.fm/serve/252/12571597.jpg", "http://userserve-ak.last.fm/serve/_/12571597/Anathema+Judgement+promo.jpg") }; var response = CreateResponseMessage(Encoding.UTF8.GetString(UserApiResponses.UserGetTopArtistsSingle)); var parsed = await command.HandleResponse(response); Assert.IsTrue(parsed.Success); Assert.AreEqual(1, parsed.Page); Assert.AreEqual(1, parsed.PageSize); Assert.AreEqual(1124, parsed.TotalItems); Assert.AreEqual(1124, parsed.TotalPages); Assert.AreEqual(1, parsed.Content.Count); var actualArtist = parsed.Content.First(); TestHelper.AssertSerialiseEqual(expectedArtist, actualArtist); }
public async Task HandleResponseSingle() { var expectedArtist = new LastArtist { Name = "Liars", Mbid = "03098741-08b3-4dd7-b3f6-1b0bfa2c879c", Url = new Uri("http://www.last.fm/music/Liars"), MainImage = new LastImageSet("http://userserve-ak.last.fm/serve/34/2261874.jpg", "http://userserve-ak.last.fm/serve/64/2261874.jpg", "http://userserve-ak.last.fm/serve/126/2261874.jpg", "http://userserve-ak.last.fm/serve/252/2261874.jpg", "http://userserve-ak.last.fm/serve/_/2261874/Liars.jpg") }; var response = CreateResponseMessage(Encoding.UTF8.GetString(UserApiResponses.UserGetRecommendedArtistsSingle)); var parsed = await _commmand.HandleResponse(response); Assert.IsTrue(parsed.Success); var expectedJson = expectedArtist.WrapEnumerable().TestSerialise(); var actualJson = parsed.Content.TestSerialise(); Assert.AreEqual(expectedJson, actualJson, expectedJson.DifferencesTo(actualJson)); }
/// <summary> /// Parses the given JToken into a track /// </summary> /// <param name="token">A valid JToken</param> /// <returns>track equivalent to the JToken</returns> /// <remarks>If this method is used directly then the duration attribute will be parsed as MILLIseconds</remarks> internal static LastTrack ParseJToken(JToken token) { var t = new LastTrack(); t.Id = token.Value <string>("id"); t.Name = token.Value <string>("name"); t.Mbid = token.Value <string>("mbid"); //some tracks do not contain the playcount prop, it will throw a FormatException var playCountStr = token.Value <string>("playcount"); int playCount; if (int.TryParse(playCountStr, out playCount)) { t.PlayCount = playCount; } var listenerCountStr = token.Value <string>("listeners"); int listenerCount; if (int.TryParse(listenerCountStr, out listenerCount)) { t.ListenerCount = listenerCount; } var userPlayCountStr = token.Value <string>("userplaycount"); int userPlayCount; if (int.TryParse(userPlayCountStr, out userPlayCount)) { t.UserPlayCount = userPlayCount; } t.Url = new Uri(token.Value <string>("url"), UriKind.Absolute); var artistToken = token.SelectToken("artist"); if (artistToken.Type != JTokenType.String) { t.ArtistName = LastArtist.GetNameFromJToken(artistToken); t.ArtistMbid = artistToken.Value <string>("mbid"); } else { t.ArtistName = artistToken.ToObject <string>(); } var albumToken = token.SelectToken("album"); if (albumToken != null) { t.AlbumName = LastAlbum.GetNameFromJToken(albumToken); } var tagsToken = token.SelectToken("toptags"); if (tagsToken != null) { var tagToken = tagsToken.SelectToken("tag"); if (tagToken != null) { t.TopTags = tagToken.Type == JTokenType.Array ? tagToken.Children().Select(token1 => LastTag.ParseJToken(token1)) : new List <LastTag> { LastTag.ParseJToken(tagToken) }; } } var date = token.SelectToken("date"); if (date != null) { var stamp = date.Value <double>("uts"); t.TimePlayed = stamp.FromUnixTime(); } var images = token.SelectToken("image"); if (images != null) { var imageCollection = LastImageSet.ParseJToken(images); t.Images = imageCollection; } var lovedToken = token.SelectToken("userloved"); if (lovedToken != null) { t.IsLoved = Convert.ToBoolean(lovedToken.Value <int>()); } var attrToken = token.SelectToken("@attr"); if (attrToken != null && attrToken.HasValues) { t.IsNowPlaying = attrToken.Value <bool>("nowplaying"); t.Rank = attrToken.Value <int?>("rank"); } // api returns milliseconds when track.getInfo is called directly var secsStr = token.Value <string>("duration"); double secs; if (double.TryParse(secsStr, out secs)) { if (Math.Abs(secs - default(double)) > double.Epsilon) { t.Duration = TimeSpan.FromMilliseconds(secs); } } return(t); }
public async Task HandleSuccessResponse() { var expectedArtist = new LastArtist() { Name = "Frightened Rabbit", Mbid = "dc21d171-7204-4759-9fd0-77d031aeb40c", Url = new Uri("http://www.last.fm/music/Frightened+Rabbit"), MainImage = new LastImageSet("http://userserve-ak.last.fm/serve/34/50340089.jpg", "http://userserve-ak.last.fm/serve/64/50340089.jpg", "http://userserve-ak.last.fm/serve/126/50340089.jpg", "http://userserve-ak.last.fm/serve/252/50340089.jpg", "http://userserve-ak.last.fm/serve/_/50340089/Frightened+Rabbit+frabbit.jpg"), // todo streamable OnTour = false, Similar = new List<LastArtist> { new LastArtist { Name = "Admiral Fallow", Url = new Uri("http://www.last.fm/music/Admiral+Fallow"), MainImage = new LastImageSet("http://userserve-ak.last.fm/serve/34/48454975.jpg", "http://userserve-ak.last.fm/serve/64/48454975.jpg", "http://userserve-ak.last.fm/serve/126/48454975.jpg", "http://userserve-ak.last.fm/serve/252/48454975.jpg", "http://userserve-ak.last.fm/serve/500/48454975/Admiral+Fallow+l_1185fb2755064ccfbab2871ecec8.jpg") }, new LastArtist { Name = "The Twilight Sad", Url = new Uri("http://www.last.fm/music/The+Twilight+Sad"), MainImage = new LastImageSet("http://userserve-ak.last.fm/serve/34/18201771.jpg", "http://userserve-ak.last.fm/serve/64/18201771.jpg", "http://userserve-ak.last.fm/serve/126/18201771.jpg", "http://userserve-ak.last.fm/serve/252/18201771.jpg", "http://userserve-ak.last.fm/serve/500/18201771/The+Twilight+Sad+hi+how+are+you.jpg"), }, new LastArtist { Name = "Owl John", Url = new Uri("http://www.last.fm/music/Owl+John"), MainImage = new LastImageSet("http://userserve-ak.last.fm/serve/34/101981791.png", "http://userserve-ak.last.fm/serve/64/101981791.png", "http://userserve-ak.last.fm/serve/126/101981791.png", "http://userserve-ak.last.fm/serve/252/101981791.png", "http://userserve-ak.last.fm/serve/500/101981791/Owl+John+owl.png"), }, new LastArtist { Name = "We Were Promised Jetpacks", Url = new Uri("http://www.last.fm/music/We+Were+Promised+Jetpacks"), MainImage = new LastImageSet("http://userserve-ak.last.fm/serve/34/53527397.jpg", "http://userserve-ak.last.fm/serve/64/53527397.jpg", "http://userserve-ak.last.fm/serve/126/53527397.jpg", "http://userserve-ak.last.fm/serve/252/53527397.jpg", "http://userserve-ak.last.fm/serve/_/53527397/We+Were+Promised+Jetpacks+wwpj.jpg"), }, new LastArtist { Name = "Meursault", Url = new Uri("http://www.last.fm/music/Meursault"), MainImage = new LastImageSet("http://userserve-ak.last.fm/serve/34/41921789.jpg", "http://userserve-ak.last.fm/serve/64/41921789.jpg", "http://userserve-ak.last.fm/serve/126/41921789.jpg", "http://userserve-ak.last.fm/serve/252/41921789.jpg", "http://userserve-ak.last.fm/serve/_/41921789/Meursault+lovely+fuckwits.jpg"), } }, Tags = new List<LastTag> { new LastTag("indie", "http://www.last.fm/tag/indie"), new LastTag("scottish", "http://www.last.fm/tag/scottish"), new LastTag("indie rock", "http://www.last.fm/tag/indie%20rock"), new LastTag("folk", "http://www.last.fm/tag/folk"), new LastTag("folk rock", "http://www.last.fm/tag/folk%20rock"), }, Bio = new LastWiki { Content = "Frightened Rabbit are an <a href=\"http://www.last.fm/tag/indie%20rock\" class=\"bbcode_tag\" rel=\"tag\">indie rock</a> band which formed in 2003 in Glasgow, Scotland. The band currently consists of Scott Hutchison (vocals, guitar), Billy Kennedy (guitar, keyboards), Grant Hutchison (drums, vocals), Andy Monaghan (guitar, keyboards) and Gordon Skene (guitar, keyboards). The band has released four albums: "Sing the Greys" (2006), "The Midnight Organ Fight" (2008), "The Winter of Mixed Drinks" (2010) and "Pedestrian Verse" (2013). \n\n <a href=\"http://www.last.fm/music/Frightened+Rabbit\">Read more about Frightened Rabbit on Last.fm</a>.\n \n \nUser-contributed text is available under the Creative Commons By-SA License and may also be available under the GNU FDL.", Summary = "Frightened Rabbit are an <a href=\"http://www.last.fm/tag/indie%20rock\" class=\"bbcode_tag\" rel=\"tag\">indie rock</a> band which formed in 2003 in Glasgow, Scotland. The band currently consists of Scott Hutchison (vocals, guitar), Billy Kennedy (guitar, keyboards), Grant Hutchison (drums, vocals), Andy Monaghan (guitar, keyboards) and Gordon Skene (guitar, keyboards). The band has released four albums: "Sing the Greys" (2006), "The Midnight Organ Fight" (2008), "The Winter of Mixed Drinks" (2010) and "Pedestrian Verse" (2013). \n\n <a href=\"http://www.last.fm/music/Frightened+Rabbit\">Read more about Frightened Rabbit on Last.fm</a>.", Published = new DateTimeOffset(2013, 2, 6, 0, 4, 40, TimeSpan.Zero), YearFormed = 2003 }, Stats = new LastStats { Listeners = 513447, Plays = 0 } }; var response = CreateResponseMessage(Encoding.UTF8.GetString(ArtistApiResponses.ArtistGetInfoSuccess)); var parsed = await _command.HandleResponse(response); Assert.IsTrue(parsed.Success); var expectedJson = expectedArtist.TestSerialise(); var actualJson = parsed.Content.TestSerialise(); Assert.AreEqual(expectedJson, actualJson, expectedJson.DifferencesTo(actualJson)); }
internal static LastArtist ParseJToken(JToken token) { var a = new LastArtist(); a.Id = token.Value <string>("id"); a.Name = token.Value <string>("name"); a.Mbid = token.Value <string>("mbid"); var url = token.Value <string>("url"); var playCountStr = token.Value <string>("playcount"); int playCount; if (int.TryParse(playCountStr, out playCount)) { a.PlayCount = playCount; } var matchStr = token.Value <string>("match"); double match; if (double.TryParse(matchStr, out match)) { a.MatchSimilar = match; } // for some stupid reason the api returns the url without http in the get similar method, WHY? if (!url.StartsWith("http")) { url = "http://" + url; } a.Url = new Uri(url, UriKind.Absolute); a.OnTour = Convert.ToBoolean(token.Value <int>("ontour")); var statsToken = token.SelectToken("stats"); if (statsToken != null) { a.Stats = LastStats.ParseJToken(statsToken); } var bioToken = token.SelectToken("bio"); if (bioToken != null) { a.Bio = LastWiki.ParseJToken(bioToken); } var tagsToken = token.SelectToken("tags"); if (tagsToken != null) { var tagToken = tagsToken.SelectToken("tag"); if (tagToken != null) { a.Tags = tagToken.Type == JTokenType.Array ? tagToken.Children().Select(token1 => LastTag.ParseJToken(token1)) : new List <LastTag> { LastTag.ParseJToken(tagToken) }; } } var images = token.SelectToken("image"); if (images != null && images.HasValues) { var imageCollection = LastImageSet.ParseJToken(images); a.MainImage = imageCollection; } var similarToken = token.SelectToken("similar"); if (similarToken != null) { a.Similar = new List <LastArtist>(); var similarArtists = similarToken.SelectToken("artist"); if (similarArtists != null && similarArtists.Children().Any()) { // array notation isn't used on the api when only one object is available if (similarArtists.Type != JTokenType.Array) { var item = ParseJToken(similarArtists); a.Similar.Add(item); } else { var items = similarArtists.Children().Select(ParseJToken); a.Similar.AddRange(items); } } } return(a); }
internal static LastArtist ParseJToken(JToken token) { var a = new LastArtist(); a.Id = token.Value<string>("id"); a.Name = token.Value<string>("name"); a.Mbid = token.Value<string>("mbid"); var url = token.Value<string>("url"); var playCountStr = token.Value<string>("playcount"); int playCount; if (int.TryParse(playCountStr, out playCount)) { a.PlayCount = playCount; } // for some stupid reason the api returns the url without http in the get similar method, WHY? if (!url.StartsWith("http")) url = "http://" + url; a.Url = new Uri(url, UriKind.Absolute); a.OnTour = Convert.ToBoolean(token.Value<int>("ontour")); var statsToken = token.SelectToken("stats"); if (statsToken != null) { a.Stats = LastStats.ParseJToken(statsToken); } var bioToken = token.SelectToken("bio"); if (bioToken != null) { a.Bio = LastWiki.ParseJToken(bioToken); } var tagsToken = token.SelectToken("tags"); if (tagsToken != null) { var tagToken = tagsToken.SelectToken("tag"); if (tagToken != null) { a.Tags = tagToken.Type == JTokenType.Array ? tagToken.Children().Select(token1 => LastTag.ParseJToken(token1)) : new List<LastTag> { LastTag.ParseJToken(tagToken) }; } } var images = token.SelectToken("image"); if (images != null && images.HasValues) { var imageCollection = LastImageSet.ParseJToken(images); a.MainImage = imageCollection; } var similarToken = token.SelectToken("similar"); if (similarToken != null) { a.Similar = new List<LastArtist>(); var similarArtists = similarToken.SelectToken("artist"); if (similarArtists != null && similarArtists.Children().Any()) { // array notation isn't used on the api when only one object is available if (similarArtists.Type != JTokenType.Array) { var item = ParseJToken(similarArtists); a.Similar.Add(item); } else { var items = similarArtists.Children().Select(ParseJToken); a.Similar.AddRange(items); } } } return a; }
/// <summary> /// Constructor. /// </summary> /// <param name="fetchedArtist">The fetched artist.</param> public FetchedArtistViewModel(LastArtist fetchedArtist) { FetchedArtist = fetchedArtist; }
private LastArtist CreateArtist(string artist, string artwork, bool withSimilar = true) { var a = new LastArtist { Name = artist, MainImage = new LastImageSet {ExtraLarge = new Uri(artwork)}, Bio = new LastWiki { YearFormed = 2011, Content = "In the begining it was called musicDownload... then it was reborn as something else..." } }; if (withSimilar) { a.Similar = new List<LastArtist> { CreateArtist("Iggy Azalea", "http://musicimage.xboxlive.com/content/music.1F154700-0200-11DB-89CA-0019B92A3933/image?locale=en-US", false), CreateArtist("Ariana Grande", "http://musicimage.xboxlive.com/content/music.89C84300-0200-11DB-89CA-0019B92A3933/image?locale=en-US", false), CreateArtist("Jason Derulo", "http://musicimage.xboxlive.com/content/music.9C080600-0200-11DB-89CA-0019B92A3933/image?locale=en-US", false) }; } return a; }