Example #1
0
        public IList <Track> GetPlaylistTracks(string href)
        {
            var request = Spotify.GetPlaylistTracks(href);

            if (request.IsSuccess)
            {
                var playlistTracksResponse = request.Content;

                var spotifyTracks = playlistTracksResponse.Items;
                while (playlistTracksResponse.Next != null)
                {
                    request = Spotify.GetNextPageContent <SpotifyPagingObject <SpotifyPlaylistTrack> >(playlistTracksResponse.Next);
                    if (request.IsSuccess)
                    {
                        playlistTracksResponse = request.Content;
                        foreach (var track in playlistTracksResponse.Items)
                        {
                            spotifyTracks.Add(track);
                        }
                    }
                }
                IList <Track> returnList = new List <Track>();
                foreach (var spotifyTrack in spotifyTracks)
                {
                    returnList.Add(SpotifyFactory.CreateTrack(spotifyTrack.Track));
                }
                return(returnList);
            }
            return(null);
        }
Example #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);
        }
Example #3
0
        public IList <Track> GetArtistTopTracks(string artistId)
        {
            var request = Spotify.GetArtistTopTracks(artistId);

            if (request.IsSuccess)
            {
                var artistTopTracks = request.Content;

                IList <Track> returnList = new List <Track>();
                foreach (var spotifyTrack in artistTopTracks.Tracks)
                {
                    returnList.Add(SpotifyFactory.CreateTrack(spotifyTrack));
                }
                return(returnList);
            }
            return(null);
        }