Exemplo n.º 1
0
        /// <summary>
        /// The entry point of the program, where the program control starts and ends.
        /// </summary>
        /// <param name="args">The command-line arguments.</param>
        public static void Main(string[] args)
        {
            try
             {
            //-user HFYBot -pass password -sub HFY -maxcount 500

            string username = "";
            string password = "";
            string subname = "HFY"; // GetSubreddit will remove /r/ from the front anyways. Lowercase doesn't matter
            int maxCount = 500;
            int maxDays = 60;
            bool fastcutoff = false;
            bool reallypost = false;
            bool verbose = false;
            bool quiet = false;
            bool showhelp = false;
            string oAuthClientID = "";
            string oAuthClientSecret = "";

            var optsParse = new NDesk.Options.OptionSet()
             {
            {"user="******"{username} of the account to run this bot under (required).", v => {if (v != null) username=v;} },
            {"pass="******"{password} for the account to run this bot under (required).", v => {if (v != null) password=v;} },
            {"oauthid=", "{client id} as generated by https://www.reddit.com/prefs/apps (optional).", v => {if (v != null) oAuthClientID=v;} },
            {"oauthsecret=", "{client secret} as generated by https://www.reddit.com/prefs/apps (optional).", v => {if (v != null) oAuthClientSecret=v;} },
            {"sub=", "{subbedit} to process (optional, defaults to HFY).", v => {if (v != null) subname=v;} },
            {"maxcount=", "{max} # of posts to scan (optional, defaults to 500).", (int v) =>  maxCount=v },
            {"maxdays=", "{max} # of days to scan into the past for unprocessed posts (optional, defaults to 60).", (int v) =>  maxDays=v },
            {"fastcutoff", "if set, then only considers posts made after the last HFY bot comment.", v => { if (v != null) fastcutoff=true; }  },
            {"reallypost", "if set, the posts are actually made; if not set, then dumped to console.", v => { if (v != null) reallypost=true; }  },
            {"v|verbose", "if set, then progress lines are displayed as the posts and comments are built.", v => { if (v != null) verbose=true; }  },
            {"q|quiet", "if set, then as little output as possible is generated.", v => { if (v != null) quiet=true; }  },
            {"h|help", "show this help.", v => { if (v != null) showhelp=true; }  },
             };

            try
            {
               List<string> leftovers = optsParse.Parse(args);
            }
            catch (NDesk.Options.OptionException e)
            {
               Console.WriteLine(e.Message);
               Console.WriteLine("Try 'hfybot --help' for more information.");
               return;
            }

            if (showhelp)
            {
               optsParse.WriteOptionDescriptions(Console.Out);
               return;
            }

            if (username == "" || password == "")
            {
               Console.WriteLine("Username and password must be supplied.");
               System.Environment.Exit(0);
            }

            if (subname == "")
            {
               Console.WriteLine("Subbedit name must be supplied.");
               System.Environment.Exit(0);
            }

            if (maxCount < 1 || maxCount > 500)
               maxCount = 500;   // enforce default

            if (maxDays < 1 || maxDays > 90)
               maxDays = 60;   // enforce default

            if (verbose)
               quiet = false;

            if (oAuthClientID != "" && oAuthClientSecret != "")
            {
               // Log in via oauth (which still requires the username and password, but also a secondary username and password called a client id and secret)
               if (!quiet)
                  Console.WriteLine("Logging in user {0} via OAUTH2 protocol...", username);

               try
               {
                  authProvider = new AuthProvider(oAuthClientID, oAuthClientSecret, "http://reddit.com");
                  authTokenString = authProvider.GetOAuthToken(username, password);
                  if (authTokenString == "")
                  {
                     Console.WriteLine("Login for user {0} (user:{1}) refused, empty token returned.", oAuthClientID, username);
                     System.Environment.Exit(0);
                  }
                  if (verbose)
                     Console.WriteLine("Auth token is {0}", authTokenString);
                  reddit = new Reddit(authTokenString);
               }
               catch (System.Security.Authentication.AuthenticationException)
               {
                  Console.WriteLine("Login for user {0} (user:{1}) refused.", oAuthClientID, username);
                  System.Environment.Exit(0);
               }
               catch (System.Net.WebException we)
               {
                  Console.WriteLine("Network error when connecting to reddit. Please check your connection. {0}",we.ToString());
                  System.Environment.Exit(0);
               }
            }
            else
            {
               // Log in without oauth in a traditional mode.
               if (!quiet)
                  Console.WriteLine("Logging in user {0}...", username);

               try
               {
                  reddit = new Reddit(username, password);
               }
               catch (System.Security.Authentication.AuthenticationException)
               {
                  Console.WriteLine("Login for user {0} refused.", username);
                  System.Environment.Exit(0);
               }
               catch (System.Net.WebException we)
               {
                  Console.WriteLine("Network error when connecting to reddit. Please check your connection. {0}",we.ToString());
                  System.Environment.Exit(0);
               }
            }

            reddit.RateLimit = WebAgent.RateLimitMode.Burst;

            if (!quiet)
               Console.WriteLine("Login successful. Getting sub {0}...", subname);

            RedditSharp.Things.Subreddit subit = reddit.GetSubreddit(subname);
            if (subit == null)
            {
               Console.WriteLine("Subbedit {0} does not exist.", subname);
               System.Environment.Exit(0);
            }

            if (!quiet)
            {
               Console.WriteLine("Processing posts and creating new comments as needed (maxcount={0}, maxdays={1}).", maxCount, maxDays);
               if (!reallypost)
                  Console.WriteLine("NOTE: running in TEST mode only, will not actually make comments.");
            }

            ProcessNewPosts prm = new ProcessNewPosts(reddit, subit);
            prm.Run(maxCount, maxDays, fastcutoff, reallypost, verbose);
             }
             catch (Exception e)
             {
            Console.WriteLine("General exception: {0}", e.ToString());
            System.Environment.Exit(0);
             }
        }
Exemplo n.º 2
0
        /// <summary>
        /// The entry point of the program, where the program control starts and ends.
        /// </summary>
        /// <param name="args">The command-line arguments.</param>
        public static void Main(string[] args)
        {
            try
            {
                //-user HFYBot -pass password -sub HFY -maxcount 500

                string username          = "";
                string password          = "";
                string subname           = "HFY"; // GetSubreddit will remove /r/ from the front anyways. Lowercase doesn't matter
                int    maxCount          = 500;
                int    maxDays           = 60;
                bool   fastcutoff        = false;
                bool   reallypost        = false;
                bool   verbose           = false;
                bool   quiet             = false;
                bool   showhelp          = false;
                string oAuthClientID     = "";
                string oAuthClientSecret = "";

                var optsParse = new NDesk.Options.OptionSet()
                {
                    { "user="******"{username} of the account to run this bot under (required).", v => { if (v != null)
                                                                                                     {
                                                                                                         username = v;
                                                                                                     }
                      } },
                    { "pass="******"{password} for the account to run this bot under (required).", v => { if (v != null)
                                                                                                      {
                                                                                                          password = v;
                                                                                                      }
                      } },
                    { "oauthid=", "{client id} as generated by https://www.reddit.com/prefs/apps (optional).", v => { if (v != null)
                                                                                                                      {
                                                                                                                          oAuthClientID = v;
                                                                                                                      }
                      } },
                    { "oauthsecret=", "{client secret} as generated by https://www.reddit.com/prefs/apps (optional).", v => { if (v != null)
                                                                                                                              {
                                                                                                                                  oAuthClientSecret = v;
                                                                                                                              }
                      } },
                    { "sub=", "{subbedit} to process (optional, defaults to HFY).", v => { if (v != null)
                                                                                           {
                                                                                               subname = v;
                                                                                           }
                      } },
                    { "maxcount=", "{max} # of posts to scan (optional, defaults to 500).", (int v) => maxCount = v },
                    { "maxdays=", "{max} # of days to scan into the past for unprocessed posts (optional, defaults to 60).", (int v) => maxDays = v },
                    { "fastcutoff", "if set, then only considers posts made after the last HFY bot comment.", v => { if (v != null)
                                                                                                                     {
                                                                                                                         fastcutoff = true;
                                                                                                                     }
                      } },
                    { "reallypost", "if set, the posts are actually made; if not set, then dumped to console.", v => { if (v != null)
                                                                                                                       {
                                                                                                                           reallypost = true;
                                                                                                                       }
                      } },
                    { "v|verbose", "if set, then progress lines are displayed as the posts and comments are built.", v => { if (v != null)
                                                                                                                            {
                                                                                                                                verbose = true;
                                                                                                                            }
                      } },
                    { "q|quiet", "if set, then as little output as possible is generated.", v => { if (v != null)
                                                                                                   {
                                                                                                       quiet = true;
                                                                                                   }
                      } },
                    { "h|help", "show this help.", v => { if (v != null)
                                                          {
                                                              showhelp = true;
                                                          }
                      } },
                };

                try
                {
                    List <string> leftovers = optsParse.Parse(args);
                }
                catch (NDesk.Options.OptionException e)
                {
                    Console.WriteLine(e.Message);
                    Console.WriteLine("Try 'hfybot --help' for more information.");
                    return;
                }

                if (showhelp)
                {
                    optsParse.WriteOptionDescriptions(Console.Out);
                    return;
                }

                if (username == "" || password == "")
                {
                    Console.WriteLine("Username and password must be supplied.");
                    System.Environment.Exit(0);
                }

                if (subname == "")
                {
                    Console.WriteLine("Subbedit name must be supplied.");
                    System.Environment.Exit(0);
                }

                if (maxCount < 1 || maxCount > 500)
                {
                    maxCount = 500; // enforce default
                }
                if (maxDays < 1 || maxDays > 90)
                {
                    maxDays = 60; // enforce default
                }
                if (verbose)
                {
                    quiet = false;
                }

                if (oAuthClientID != "" && oAuthClientSecret != "")
                {
                    // Log in via oauth (which still requires the username and password, but also a secondary username and password called a client id and secret)
                    if (!quiet)
                    {
                        Console.WriteLine("Logging in user {0} via OAUTH2 protocol...", username);
                    }

                    try
                    {
                        authProvider    = new AuthProvider(oAuthClientID, oAuthClientSecret, "http://reddit.com");
                        authTokenString = authProvider.GetOAuthToken(username, password);
                        if (authTokenString == "")
                        {
                            Console.WriteLine("Login for user {0} (user:{1}) refused, empty token returned.", oAuthClientID, username);
                            System.Environment.Exit(0);
                        }
                        if (verbose)
                        {
                            Console.WriteLine("Auth token is {0}", authTokenString);
                        }
                        reddit = new Reddit(authTokenString);
                    }
                    catch (System.Security.Authentication.AuthenticationException)
                    {
                        Console.WriteLine("Login for user {0} (user:{1}) refused.", oAuthClientID, username);
                        System.Environment.Exit(0);
                    }
                    catch (System.Net.WebException we)
                    {
                        Console.WriteLine("Network error when connecting to reddit. Please check your connection. {0}", we.ToString());
                        System.Environment.Exit(0);
                    }
                }
                else
                {
                    // Log in without oauth in a traditional mode.
                    if (!quiet)
                    {
                        Console.WriteLine("Logging in user {0}...", username);
                    }

                    try
                    {
                        reddit = new Reddit(username, password);
                    }
                    catch (System.Security.Authentication.AuthenticationException)
                    {
                        Console.WriteLine("Login for user {0} refused.", username);
                        System.Environment.Exit(0);
                    }
                    catch (System.Net.WebException we)
                    {
                        Console.WriteLine("Network error when connecting to reddit. Please check your connection. {0}", we.ToString());
                        System.Environment.Exit(0);
                    }
                }

                reddit.RateLimit = WebAgent.RateLimitMode.Burst;

                if (!quiet)
                {
                    Console.WriteLine("Login successful. Getting sub {0}...", subname);
                }

                RedditSharp.Things.Subreddit subit = reddit.GetSubreddit(subname);
                if (subit == null)
                {
                    Console.WriteLine("Subbedit {0} does not exist.", subname);
                    System.Environment.Exit(0);
                }

                if (!quiet)
                {
                    Console.WriteLine("Processing posts and creating new comments as needed (maxcount={0}, maxdays={1}).", maxCount, maxDays);
                    if (!reallypost)
                    {
                        Console.WriteLine("NOTE: running in TEST mode only, will not actually make comments.");
                    }
                }

                ProcessNewPosts prm = new ProcessNewPosts(reddit, subit);
                prm.Run(maxCount, maxDays, fastcutoff, reallypost, verbose);
            }
            catch (Exception e)
            {
                Console.WriteLine("General exception: {0}", e.ToString());
                System.Environment.Exit(0);
            }
        }