예제 #1
0
        public static async Task <PagingTracks> GetTopTracks(string token, string type)
        {
            //A Method to recieve the Users Top Tracks
            //string timeRange = "";

            /*switch (type)
             * {
             *  case "short term":
             *      timeRange = "short-term";
             *      break;
             *  case "medium term":
             *      timeRange = "medium-term";
             *      break;
             *  case "long term":
             *      timeRange = "long-term";
             *      break;
             *  default:
             *      break;
             * }
             */
            //Declaring the strings needed for the Method
            string topTracksHeader = $"Bearer {/*Convert.ToBase64String(Encoding.UTF8.GetBytes(*/token/*))*/}";
            string apiUrl          = $"https://api.spotify.com/v1/me/top/tracks?time_range={type}&limit=50&offset=0";

            //Creating a HttpClient with a Header, that contains the access token
            HttpClient spotifyClient = new HttpClient();

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

            //Recieving the Response as a string from the specified Url and Deserializing it into a Paging Object
            string responseBody = await spotifyClient.GetStringAsync(apiUrl);

            PagingTracks getTopTracks = JsonConvert.DeserializeObject <PagingTracks>(responseBody);

            /*
             * //Creating a .txt file that contains the json Response
             * if (!File.Exists(pathTopTracks))
             * {
             *  using StreamWriter streamWriter = File.CreateText(pathTopTracks);
             *  streamWriter.WriteLine(responseBody);
             * }
             */
            //Dispoal of the HttpClient
            spotifyClient.Dispose();

            //Returning the Paging Obejct that contains the useres Top Tracks
            Console.WriteLine("Got Top Tracks");
            return(getTopTracks);
        }
예제 #2
0
        public static async Task MakePlaylist(string type, string token)
        {
            PagingTracks tracks = await GetTopTracks(token, type);

            string playlistId = await FindPlaylist(token, await GetUserPlaylists(token), type);

            List <string> myTopTracks = new List <string>();
            List <string> myPlaylists = new List <string>();

            foreach (Track item in tracks.Items)
            {
                myTopTracks.Add(item.Uri);
            }

            if (playlistExists == true)
            {
                await ReplaceTracks(playlistId, myTopTracks, token);
            }
            else
            {
                await AddTracks(playlistId, myTopTracks, token);
            }
        }