public static TracksBasedOnSeed GetPlaylistTracksByVariable(string AccessToken, int SongLimit, double Dancability, double Energy, double Speechiness, string Genres) { var client = new RestClient("https://api.spotify.com/v1"); var request = new RestRequest("recommendations?target_speechiness={speechiness}&target_energy={energy}&limit={limit}&seed_genres={genres}&target_danceability={dancability}&market=US", Method.GET); request.AddUrlSegment("speechiness", Speechiness.ToString()); request.AddUrlSegment("energy", Energy.ToString()); request.AddUrlSegment("dancability", Dancability.ToString()); request.AddUrlSegment("limit", SongLimit.ToString()); request.AddUrlSegment("genres", Genres); request.AddHeader("Authorization", string.Format("Bearer {0}", AccessToken)); // execute api call and deserialize the results into the object IRestResponse response = client.Execute(request); TracksBasedOnSeed tracks = new TracksBasedOnSeed(); tracks = JsonConvert.DeserializeObject <TracksBasedOnSeed>(response.Content); return(tracks); }
public JsonResult CraftPlaylist(string AccessToken, string NameString) { List <string> NameList = NameString.Trim().Trim(',').Split(',').ToList(); double TotalDancability = 0.0; int DancabilityCount = 0; double TotalEnergy = 0.0; int EnergyCount = 0; double TotalSpeechiness = 0.0; int SpeechinessCount = 0; SortedDictionary <string, int> Genres = new SortedDictionary <string, int>(); List <string> ArtistIds = new List <string>(); foreach (string name in NameList) { UserPlaylists playlists = APICalls.GetUsersPlaylists(AccessToken, name, 10); List <string> TrackIds = new List <string>(); Random rand = new Random(); playlists.Playlists = playlists.Playlists.OrderBy(x => rand.Next()).Take(3).ToList(); foreach (Item pl in playlists.Playlists) { PlaylistTracks tracks = APICalls.GetPlaylistTracks(AccessToken, name, pl.id); if (tracks.Songs != null) { foreach (PlaylistItem pli in tracks.Songs) { if (pli.track.id != null) { TrackIds.Add(pli.track.id); } if (pli.track.artists[0].id != null) { if (!ArtistIds.Contains(pli.track.artists[0].id)) { ArtistIds.Add(pli.track.artists[0].id); } } } } } for (int i = 0; i <= ArtistIds.Count / 20; i++) { ArtistList artists = APICalls.GetArtistList(AccessToken, ArtistIds.Take(20).ToList()); if (artists.artists == null || artists.artists.Count == 0) { break; } foreach (FullArtist fa in artists.artists) { foreach (object o in fa.genres) { if (Genres.ContainsKey(o.ToString())) { Genres[o.ToString()]++; } else { Genres.Add(o.ToString(), 1); } } } try { for (int z = 1; z <= 20; z++) { ArtistIds.RemoveAt(0); } } catch (Exception e) { } } MultipleSongFeatures features = APICalls.GetMultipleSongFeatures(AccessToken, TrackIds); foreach (AudioFeature af in features.features) { TotalDancability += af.danceability; DancabilityCount++; TotalEnergy += af.energy; EnergyCount++; TotalSpeechiness += af.speechiness; SpeechinessCount++; } } var sortedDict = Genres.OrderByDescending(x => x.Value).Take(5).ToDictionary(x => x.Key, x => x.Value); string GenreString = ""; foreach (KeyValuePair <string, int> o in sortedDict) { GenreString += o.Key + ","; } GenreString = GenreString.Trim(','); double AvgDancability = TotalDancability / DancabilityCount; double AvgEnergy = TotalEnergy / EnergyCount; double AvgSpeechiness = TotalSpeechiness / SpeechinessCount; TracksBasedOnSeed SeededTracks = APICalls.GetPlaylistTracksByVariable(AccessToken, 40, AvgDancability, AvgEnergy, AvgSpeechiness, GenreString); return(Json(new { Tracks = SeededTracks, AvgDancability = AvgDancability, AvgEnergy = AvgEnergy, AvgSpeechiness = AvgSpeechiness, Genres = GenreString.Replace(",", ", ") })); }
public JsonResult CraftPlaylistFromGenres(string AccessToken, string NameString, string[] GenreStringList) { List <string> NameList = NameString.Trim().Trim(',').Split(',').ToList(); double TotalDancability = 0.0; int DancabilityCount = 0; double TotalEnergy = 0.0; int EnergyCount = 0; double TotalSpeechiness = 0.0; int SpeechinessCount = 0; //SortedDictionary<string, int> Genres = new SortedDictionary<string, int>(); List <string> ArtistIds = new List <string>(); foreach (string name in NameList) { UserPlaylists playlists = APICalls.GetUsersPlaylists(AccessToken, name, 10); List <string> TrackIds = new List <string>(); Random rand = new Random(); playlists.Playlists = playlists.Playlists.OrderBy(x => rand.Next()).Take(3).ToList(); foreach (Item pl in playlists.Playlists) { PlaylistTracks tracks = APICalls.GetPlaylistTracks(AccessToken, name, pl.id); if (tracks.Songs != null) { foreach (PlaylistItem pli in tracks.Songs) { if (pli.track.id != null) { TrackIds.Add(pli.track.id); } if (pli.track.artists[0].id != null) { if (!ArtistIds.Contains(pli.track.artists[0].id)) { ArtistIds.Add(pli.track.artists[0].id); } } } } } MultipleSongFeatures features = APICalls.GetMultipleSongFeatures(AccessToken, TrackIds); foreach (AudioFeature af in features.features) { TotalDancability += af.danceability; DancabilityCount++; TotalEnergy += af.energy; EnergyCount++; TotalSpeechiness += af.speechiness; SpeechinessCount++; } } //var sortedDict = Genres.OrderByDescending(x => x.Value).Take(5).ToDictionary(x => x.Key, x => x.Value); string GenreString = ""; foreach (string o in GenreStringList) { GenreString += o + ","; } GenreString = GenreString.Trim(','); double AvgDancability = TotalDancability / DancabilityCount; double AvgEnergy = TotalEnergy / EnergyCount; double AvgSpeechiness = TotalSpeechiness / SpeechinessCount; TracksBasedOnSeed SeededTracks = APICalls.GetPlaylistTracksByVariable(AccessToken, 40, AvgDancability, AvgEnergy, AvgSpeechiness, GenreString); return(Json(new { Tracks = SeededTracks, AvgDancability = AvgDancability, AvgEnergy = AvgEnergy, AvgSpeechiness = AvgSpeechiness, Genres = GenreString.Replace(",", ", ") })); }