Beispiel #1
0
        public async Task <List <SpotifyTrack> > GetRecommendationByTrackFeatureAsync(List <string> tracks)
        {
            try
            {
                var trackFeatureList = new List <SpotifyTrackFeaturesResponse>();
                foreach (var track in tracks)
                {
                    var temp = await GetTrackFeaturesAsync(track);

                    trackFeatureList.Add(temp);
                }

                var features = new TrackFeatures
                {
                    Danceability     = trackFeatureList.Average(t => t.Danceability),
                    Acousticness     = trackFeatureList.Average(t => t.Acousticness),
                    Energy           = trackFeatureList.Average(t => t.Energy),
                    Instrumentalness = trackFeatureList.Average(t => t.Instrumentalness),
                    Tracks           = tracks
                };

                var recommendations = await GetRecommendationsByFeaturesAsync(features);

                var topTracks = (from t in recommendations.Tracks
                                 orderby t.Popularity descending
                                 select t).Take(20);

                return(topTracks.ToList());
            }

            catch (HttpRequestException)
            {
                return(null);
            }
        }
Beispiel #2
0
        public IList <Track> GetRecommendations(List <Artist> artists, List <Track> tracks)
        {
            //Fix for Double.ToString() using ,
            System.Globalization.CultureInfo customCulture = (System.Globalization.CultureInfo)System.Threading.Thread.CurrentThread.CurrentCulture.Clone();
            customCulture.NumberFormat.NumberDecimalSeparator = ".";

            System.Threading.Thread.CurrentThread.CurrentCulture = customCulture;


            Dictionary <string, string> seedDictionary = new Dictionary <string, string>();
            TrackFeatures recommendationFeatures       = new TrackFeatures();

            if (tracks.Count > 0)
            {
                recommendationFeatures = AnalyzeTracks(tracks);
                seedDictionary.Add("seed_tracks", string.Join(",", tracks.ConvertAll(t => t.Id)));
            }
            else if (artists.Count > 0)
            {
                var artistsTopTracks = new List <SpotifyTrack>();
                foreach (var artist in artists)
                {
                    var response = Spotify.GetArtistTopTracks(artist.Id);
                    if (response.IsSuccess)
                    {
                        artistsTopTracks.AddRange(response.Content.Tracks);
                    }
                }
                recommendationFeatures = AnalyzeTracks(artistsTopTracks.ConvertAll(SpotifyFactory.CreateTrack));
                seedDictionary.Add("seed_artists", string.Join(",", artists.ConvertAll(t => t.Id)));
            }
            foreach (var feature in recommendationFeatures.GetType().GetProperties())
            {
                seedDictionary.Add($"target_{feature.Name.ToLower()}", feature.GetValue(recommendationFeatures).ToString());
            }
            var recommendationResponse = Spotify.GetSeedRecommendations(seedDictionary);

            if (recommendationResponse.IsSuccess)
            {
                var responseContent = recommendationResponse.Content;
                var returnObject    = new List <Track>();
                foreach (var spotifyTrack in responseContent.Tracks)
                {
                    returnObject.Add(SpotifyFactory.CreateTrack(spotifyTrack));
                }
                return(returnObject);
            }
            return(null);
        }
        public async Task LoadTabForm()
        {
            Track         track         = Globals.DataStore.SelectedTrack;
            TrackFeatures trackFeatures = Globals.DataStore.SelectedTrackFeatures;

            lblTrackName.Text = track.name;

            lblEnergy.Text           = (trackFeatures.energy * 100).ToString() + "%";
            lblLiveness.Text         = (trackFeatures.liveness * 100).ToString() + "%";
            lblSpeechiness.Text      = (trackFeatures.speechiness * 100).ToString() + "%";
            lblDanceability.Text     = (trackFeatures.danceability * 100).ToString() + "%";
            lblInstrumentalness.Text = (trackFeatures.instrumentalness * 100).ToString() + "%";
            lblValence.Text          = (trackFeatures.valence * 100).ToString() + "%";
            lblAcousticness.Text     = (trackFeatures.acousticness * 100).ToString() + "%";

            lblTempo.Text         = (trackFeatures.tempo).ToString();
            lblTimeSignature.Text = (trackFeatures.time_signature).ToString();
            lblLoudness.Text      = (trackFeatures.loudness).ToString();
        }
Beispiel #4
0
        public async Task <SpotifyRecomendationsresponse> GetRecommendationsByFeaturesAsync(TrackFeatures features)
        {
            var url = new Url("/v1/recommendations");

            url = url.SetQueryParam("target_danceability", features.Danceability);
            url = url.SetQueryParam("target_acousticness", features.Acousticness);
            url = url.SetQueryParam("target_energy", features.Energy);
            url = url.SetQueryParam("target_instrumentalness", features.Instrumentalness);
            url = url.SetQueryParam("min_popularity", 50);

            foreach (var track in features.Tracks)
            {
                url = url.SetQueryParam("seed_tracks", track);
            }

            return(await GetResponseAsync <SpotifyRecomendationsresponse>(url));
        }
 private static RecommendationsParameters TranslateTrackFeaturesToRecommendationsParameters(TrackFeatures features, IEnumerable <string> genres, int margin)
 {
     return(new RecommendationsParameters(genres.Select(GenresHelper.GetGenreByApiName))
     {
         MinAcousticness = GetValueFixedByMarginAsString(features.Acousticness, margin, MinMaxProperty.Min),
         MaxAcousticness = GetValueFixedByMarginAsString(features.Acousticness, margin, MinMaxProperty.Max),
         MinDanceability = GetValueFixedByMarginAsString(features.Danceability, margin, MinMaxProperty.Min),
         MaxDanceability = GetValueFixedByMarginAsString(features.Danceability, margin, MinMaxProperty.Max),
         MinEnergy = GetValueFixedByMarginAsString(features.Energy, margin, MinMaxProperty.Min),
         MaxEnergy = GetValueFixedByMarginAsString(features.Energy, margin, MinMaxProperty.Max),
         MinInstrumentalness = GetValueFixedByMarginAsString(features.Instrumentalness, margin, MinMaxProperty.Min),
         MaxInstrumentalness = GetValueFixedByMarginAsString(features.Instrumentalness, margin, MinMaxProperty.Max),
         MinLivenes = GetValueFixedByMarginAsString(features.Liveness, margin, MinMaxProperty.Min),
         MaxLiveness = GetValueFixedByMarginAsString(features.Liveness, margin, MinMaxProperty.Max),
         MinLoudness = GetValueFixedByMarginAsString(features.Loudness, margin, MinMaxProperty.Min),
         MaxLoudness = GetValueFixedByMarginAsString(features.Loudness, margin, MinMaxProperty.Max),
         MinSpeechiness = GetValueFixedByMarginAsString(features.Speechiness, margin, MinMaxProperty.Min),
         MaxSpeechiness = GetValueFixedByMarginAsString(features.Speechiness, margin, MinMaxProperty.Max),
         MinTempo = GetValueFixedByMarginAsString(features.Tempo, margin, MinMaxProperty.Min),
         MaxTempo = GetValueFixedByMarginAsString(features.Tempo, margin, MinMaxProperty.Max),
         MinValence = GetValueFixedByMarginAsString(features.Valence, margin, MinMaxProperty.Min),
         MaxValance = GetValueFixedByMarginAsString(features.Valence, margin, MinMaxProperty.Max)
     });
 }