示例#1
0
        public async Task <IActionResult> CreatePlaylist([FromQuery] string token,
                                                         [FromBody] CreatePlaylistModel data)
        {
            var api = new SpotifyWebAPI
            {
                AccessToken = token,
                TokenType   = "Bearer",
            };

            var headers = new Dictionary <string, string> {
                { "Authorization", "Bearer " + token }
            };

            var me = await api.WebClient.DownloadAsync("https://api.spotify.com/v1/me", headers);

            var meJson = JToken.Parse(me.Item2);

            var userId = meJson["id"].Value <string>();

            var playlistName = data.PlaylistName
                               ?? "Spot " + DateTime.UtcNow.ToString("o");

            var playlist = await api.CreatePlaylistAsync(userId, playlistName, isPublic : false);

            var trackUris             = data.TrackIds.Select(x => "spotify:track:" + x).ToList();
            var addPlaylistTracksResp = await api.AddPlaylistTracksAsync(playlist.Id, trackUris);

            return(Ok(playlist.Id));
        }
示例#2
0
        public async Task <ValidationResponse> CreatePlayListAsync(CreatePlaylistRequest request)
        {
            var response = new ValidationResponse {
                Success = true, HttpStatusCode = 200
            };

            spotifyApi = new SpotifyWebAPI
            {
                AccessToken = request.AccessToken,
                TokenType   = request.TokenType
            };
            var userId = await spotifyApi.GetPrivateProfileAsync();

            var playlist = await spotifyApi.CreatePlaylistAsync(userId.Id, $"SpotifyTree - {request.PlaylistName}");

            var addToPlaylist = await spotifyApi.AddPlaylistTracksAsync(playlist.Id, request.SongIds.ToList());

            if (addToPlaylist.Error != null)
            {
                response.Success        = false;
                response.Message        = addToPlaylist.Error.Message;
                response.StatusCode     = addToPlaylist.Error.Status;
                response.HttpStatusCode = (int)addToPlaylist.StatusCode();
            }
            return(response);
        }
示例#3
0
        private async void AddToPlaylist(string userID, FullPlaylist playlist, List <FullTrack> tracks)
        {
            List <string> URIs = new List <string>();

            foreach (FullTrack track in tracks)
            {
                URIs.Add(track.Uri);
            }
            await spotify.AddPlaylistTracksAsync(userID, playlist.Id, URIs);
        }
        private static async Task AddSongsToPlaylist(SpotifyWebAPI api, string playlistID, List <string> songIDs)
        {
            ErrorResponse response = await api.AddPlaylistTracksAsync(playlistID, songIDs);

            if (!response.HasError())
            {
                Console.WriteLine("Success");
            }
            else
            {
                Console.WriteLine(response.Error.Message);
            }
        }
示例#5
0
        public async Task SaveDiscoverWeeklyPlaylist()
        {
            string discoverWeeklyId = await GetDiscoverWeeklyPlaylistId();

            var pagedTracks = await _spotifyWebAPI.GetPlaylistTracksAsync(discoverWeeklyId);

            DateTime      discoverCreatedate = pagedTracks.Items.Min(t => t.AddedAt);
            List <string> trackUris          = pagedTracks.Items.Select(t => t.Track.Uri).ToList();

            string newPlaylistName = $"Saved Weekly {discoverCreatedate:dd/MM/yy}";
            var    newPlaylist     = await _spotifyWebAPI.CreatePlaylistAsync(_userId, newPlaylistName, isPublic : false);

            await _spotifyWebAPI.AddPlaylistTracksAsync(newPlaylist.Id, trackUris);
        }
        public async Task UpdatePlaylistTracksAsync(Playlist playlist, List <Track> tracks)
        {
            var uris = tracks.Select(track => track.Uri).ToList();

            // Step One: Replace tracks (first 100 tracks)
            await spotify.ReplacePlaylistTracksAsync(playlist.Id, uris).ConfigureAwait(false);

            // Step Two: Add remaining tracks (as we are only able to replace/add 100 tracks
            uris.RemoveRange(0, Math.Min(uris.Count, 100));
            while (uris.Count > 100)
            {
                await spotify.AddPlaylistTracksAsync(playlist.Id, uris).ConfigureAwait(false);

                uris.RemoveRange(0, Math.Min(uris.Count, 100));
            }
        }