Пример #1
0
        public static async Task <PagingPlaylists> GetUserPlaylists(string token)
        {
            //A Method to recieve the Users Playlists

            //Creating the strings for the Header and Url for the Request
            string userPlaylistHeader = $"Bearer {token}";
            string apiUrl             = "https://api.spotify.com/v1/me/playlists?limit=50&offset=0";

            //Creating a HttpClient to handle the Get Request and adding the Request Header
            HttpClient spotifyClient = new HttpClient();

            spotifyClient.DefaultRequestHeaders.Add("Authorization", userPlaylistHeader);

            //Making a Get Request to the apiUrl and recieving a Json Object as a string
            string responseBody = await spotifyClient.GetStringAsync(apiUrl);

            //Converting the Json into a Paging Object, which contains the Users Playlists
            PagingPlaylists getUserPlaylists = JsonConvert.DeserializeObject <PagingPlaylists>(responseBody);


            /*//Useless, creates a .txt file in which the requested json will be displayed
             * if (!File.Exists(pathUserPlaylists))
             * {
             *  using StreamWriter streamWriter = File.CreateText(pathUserPlaylists);
             *  streamWriter.WriteLine(responseBody);
             * }
             */
            //Disposing of the HttpClient
            spotifyClient.Dispose();

            //returning the Paging Object, that contains the Playlists
            Console.WriteLine("Got User Playlists");
            return(getUserPlaylists);
        }
Пример #2
0
        public static async Task <string> FindPlaylist(string token, PagingPlaylists playlists, string type)
        {
            //A Method that checks, if the Playlist that this app creates already exists, and if so, which id it has
            string name        = "";
            string description = "";

            switch (type)
            {
            case "short_term":
                name        = "Short Term Top Tracks";
                description = "Your Short Term(4 weeks) Top Tracks";
                break;

            case "medium_term":
                name        = "Medium Term Top Tracks";
                description = "Your Medium Term(6 months) Top Tracks";
                break;

            case "long_term":
                name        = "Long Term Top Tracks";
                description = "Your Long Term(all time) Top Tracks";
                break;
            }
            //Creating a Playlist Object whose ID we want to get
            Playlist_simplified fgAnalytics = new Playlist_simplified();

            //Searching the Playlists return from the GetUserPlaylists() Method for the Name and Description that this App creates.
            fgAnalytics = playlists.Items.Find(x => x.Name.Contains(name) && x.Description == description);

            //If a Playlist exists, that matches our Specifications, return its ID, otherwise return, that it is non existent.
            if (fgAnalytics != null)
            {
                playlistExists = true;
                return(fgAnalytics.Id);
            }
            else
            {
                playlistExists = false;
                string id = await CreatePlaylist(token, await GetUserId(token), name, description);

                //string id = "x";
                return(id);
            }
        }