Beispiel #1
0
        private async Task OnImplictGrantReceivedAsync(object _, ImplictGrantResponse response)
        {
            // Stop listening to server.
            await _server !.Stop();

            await SpotifyRequests.Instance.SetLoginTokenAsync(response.AccessToken);

            Cleanup();
        }
        private async Task OnImplicitGrantReceived(object sender, ImplictGrantResponse response)
        {
            await _server.Stop();

            var spotify = new SpotifyClient(response.AccessToken);

            Client = spotify;
            OnAuthEvent?.Invoke();
        }
Beispiel #3
0
    private async Task OnImplicitGrantReceived(object sender, ImplictGrantResponse response)
    {
        // Stop server
        await _server.Stop();

        _lastAuthToken = response;

        // Trigger complete with auth token
        OnAuthenticatorComplete?.Invoke(_lastAuthToken);
    }
Beispiel #4
0
    public void DeauthorizeUser()
    {
        if (_server != null)
        {
            _server.Dispose();
        }

        if (_lastAuthToken != null)
        {
            _lastAuthToken = null;
        }
    }
Beispiel #5
0
        /// <summary>
        /// Called when the user has been authorised
        /// </summary>
        private async Task OnImplicitGrantReceivedAsync(object sender, ImplictGrantResponse response)
        {
            // stop the server and create a new client using the token, get the current users id
            await Server.Stop();

            Client = new SpotifyClient(response.AccessToken);
            string user = (await Client.UserProfile.Current()).Id;

            // make sure the users id isnt null/empty for some strange reason
            if (!String.IsNullOrEmpty(user) && !String.IsNullOrWhiteSpace(user))
            {
                while (true)
                {
                    // get the current users playlists and make sure the list isnt null
                    Paging <SimplePlaylist> playlists = await Client.Playlists.GetUsers(user);

                    if (playlists != null && playlists.Items != null)
                    {
                        // list the playlists to the user
                        ListPlaylists(user, playlists);

                        try
                        {
                            // ask the user which playlist we want to shuffle
                            Console.Write("\nEnter ID of playlist to shuffle: ");
                            int playlistId = Convert.ToInt32(Console.ReadLine());
                            Console.Clear();

                            // make sure the playlist id is valid
                            if (playlistId >= 0 && playlistId < playlists.Items.Count)
                            {
                                // start the shuffle procedure and get the playlist uri
                                string playlistUri = playlists.Items[playlistId].Uri.Split(':')[2];

                                // create our empty lists ready to occupy
                                List <PlaylistTrack <IPlayableItem> > allTracks = new List <PlaylistTrack <IPlayableItem> >();
                                List <Item> songs         = new List <Item>();
                                List <Item> songsToRemove = new List <Item>();

                                // calculate how many loops of 100 to cycle through the whole playlist, most api calls are limited to 100 tracks
                                int loops     = (int)playlists.Items[playlistId].Tracks.Total / 100;
                                int remainder = (int)playlists.Items[playlistId].Tracks.Total % 100;

                                // get all the tracks from the playlist and populate the lists
                                await GetAllTracksAsync(playlistUri, allTracks, loops);

                                PopulateSongLists(allTracks, songs, songsToRemove);

                                // recalculate the loops and remainder of the playlist, some of the tracks may have been invalid
                                loops     = Tracks / 100;
                                remainder = Tracks % 100;
                                Log(LogType.Info, "Shuffle", $"Tracks: {Tracks}, Loops: {loops}, Remainder: {remainder}, Local tracks: {Locals}");

                                // do the actual shuffle
                                List <string> shuffled = Shuffle(songs);
                                if (shuffled.Count != songsToRemove.Count)
                                {
                                    throw new Exception($"For some reason there are not the same amount of songs in each list... Shuffled: {shuffled.Count}, Original: {songsToRemove.Count}");
                                }

                                // remove the tracks from the playlist and then add the shuffled list back
                                await RemoveSongsFromPlaylistAsync(playlistUri, songsToRemove, loops);

                                await Task.Delay(100);
                                await AddSongsToPlaylistAsync(playlistUri, shuffled, loops);

                                await Task.Delay(100);

                                // shuffle local tracks
                                await ReorderSongsAsync(playlistUri);

                                Log(LogType.Success, "Shuffle", "Playlist shuffle complete.");
                            }
                            else
                            {
                                Log(LogType.Error, "Playlist", "Invalid playlist ID");
                            }
                        }
                        catch (APIException apiEx)
                        {
                            Log(LogType.Error, apiEx.Response.StatusCode.ToString(), apiEx.Message);
                        }
                        catch (Exception ex)
                        {
                            Log(LogType.Error, ex.Source, ex.Message);
                        }
                    }
                    else
                    {
                        Log(LogType.Error, "Playlist", "No playlists found");
                    }

                    // check how long left of token
                    int timeLeft = response.ExpiresIn - (int)(DateTime.UtcNow - response.CreatedAt).TotalSeconds;

                    // if enough time remains, ask if they want to shuffle another playlist...
                    if (timeLeft > 60)
                    {
                        Console.Write($"\n\nTime left on token: {timeLeft} seconds");
                        Console.Write("\nWould you like to shuffle another playlist? Y/N ");
                        var key = Console.ReadKey();
                        if (!key.Key.Equals(ConsoleKey.Y))
                        {
                            Log(LogType.Info, "Program", "Exitting program...");
                            await Task.Delay(500);

                            Environment.Exit(0);
                        }
                        else
                        {
                            await Task.Delay(500);
                        }
                    }
                    else // else, ask if they want to obtain a new token
                    {
                        Console.Write("\n\nToken expired... Would you like to obtain a new token? Y/N ");
                        var key = Console.ReadKey();
                        if (!key.Key.Equals(ConsoleKey.Y))
                        {
                            Log(LogType.Info, "Program", "Exitting program...");
                            await Task.Delay(500);

                            Environment.Exit(0);
                        }
                        else
                        {
                            Log(LogType.Info, "Program", "Obtaining new token...");
                            await Server.Start();

                            ObtainToken();
                            return;
                        }
                    }
                }
            }
            else
            {
                Log(LogType.Error, "Playlist", "Invalid user id");
            }

            // end the program if we make it to here...
            Environment.Exit(0);
        }
Beispiel #6
0
        private static async Task OnImplictGrantReceived(object sender, ImplictGrantResponse response)
        {
            await _server.Stop();

            await Start(response.AccessToken);
        }
Beispiel #7
0
        private static async Task OnImplicitGrantReceived(object sender, ImplictGrantResponse response)
        {
            await _server.Stop();

            _spotify = new SpotifyClient(response.AccessToken);
        }