Ejemplo n.º 1
0
        public ListOfTracks GetMyTopTracks()
        {
            var     webClient = new WebClient();
            JObject jObject   = JObject.Parse(storage.AuthorizationCodeFlowAuthTokenResponse);
            string  AuthToken = (string)jObject.SelectToken("access_token");

            webClient.Headers.Add(HttpRequestHeader.Accept, "application/json");
            webClient.Headers.Add(HttpRequestHeader.ContentType, "application/json");
            webClient.Headers.Add(HttpRequestHeader.Authorization, "Bearer " + AuthToken);

            var GetResponse = SendAuthorizationCodeRequest(webClient, "https://api.spotify.com/v1/me/top/tracks");


            var          jsonTracks = JObject.Parse(GetResponse);
            JArray       tracks     = (JArray)jsonTracks["items"];
            ListOfTracks listTracks = new ListOfTracks();

            for (int i = 0; i < tracks.Count; i++)
            {
                var trackId          = tracks[i]["id"].ToString();
                var thesongName      = tracks[i]["name"].ToString();
                var artistName       = tracks[i]["artists"][0]["name"].ToString();
                var image_small_url  = tracks[i]["album"]["images"][2]["url"].ToString();
                var image_medium_url = tracks[i]["album"]["images"][1]["url"].ToString();
                var image_large_url  = tracks[i]["album"]["images"][0]["url"].ToString();
                var webplayerLink    = tracks[i]["external_urls"]["spotify"].ToString();
                listTracks.Tracks.Add(new Track(trackId, thesongName, artistName, image_small_url, image_medium_url, image_large_url, webplayerLink));
            }

            return(listTracks);

            //return JObject.Parse(GetResponse);
        }
Ejemplo n.º 2
0
        public void PlayPlaylist()
        {
            Console.WriteLine("Starting playlist.");
            int    maxRandomNumber = SizeOfPlaylist; //maxRandomNumber cannot be larget than sizeOfPlaylist
            Random random          = new Random();

            while (ListOfTracks.Count > 0)
            {
                int randomNumber = random.Next(maxRandomNumber); //generating random number that is smaller than sizeOfPlaylist
                ListOfTracks[randomNumber].PlayTrack();
                ListOfTracks.RemoveAt(randomNumber);             //removing track after playing it
                maxRandomNumber--;                               //decreasing maxRandomNumber so it isn't larger than current sizeOfPlaylist
            }
            Console.WriteLine("End of playlist.");
        }
Ejemplo n.º 3
0
        public ListOfTracks GetSearch(String songName)
        {
            if (storage.ClientCredentialsFlowAuthToken == null)
            {
                auth.GetClientCredentialsFlowAuthToken();
            }

            var     webClient = new WebClient();
            JObject jObject   = JObject.Parse(storage.ClientCredentialsFlowAuthToken);
            string  AuthToken = (string)jObject.SelectToken("access_token");

            //webClient.Headers.Add(HttpRequestHeader.Accept, "application/json");
            webClient.Headers.Add(HttpRequestHeader.Authorization, "Bearer " + AuthToken);
            var limit = "20";    //Number of songs that Spotify returns



            var GetResponse = SendClientCredentialsRequest(webClient, "https://api.spotify.com/v1/search?q=track:" + Uri.EscapeUriString(songName) + "*&type=track&market=DK&limit=" + limit + "&offset=0");

            var          jsonTracks = JObject.Parse(GetResponse);
            JArray       tracks     = (JArray)jsonTracks["tracks"]["items"];
            ListOfTracks listTracks = new ListOfTracks();

            for (int i = 0; i < tracks.Count; i++)
            {
                var trackId          = tracks[i]["id"].ToString();
                var thesongName      = tracks[i]["name"].ToString();
                var artistName       = tracks[i]["artists"][0]["name"].ToString();
                var image_small_url  = tracks[i]["album"]["images"][2]["url"].ToString();
                var image_medium_url = tracks[i]["album"]["images"][1]["url"].ToString();
                var image_large_url  = tracks[i]["album"]["images"][0]["url"].ToString();
                var webplayerLink    = tracks[i]["external_urls"]["spotify"].ToString();
                listTracks.Tracks.Add(new Track(trackId, thesongName, artistName, image_small_url, image_medium_url, image_large_url, webplayerLink));
            }

            return(listTracks);
            //return GetResponse;
        }