public spotifyPlaylist GetPlaylist(string id, int offset = 0, int limit = 0)
        {
            string endPoint = "https://api.spotify.com/v1/users/ram_marwaha/playlists/" + id + "/tracks?offset=" + offset + ((limit == 0) ? "" : "&limit=" + limit);
            string response = MakeRequest(endPoint);

            byte[]       byteArray = Encoding.ASCII.GetBytes(response);
            MemoryStream stream    = new MemoryStream(byteArray);

            var             serializer = new DataContractJsonSerializer(typeof(spotifyPlaylist));
            spotifyPlaylist playlist   = (spotifyPlaylist)serializer.ReadObject(stream);

            while (playlist.next != null)
            {
                string       endPointNext  = playlist.next;
                string       responseNext  = MakeRequest(endPointNext);
                byte[]       byteArrayNext = Encoding.ASCII.GetBytes(responseNext);
                MemoryStream streamNext    = new MemoryStream(byteArrayNext);


                var             serializerNext = new DataContractJsonSerializer(typeof(spotifyPlaylist));
                spotifyPlaylist temp           = (spotifyPlaylist)serializer.ReadObject(streamNext);
                playlist.items.AddRange(temp.items);

                playlist.next = temp.next;
            }

            return(playlist);
        }
Пример #2
0
        public void AddPlaylistToTable(string playlist_name, string playlistId)
        {
            SpotifyAPIGetData p        = new SpotifyAPIGetData();
            spotifyPlaylist   playlist = p.GetPlaylist(playlistId);

            string     d   = "DELETE FROM dbo.Playlists WHERE Playlist = '" + playlist_name + "'";
            SqlCommand del = new SqlCommand(d, connection);

            try
            {
                del.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                MessageBox.Show("Something Went Wrong, Please Try Again Later ://");
            }

            foreach (songInfo song in playlist.items)
            {
                spotifyTrack t = song.track;

                string        title   = t.name.Replace("'", "''");
                List <string> artists = new List <string>();
                foreach (artistInfo a in t.artists)
                {
                    artists.Add(a.name);
                }
                string   artistString = String.Join(", ", artists).Replace("'", "''");
                string   album        = t.album.name.Replace("'", "''");
                int      @explicit    = (t.@explicit) ? 1 : 0;
                TimeSpan duration     = TimeSpan.FromMilliseconds(t.duration_ms);
                int      popularity   = t.popularity;
                string   trackId      = t.id.Replace("'", "''");
                string   release_date = t.album.release_date;

                string     c       = String.Format("EXEC dbo.insertPlaylistData @Playlist = '{0}', @Title = '{1}', @Artist = '{2}', @Album = '{3}', @Explicit = {4}, @Duration = '{5}', @Popularity = {6}, @Release_Date = '{7}', @ID = '{8}'", playlist_name, title, artistString, album, @explicit, duration, popularity, release_date, trackId);
                SqlCommand command = new SqlCommand(c, connection);
                try
                {
                    command.ExecuteNonQuery();
                }
                catch (Exception e)
                {
                    MessageBox.Show("Something Went Wrong, Please Try Again Later ://");
                }
            }
        }