Ejemplo n.º 1
0
        private SpotifyPlaylistModel GetPlaylist(WebClient webClient, string id)
        {
            string url        = string.Format("https://api.spotify.com/v1/playlists/{0}", id);
            var    jsonString = webClient.DownloadString(url);

            return(SpotifyPlaylistModel.FromJson(jsonString));
        }
Ejemplo n.º 2
0
        public async Task RemoveSongsFromPlaylist(SpotifyPlaylistModel playlist, IEnumerable <SpotifySongModel> songs)
        {
            try
            {
                if (playlist != null)
                {
                    for (int i = 0; i < songs.Count(); i += 50)
                    {
                        JArray songsToDeleteArray = new JArray();
                        foreach (SpotifySongModel songToDelete in songs.Skip(i).Take(50))
                        {
                            JObject songPayload = new JObject();
                            songPayload["uri"] = "spotify:track:" + songToDelete.ID;
                            songsToDeleteArray.Add(songPayload);
                        }

                        JObject payload = new JObject();
                        payload["tracks"] = songsToDeleteArray;

                        using (HttpClientWrapper client = await this.GetHttpClient())
                        {
                            HttpMethod         method  = new HttpMethod("DELETE");
                            HttpRequestMessage request = new HttpRequestMessage(method, string.Format("playlists/{0}/tracks", playlist.ID))
                            {
                                Content = this.CreateContentFromObject(payload)
                            };
                            await client.SendAsync(request);
                        }
                    }
                }
            }
            catch (Exception ex) { Logger.Log(ex); }
        }
Ejemplo n.º 3
0
 public async Task <SpotifyPlaylistModel> GetPlaylist(SpotifyPlaylistModel playlist)
 {
     try
     {
         if (playlist != null)
         {
             return(new SpotifyPlaylistModel(await this.GetJObjectAsync(string.Format("playlists/{0}", playlist.ID))));
         }
     }
     catch (Exception ex) { Logger.Log(ex); }
     return(null);
 }
Ejemplo n.º 4
0
        public async Task <bool> AddSongToPlaylist(SpotifyPlaylistModel playlist, SpotifySongModel song)
        {
            try
            {
                if (playlist != null && song != null)
                {
                    HttpResponseMessage response = await this.PostAsync(string.Format("playlists/{0}/tracks?uris=spotify:track:" + song.ID, playlist.ID), null);

                    return(response.StatusCode == HttpStatusCode.Created);
                }
            }
            catch (Exception ex) { Logger.Log(ex); }
            return(false);
        }
Ejemplo n.º 5
0
        public async Task <IEnumerable <SpotifySongModel> > GetPlaylistSongs(SpotifyPlaylistModel playlist)
        {
            List <SpotifySongModel> results = new List <SpotifySongModel>();

            try
            {
                if (playlist != null)
                {
                    foreach (JObject song in await this.GetPagedResult(string.Format("playlists/{0}/tracks", playlist.ID)))
                    {
                        results.Add(new SpotifySongModel((JObject)song["track"]));
                    }
                }
            }
            catch (Exception ex) { Logger.Log(ex); }
            return(results);
        }
Ejemplo n.º 6
0
        public async Task <SpotifyPlaylistModel> GetPlaylist(SpotifyPlaylistModel playlist)
        {
            try
            {
                if (playlist != null)
                {
                    HttpResponseMessage response = await this.GetAsync(string.Format("playlists/{0}", playlist.ID));

                    string responseString = await response.Content.ReadAsStringAsync();

                    Logger.LogDiagnostic(string.Format("Spotify Log: {0} - {1}", response.RequestMessage.ToString(), responseString));

                    return(new SpotifyPlaylistModel(JObject.Parse(responseString)));
                }
            }
            catch (Exception ex) { Logger.Log(ex); }
            return(null);
        }
Ejemplo n.º 7
0
        public async Task <bool> PlayPlaylist(SpotifyPlaylistModel playlist, bool random = false)
        {
            try
            {
                JObject payload = new JObject();
                payload["context_uri"] = playlist.Uri;

                JObject position = new JObject();
                position["position"] = 0;
                payload["offset"]    = position;

                if (random)
                {
                    IEnumerable <SpotifySongModel> playlistSongs = await this.GetPlaylistSongs(playlist);

                    if (playlistSongs != null && playlistSongs.Count() > 0)
                    {
                        position["position"] = RandomHelper.GenerateRandomNumber(playlistSongs.Count());
                    }
                }

                await this.PutAsync("me/player/shuffle?state=true", null);

                await Task.Delay(250);

                HttpResponseMessage playResponse = await this.PutAsync("me/player/play", this.CreateContentFromObject(payload));

                await Task.Delay(250);

                await this.DisableRepeat();

                return(playResponse.StatusCode == HttpStatusCode.NoContent);
            }
            catch (Exception ex) { Logger.Log(ex); }
            return(false);
        }
Ejemplo n.º 8
0
 public async Task RemoveSongFromPlaylist(SpotifyPlaylistModel playlist, SpotifySongModel song)
 {
     await this.RemoveSongsFromPlaylist(playlist, new List <SpotifySongModel>() { song });
 }