Пример #1
0
        static void Main(string[] args)
        {
            int port = 8080;

            if (args.Length > 0)
            {
                if (!int.TryParse(args[0], out port))
                {
                    Console.WriteLine("Reddit.NET OAuth Token Retriever");
                    Console.WriteLine("Created by Kris Craig");

                    Console.WriteLine();

                    Console.WriteLine("Usage:  AuthTokenRetriever [port] [App ID [App Secret]]");

                    Environment.Exit(Environment.ExitCode);
                }
            }

            string appId     = (args.Length >= 2 ? args[1] : null);
            string appSecret = (args.Length >= 3 ? args[2] : null);

            // If appId and appSecret are unspecified, use guided mode.  --Kris
            if (string.IsNullOrWhiteSpace(appId) && string.IsNullOrWhiteSpace(appSecret))
            {
                if (string.IsNullOrWhiteSpace(appId))
                {
                    Console.Write("App ID: ");
                    appId = Console.ReadLine();
                }

                Console.Write("App Secret (leave blank for 'installed'-type apps): ");
                appSecret = Console.ReadLine();

                Console.WriteLine();
                Console.WriteLine("** IMPORTANT:  Before you proceed any further, make sure you are logged into Reddit as the user you wish to authenticate! **");
                Console.WriteLine();

                Console.WriteLine("In the next step, a browser window will open and you'll be taken to Reddit's app authentication page.  Press any key to continue....");
                Console.ReadKey();
            }

            // Create a new instance of the auth token retrieval library.  --Kris
            AuthTokenRetrieverLib authTokenRetrieverLib = new AuthTokenRetrieverLib(appId, appSecret, port);

            // Start the callback listener.  --Kris
            authTokenRetrieverLib.AwaitCallback();

            // Clearning the Console causes an IO exception whilst running a non-console application, which in turn crashes uHttpSharp. --Adam
            // Console.Clear();  // Gets rid of that annoying logging exception message generated by the uHttpSharp library.  --Kris

            // Open the browser to the Reddit authentication page.  Once the user clicks "accept", Reddit will redirect the browser to localhost:8080, where AwaitCallback will take over.  --Kris
            OpenBrowser(authTokenRetrieverLib.AuthURL());

            Console.ReadKey();  // Hit any key to exit.  --Kris

            authTokenRetrieverLib.StopListening();

            Console.WriteLine("Token retrieval utility terminated.");
        }
Пример #2
0
        public static string AuthorizeUser(string appId, string appSecret = null, int port = 8080)
        {
            // Create a new instance of the auth token retrieval library.  --Kris
            AuthTokenRetrieverLib authTokenRetrieverLib = new AuthTokenRetrieverLib(appId, appSecret, port);

            // Start the callback listener.
            authTokenRetrieverLib.AwaitCallback();

            // Open the browser to the Reddit authentication page.  Once the user clicks "accept", Reddit will redirect the browser to localhost:8080, where AwaitCallback will take over.
            OpenBrowser(authTokenRetrieverLib.AuthURL());

            while (true)
            {
            }

            // Cleanup.
            authTokenRetrieverLib.StopListening();

            return(authTokenRetrieverLib.RefreshToken);
        }
Пример #3
0
        static void Main(string[] args)
        {
            string appId     = (args.Length >= 1 ? args[0] : null);
            string appSecret = (args.Length >= 2 ? args[1] : null);

            // Create a new instance of the auth token retrieval library.  --Kris
            AuthTokenRetrieverLib authTokenRetrieverLib = new AuthTokenRetrieverLib(appId, appSecret, 8080);

            // Start the callback listener.  --Kris
            authTokenRetrieverLib.AwaitCallback();
            Console.Clear();  // Gets rid of that annoying logging exception message generated by the uHttpSharp library.  --Kris

            // Open the browser to the Reddit authentication page.  Once the user clicks "accept", Reddit will redirect the browser to localhost:8080, where AwaitCallback will take over.  --Kris
            OpenBrowser(authTokenRetrieverLib.AuthURL());

            Console.ReadKey();  // Hit any key to exit.  --Kris

            authTokenRetrieverLib.StopListening();

            Console.WriteLine("Token retrieval utility terminated.");
        }
Пример #4
0
        public Workflow()
        {
            ConfigDir = Path.Combine(Environment.CurrentDirectory, "config");
            if (!Directory.Exists(ConfigDir))
            {
                Directory.CreateDirectory(ConfigDir);
            }

            ConfigPath = Path.Combine(ConfigDir, "RedditBerner.config.json");
            if (!File.Exists(ConfigPath))
            {
                // Create new config file and prompt user for token retrieval process.  --Kris
                Config = new Config(AppId);

                Console.WriteLine("****************************");
                Console.WriteLine("* Welcome to RedditBerner! *");
                Console.WriteLine("****************************");

                Console.WriteLine();

                Console.WriteLine("Before the bot can run, we'll need to link it to your Reddit account.");
                Console.WriteLine("This is very easy:  Whenever you're ready, press any key and a browser window will open and take you to the Reddit authentication page.");
                Console.WriteLine("Enter your username/password if you're not already logged in, then scroll down and click on the 'Allow' button to authorize this app to use your Reddit account.");

                Console.WriteLine();

                Console.WriteLine("Press any key to continue....");

                Console.ReadKey();

                AuthTokenRetrieverLib authTokenRetrieverLib = new AuthTokenRetrieverLib(AppId);
                authTokenRetrieverLib.AwaitCallback();

                Console.Clear();

                Console.WriteLine("Opening web browser....");

                OpenBrowser(authTokenRetrieverLib.AuthURL());

                DateTime start = DateTime.Now;
                while (string.IsNullOrWhiteSpace(authTokenRetrieverLib.RefreshToken) &&
                       start.AddMinutes(5) > DateTime.Now)
                {
                    Thread.Sleep(1000);
                }

                if (string.IsNullOrWhiteSpace(authTokenRetrieverLib.RefreshToken))
                {
                    throw new Exception("Unable to authorize Reddit; timeout waiting for Refresh Token.");
                }

                Config.AccessToken  = authTokenRetrieverLib.AccessToken;
                Config.RefreshToken = authTokenRetrieverLib.RefreshToken;

                SaveConfig();

                Console.WriteLine("Reddit authentication successful!  Press any key to continue....");

                Console.ReadKey();
            }
            else
            {
                Console.WriteLine("*************************");
                Console.WriteLine("*      RedditBerner     *");
                Console.WriteLine("* Created by Kris Craig *");
                Console.WriteLine("*************************");

                Console.WriteLine();

                LoadConfig();
            }

            ScriptsDir = Path.Combine(Environment.CurrentDirectory, "scripts");
            if (!Directory.Exists(ScriptsDir))
            {
                Directory.CreateDirectory(ScriptsDir);
            }

            if (!Directory.EnumerateFileSystemEntries(ScriptsDir).Any())
            {
                throw new Exception("Scripts directory cannot be empty!  Please add at least 1 text file to serve as a comment template so the app knows what content to post.");
            }

            LoadScripts();
            if (Scripts == null ||
                Scripts.Count.Equals(0))
            {
                throw new Exception("No suitable scripts found!  Please add at least 1 text file under 10 K to serve as a comment template so the app knows what content to post.");
            }

            Reddit = new RedditAPI(appId: AppId, refreshToken: Config.RefreshToken, accessToken: Config.AccessToken);

            SubredditsPath = Path.Combine(ConfigDir, "subreddits.json");
            if (!File.Exists(SubredditsPath))
            {
                Subreddits = new List <Subreddit>
                {
                    Reddit.Subreddit("StillSandersForPres"),
                    Reddit.Subreddit("WayOfTheBern"),
                    Reddit.Subreddit("SandersForPresident"),
                    Reddit.Subreddit("BernieSanders")
                };
                SaveSubreddits();
            }
            else
            {
                LoadSubreddits();
            }

            Random = new Random();
        }