public async Task <ActionResult> GetRecommandationWithAudioFeatures(string ArtistIds, string Genre, string TracksIds, string Market, string Dancablity, string Acousticness, string Energy,
                                                                            string Instrumentalness)
        {
            var Token         = GetAccessTokenCookie(); //The SpotifyAuthenticationComponent middleware, always checks for having cookie in the app pipeline!
            var Client        = new SpotifyCommunication.SpotifyApiLibrary(Token);
            var AudioFeatures = new SpotifyAudioFeatures()
            {
                MaxAcousticness     = Utilities.GetDoubleRange(Acousticness).Max,
                MinAcousticness     = Utilities.GetDoubleRange(Acousticness).Min,
                MaxDancablity       = Utilities.GetDoubleRange(Dancablity).Max,
                MinDancablity       = Utilities.GetDoubleRange(Dancablity).Min,
                MaxEnergy           = Utilities.GetDoubleRange(Energy).Max,
                MinEnergy           = Utilities.GetDoubleRange(Energy).Min,
                MaxInstrumentalness = Utilities.GetDoubleRange(Instrumentalness).Max,
                MinInstrumentalness = Utilities.GetDoubleRange(Instrumentalness).Min,
            };
            var Recoms = await Client.GetRecommendationsAsync(ArtistIds, Genre, TracksIds, Market, 25, AudioFeatures);

            var ViewModel = Recoms.tracks.Select(x => new RecommendationViewModel()
            {
                Id       = x.id,
                EmbedUrl = Utilities.GetSpotifyEmbedTrackUrl(x.id)
            }).ToList();

            return(Content(Serializer.Serialize(ViewModel)));
        }
Exemple #2
0
 public static IEnumerable <AudioFeature> FilterAudioFeatures(List <AudioFeature> Tracks, SpotifyAudioFeatures AudioFeatures)
 {
     return(Tracks.Where(x =>
                         (x.acousticness >= AudioFeatures.MinAcousticness && x.acousticness <= AudioFeatures.MaxAcousticness) &&
                         (x.danceability >= AudioFeatures.MinDancablity && x.danceability <= AudioFeatures.MaxDancablity) &&
                         (x.energy >= AudioFeatures.MinEnergy && x.energy <= AudioFeatures.MaxEnergy) &&
                         (x.instrumentalness >= AudioFeatures.MinInstrumentalness && x.instrumentalness <= AudioFeatures.MaxInstrumentalness)));
 }
        public async Task <SpotifyRecommendationObject> GetRecommendationsAsync(string ArtistsIds, string Genres, string TracksIds, string Market, int Limit, SpotifyAudioFeatures AudioFeature)
        {
            var content = $"?limit={Limit}&market={Market}&seed_artists={ArtistsIds}&seed_genres={Genres}&seed_tracks={TracksIds}&min_acousticness={AudioFeature.MinAcousticness}&max_acousticness={AudioFeature.MaxAcousticness}&min_danceability={AudioFeature.MinDancablity}&max_danceability={AudioFeature.MaxDancablity}&min_energy={AudioFeature.MinEnergy}&max_energy={AudioFeature.MaxEnergy}&min_instrumentalness={AudioFeature.MinInstrumentalness}&max_instrumentalness={AudioFeature.MaxInstrumentalness}";
            var Recom   = await SendGetRequestAsync <SpotifyRecommendationObject>(true, "recommendations", _AccessToken, content);

            return(Recom);
        }
Exemple #4
0
        public static async Task <List <RecommendationViewModel> > GetRecommendationsByTracksAsync(List <Track> Tracks, SpotifyApiLibrary Client, string Market, SpotifyAudioFeatures AudioFeatures)
        {
            var FinalRecommendation = new List <RecommendationViewModel>();

            foreach (var RecomTrack in Tracks)
            {
                try
                {
                    var RecomArtist    = RecomTrack.artists.First();
                    var relatedArtists = await Client.GetArtistsRelatedAsync(RecomArtist.id);

                    relatedArtists.artists.Shuffle();
                    var ItemRelatedArtist = relatedArtists.artists.First();
                    var TopSong           = await Client.GetArtistTopSongsAsync(ItemRelatedArtist.id, Market);

                    TopSong.tracks.Shuffle();
                    var Top20        = TopSong.tracks.Take(20);
                    var FinalSongIds = GetTrackIds(Top20.Take(100));
                    var SpotifySeveralTracksAudioFeatures = await Client.GetAudioFeaturesForSeveralTracks(FinalSongIds);

                    var filtered = FilterAudioFeatures(SpotifySeveralTracksAudioFeatures.audio_features, AudioFeatures);
                    FinalSongIds = GetTrackIds(filtered);
                    var FinalRelatedArtistTracksFilteredResult = await Client.GetSeveralTracks(FinalSongIds);

                    var TheMostPopularTrack = FinalRelatedArtistTracksFilteredResult.tracks.OrderBy(x => x.popularity).FirstOrDefault();
                    if (TheMostPopularTrack != null)
                    {
                        FinalRecommendation.Add(new RecommendationViewModel {
                            Id = TheMostPopularTrack.id, EmbedUrl = GetSpotifyEmbedTrackUrl(TheMostPopularTrack.id)
                        });
                    }
                }
                catch (Exception e)
                {
                    break;
                }
            }
            return(FinalRecommendation);
        }