Пример #1
0
        static void Main(string[] args)
        {
            string username = args.Length > 0 ? args[0] : null;
            string blob     = args.Length > 1 ? args[1] : null;
            string password = null;

            byte[] appkey;

            bool selftest = args.Length > 2 ? args[2] == "selftest" : false;

            try
            {
                appkey = File.ReadAllBytes("spotify_appkey.key");
            }
            catch (IOException)
            {
                Console.WriteLine("Please download your binary app key from Spotify and put it in");
                Console.WriteLine("the working directory as 'spotify_appkey.key'. See here:");
                Console.WriteLine("https://developer.spotify.com/technologies/libspotify/keys/");
                Console.WriteLine("");
                Console.WriteLine("Press any key...");
                Console.ReadKey();
                return;
            }

            using (var consoleReader = new ConsoleReader())
            {
                WaitHandle[]   handles = new WaitHandle[2];
                AutoResetEvent spotifyEvent;
                handles[0] = spotifyEvent = new AutoResetEvent(false);
                handles[1] = consoleReader.InputReady;

                Console.WriteLine("Using libspotify {0}", Spotify.BuildId());

                if (username == null)
                {
                    Console.Write("Username (press enter to login with stored credentials): ");
                    username = (Console.ReadLine() ?? "").TrimEnd();
                    if (username == "")
                    {
                        username = null;
                    }
                }

                if (username != null && blob == null)
                {
                    Console.WriteLine("Password: "******"").TrimEnd();
                }

                using (SpShell shell = new SpShell(spotifyEvent, username, password, blob, selftest, consoleReader, appkey))
                {
                    //consoleReader.RequestInput("> ");
                    int next_timeout = 0;
                    while (!shell.IsFinished)
                    {
                        int ev = WaitHandle.WaitAny(handles, next_timeout != 0 ? next_timeout : Timeout.Infinite);
                        switch (ev)
                        {
                        case 0:
                        case WaitHandle.WaitTimeout:
                            do
                            {
                                shell.ProcessEvents(ref next_timeout);
                            } while (next_timeout == 0);
                            if (selftest)
                            {
                                // TODO: TestProcess
                            }
                            break;

                        case 1:
                            shell.ProcessConsoleInput(consoleReader.GetInput());
                            break;
                        }
                    }
                    Console.WriteLine("Logged out");
                }
                Console.WriteLine("Exiting...");
            }
        }
Пример #2
0
        public SpShell(AutoResetEvent aSpotifyEvent, string aUsername, string aPassword, string aBlob, bool aSelftest, ConsoleReader aReader, byte[] aAppKey)
        {
            iReader       = aReader;
            iSpotifyEvent = aSpotifyEvent;
            SpotifySessionConfig config = new SpotifySessionConfig();

            config.ApiVersion       = 12;
            config.CacheLocation    = aSelftest ? "" : "tmp";
            config.SettingsLocation = aSelftest ? "" : "tmp";
            config.ApplicationKey   = aAppKey;
            config.UserAgent        = "spshell#";
            config.Listener         = this;

            try
            {
                iSession = SpotifySession.Create(config);
            }
            catch (SpotifyException e)
            {
                Console.Error.WriteLine("Failed to create session: {0}", e.Message);
                throw;
            }

            iBrowser         = new Browser(iSession, this, aReader);
            iSearcher        = new Searcher(iSession, aReader);
            iTopLister       = new TopLister(iSession, aReader);
            iMessaging       = new Messaging(iSession, aReader, iBrowser);
            iStarManager     = new StarManager(iSession, aReader, iBrowser);
            iPlaylistManager = new PlaylistManager(iSession, aReader, iBrowser);

            iCommands = new ConsoleCommandDictionary(CmdDone)
            {
                { "log", CmdLog, "Enable/Disable logging to console (default off)" },
                { "logout", CmdLogout, "Logout and exit app" },
                { "exit", CmdLogout, "Logout and exit app" },
                { "quit", CmdLogout, "Logout and exit app" },
                { "browse", iBrowser.CmdBrowse, "Browse a Spotify URL" },
                { "search", iSearcher.CmdSearch, "Search" },
                { "whatsnew", iSearcher.CmdWhatsNew, "List new albums" },
                { "toplist", iTopLister.CmdTopList, "Browse toplists" },
                { "post", iMessaging.CmdPost, "Post track to a user's inbox" },
                { "inbox", iMessaging.CmdInbox, "View inbox" },
                { "star", iStarManager.CmdStar, "Star a track" },
                { "unstar", iStarManager.CmdUnstar, "Unstar a track" },
                { "starred", iStarManager.CmdStarred, "List all starred tracks" },
                { "playlists", iPlaylistManager.CmdPlaylists, "List playlists" },
                { "playlist", iPlaylistManager.CmdPlaylist, "List playlist contents" },
                { "set_autolink", iPlaylistManager.CmdSetAutolink, "Set autolinking state" },
                { "add_folder", iPlaylistManager.CmdAddFolder, "Add playlist folder" },
                { "update_subscriptions", iPlaylistManager.CmdUpdateSubscriptions, "Update playlist subscription info" },
                { "add", iPlaylistManager.CmdAddTrack, "Add track to playlist" },
                { "offline", iPlaylistManager.CmdPlaylistOffline, "Set offline mode for a playlist" },
            };
            iCommands.Add("help", iCommands.CmdHelp, "This help");

            try
            {
                if (aUsername == null)
                {
                    iSession.Relogin();
                    var reloginname = iSession.RememberedUser();
                    Console.Error.WriteLine("Trying to relogin as user {0}", reloginname);
                }
                else
                {
                    iSession.Login(aUsername, aPassword, true, aBlob);
                }
            }
            catch (SpotifyException e)
            {
                if (e.Error == SpotifyError.NoCredentials)
                {
                    Console.Error.WriteLine("No stored credentials");
                    throw;
                }
            }
        }