/// <summary> /// Creates an Artist from a JSON Object /// </summary> /// <param name="item">The item.</param> /// <param name="settings">The settings.</param> /// <returns> /// An Artist object /// </returns> internal static Artist FromJToken(JToken item, IMusicClientSettings settings) { // Extract thumbnails... Uri square50 = null; Uri square100 = null; Uri square200 = null; Uri square320 = null; MusicItem.ExtractThumbs(item["thumbnails"], out square50, out square100, out square200, out square320); // Derive 640 thumb Uri square640 = null; if (square320 != null) { square640 = new Uri(square320.ToString().Replace("320x320", "640x640")); } var count = item["count"]; var playCount = (count != null && count.Type != JTokenType.Null) ? count.Value <int>() : 0; return(new Artist() { Id = item.Value <string>("id"), Name = item.Value <string>("name"), Country = GetCountry(item), Genres = GetGenres(item, settings), MusicBrainzId = item.Value <string>("mbid"), Origin = GetOrigin(item), Thumb50Uri = square50, Thumb100Uri = square100, Thumb200Uri = square200, Thumb320Uri = square320, Thumb640Uri = square640, PlayCount = playCount }); }
/// <summary> /// Creates a Mix from a JSON Object /// </summary> /// <param name="item">The item.</param> /// <param name="settings">The settings.</param> /// <returns> /// A Mix object /// </returns> internal static Mix FromJToken(JToken item, IMusicClientSettings settings) { const string PlayMeThumbUri = "http://dev.mixrad.io/assets/playme/{0}x{0}.png"; if (item == null) { return(null); } bool parentalAdvisory = false; JToken parentaladvisoryToken = item["parentaladvisory"]; if (parentaladvisoryToken != null) { parentalAdvisory = item.Value <bool>("parentaladvisory"); } JToken featuredArtists = item["featuredartists"]; var featuredArtistsList = new List <Artist>(); if (featuredArtists != null) { foreach (JToken token in featuredArtists) { Artist artist = Artist.FromJToken(token, settings); featuredArtistsList.Add(artist); } } Uri square50 = null; Uri square100 = null; Uri square200 = null; Uri square320 = null; Uri square640 = null; var thumbnailsToken = item["thumbnails"]; MusicItem.ExtractThumbs(thumbnailsToken, out square50, out square100, out square200, out square320); if (thumbnailsToken != null) { square640 = Mix.ExtractThumb(thumbnailsToken, "640x640"); } var seeds = item["seeds"]; SeedCollection seedCollection = null; if (seeds != null) { seedCollection = SeedCollection.FromJson(item.ToString()); } else { var mixId = item.Value <string>("id"); if (!string.IsNullOrEmpty(mixId)) { seedCollection = new SeedCollection(Seed.FromMixId(mixId)); } } var name = item.Value <string>("name"); var description = item.Value <string>("description"); if (seedCollection != null && seedCollection.Count > 0) { if (seedCollection.Count(s => s.Type == SeedType.UserId) > 0) { if (square50 == null) { square50 = new Uri(string.Format(PlayMeThumbUri, 50)); } if (square100 == null) { square100 = new Uri(string.Format(PlayMeThumbUri, 100)); } if (square200 == null) { square200 = new Uri(string.Format(PlayMeThumbUri, 200)); } if (square320 == null) { square320 = new Uri(string.Format(PlayMeThumbUri, 320)); } if (square640 == null) { square640 = new Uri(string.Format(PlayMeThumbUri, 640)); } if (string.IsNullOrEmpty(name)) { name = "Play Me"; } } else if (seedCollection.Count(s => s.Type == SeedType.ArtistId) > 0) { var artistSeeds = seedCollection.Where(s => (s.Type == SeedType.ArtistId)).ToArray(); if (string.IsNullOrEmpty(name)) { // Derive a name var names = artistSeeds.Select(s => s.Name).Where(s => !string.IsNullOrEmpty(s)).ToArray(); name = names.Length > 0 ? string.Join(", ", names) : "Artist Mix"; } // Derive a thumbnail image var idSeed = artistSeeds.FirstOrDefault(s => !string.IsNullOrEmpty(s.Id)); if (idSeed != null && settings != null) { var builder = new ArtistImageUriWriter(settings); if (square50 == null) { square50 = builder.BuildForId(idSeed.Id, 50); } if (square100 == null) { square100 = builder.BuildForId(idSeed.Id, 100); } if (square200 == null) { square200 = builder.BuildForId(idSeed.Id, 200); } if (square320 == null) { square320 = builder.BuildForId(idSeed.Id, 320); } if (square640 == null) { square640 = builder.BuildForId(idSeed.Id, 640); } } } } return(new Mix() { Name = name, TrackCount = item.Value <int>("numbertracks"), ParentalAdvisory = parentalAdvisory, Seeds = seedCollection, Thumb50Uri = square50, Thumb100Uri = square100, Thumb200Uri = square200, Thumb320Uri = square320, Thumb640Uri = square640, Description = description, FeaturedArtists = featuredArtistsList }); }
/// <summary> /// Creates a Product from a JSON Object /// </summary> /// <param name="item">The item.</param> /// <param name="settings">The settings.</param> /// <returns> /// A Product object /// </returns> internal static Product FromJToken(JToken item, IMusicClientSettings settings) { if (item == null) { return(null); } // Extract category... Category category = Category.Unknown; JToken jsonCategory = item["category"]; if (jsonCategory != null) { category = ParseHelper.ParseEnumOrDefault <Category>(jsonCategory.Value <string>("id")); } // Extract genres... Genre[] genres = null; JArray jsonGenres = item.Value <JArray>("genres"); if (jsonGenres != null) { List <Genre> list = new List <Genre>(); foreach (JToken jsonGenre in jsonGenres) { list.Add((Genre)Genre.FromJToken(jsonGenre, settings)); } genres = list.ToArray(); } // Extract takenfrom... Product takenFrom = null; JToken jsonTakenFrom = item["takenfrom"]; if (jsonTakenFrom != null) { takenFrom = (Product)FromJToken(jsonTakenFrom, settings); } // Extract price... Price price = null; JToken jsonPrices = item["prices"]; if (jsonPrices != null) { JToken jsonPermDownload = jsonPrices["permanentdownload"]; if (jsonPermDownload != null) { price = Price.FromJToken(jsonPermDownload); } } // Extract Artists... Artist[] performers = null; if (item["creators"] != null) { JArray jsonArtists = item["creators"].Value <JArray>("performers") ?? item["creators"].Value <JArray>("composers"); if (jsonArtists != null) { List <Artist> list = new List <Artist>(); foreach (JToken jsonArtist in jsonArtists) { list.Add((Artist)Artist.FromJToken(jsonArtist, settings)); } performers = list.ToArray(); } } // Extract trackcount... int? trackCount = null; JToken jsonTrackCount = item["trackcount"]; if (jsonTrackCount != null) { trackCount = item.Value <int>("trackcount"); } // Extract thumbnails... Uri square50 = null; Uri square100 = null; Uri square200 = null; Uri square320 = null; MusicItem.ExtractThumbs(item["thumbnails"], out square50, out square100, out square200, out square320); // Extract Bollywood information var actorNames = new List <string>(); if (item["actornames"] != null) { ParseArray(item, actorNames, "actornames"); } var lyricistNames = new List <string>(); if (item["lyricistnames"] != null) { ParseArray(item, lyricistNames, "lyricistnames"); } var singerNames = new List <string>(); if (item["singernames"] != null) { ParseArray(item, singerNames, "singernames"); } var movieDirectorNames = new List <string>(); if (item["moviedirectornames"] != null) { ParseArray(item, movieDirectorNames, "moviedirectornames"); } var movieProducerNames = new List <string>(); if (item["movieproducernames"] != null) { ParseArray(item, movieProducerNames, "movieproducernames"); } var musicDirectorNames = new List <string>(); if (item["musicdirectornames"] != null) { ParseArray(item, musicDirectorNames, "musicdirectornames"); } var parentalAdvisory = item.Value <bool>("parentaladvisory"); // Extract bpm... int bpm = 0; if (item["bpm"] != null) { bpm = item.Value <int>("bpm"); } // Derive 640 thumb Uri square640 = null; if (square320 != null) { square640 = new Uri(square320.ToString().Replace("w=320", "w=640")); } // Create the resulting Product object... var product = new Product() { Id = item.Value <string>("id"), Name = item.Value <string>("name"), Thumb50Uri = square50, Thumb100Uri = square100, Thumb200Uri = square200, Thumb320Uri = square320, Thumb640Uri = square640, Category = category, Genres = genres, TakenFrom = takenFrom, Price = price, TrackCount = trackCount, Tracks = ExtractTracks(item["tracks"], settings), Performers = performers, Duration = item.Value <int?>("duration"), VariousArtists = item.Value <bool>("variousartists"), StreetReleaseDate = item.Value <DateTime>("streetreleasedate"), SellerStatement = item.Value <string>("sellerstatement"), Label = item.Value <string>("label"), ActorNames = actorNames, LyricistsNames = lyricistNames, SingerNames = singerNames, MovieDirectorNames = movieDirectorNames, MovieProducerNames = movieProducerNames, MusicDirectorNames = musicDirectorNames, ParentalAdvisory = parentalAdvisory, Bpm = bpm }; var sequence = item.Value <int>("sequence"); if (sequence >= 1) { product.Sequence = sequence; } return(product); }