Exemple #1
0
        public async Task <ActionResult <SpotifyData> > GetExportAsync([FromQuery] SpotifyToken token)
        {
            var hasMore = false;
            var offset  = 0;

            if (string.IsNullOrEmpty(token?.AccessToken))
            {
                return(RedirectToAction(nameof(GetAuthorization)));
            }
            var data = new SpotifyData();
            var user = await _spotifyService.GetMeAsync(token);

            if (string.IsNullOrEmpty(user.Id))
            {
                return(RedirectToPage($"/Index", new { Message = "Invalid token, login!", Type = "warning" }));
            }
            data.UserId      = user.Id;
            data.DisplayName = user.DisplayName;
            do
            {
                var playlists = await _spotifyService.GetPlaylistsAsync(token, offset);

                foreach (var playlist in playlists.Items)
                {
                    if (!playlist.Owner.Id.Equals(user.Id))
                    {
                        data.FollowPlaylists.Add(playlist.Uri);
                        continue;
                    }

                    var playlistItem = new SpotifyPlaylist(playlist);
                    var _continue    = false;
                    offset = 0;
                    do
                    {
                        var playlistTracks = await _spotifyService.GetPlaylistsTracksAsync(token, playlist.Id, offset);

                        if (playlistTracks.Total == 0)
                        {
                            _continue = true; break;
                        }
                        foreach (var playlistTrack in playlistTracks.Items)
                        {
                            if (playlistTrack.IsLocal)
                            {
                                continue;                        //ignore local
                            }
                            playlistItem.Tracks.Add(new SpotifyPlaylistTrack(playlistTrack.Track));
                        }
                        hasMore = !string.IsNullOrEmpty(playlistTracks.Next);
                        offset  = playlistTracks.Offset + playlistTracks.Limit;
                    } while (hasMore);
                    if (_continue)
                    {
                        continue;
                    }
                    data.Playlists.Add(playlistItem);
                }
                hasMore = !string.IsNullOrEmpty(playlists.Next);
                offset  = playlists.Offset + playlists.Limit;
            } while (hasMore);

            offset = 0;
            do
            {
                var tracks = await _spotifyService.GetTracksAsync(token, offset);

                foreach (var track in tracks.Items)
                {
                    data.Tracks.Add(new SpotifyPlaylistTrack(track.Track));
                }
                hasMore = !string.IsNullOrEmpty(tracks.Next);
                offset  = tracks.Offset + tracks.Limit;
            } while (hasMore);
            return(data);
        }