Esempio n. 1
0
 /// <summary>
 /// Initializes a new instance of Eleia.Bot
 /// </summary>
 /// <param name="handler">CoyoteHandler to work with Coyote</param>
 /// <param name="analyzer">PostAnalyzer for analyzing posts</param>
 /// <param name="list">Blacklist for blacklisting forums, posts or so on</param>
 /// <param name="loggerFactory">LoggerFactory for login purposes</param>
 public Bot(CoyoteHandler handler, PostAnalyzer analyzer, Blacklist list, ILoggerFactory loggerFactory)
 {
     coyoteHandler = handler;
     postAnalyzer  = analyzer;
     blacklist     = list;
     logger        = loggerFactory.CreateLogger("Bot");
 }
Esempio n. 2
0
        private static void Main(string[] args)
        {
            analyzed = new HashSet <int>();

            var config = new ConfigurationBuilder()
                         .SetBasePath(Directory.GetCurrentDirectory())
                         .AddJsonFile("appsettings.json", optional: true, reloadOnChange: false)
                         .AddEnvironmentVariables("ELEIA_")
                         .AddCommandLine(args)
                         .Build();

            var username = config.GetValue <string>("username");
            var password = config.GetValue <string>("password");

            timeBetweenUpdates = config.GetValue("timeBetweenUpdates", 60);

            nagMessage = config.GetValue("nagMessage", "Hej! Twój post prawdopodobnie zawiera niesformatowany kod. Użyj znaczników ``` aby oznaczyć, co jest kodem, będzie łatwiej czytać. (jestem botem, ta akcja została wykonana automatycznie, prawdopodobieństwo {0})");

            Endpoints.IsDebug = config.GetValue("useDebug4p", true);
            postComments      = config.GetValue("postComments", false);

            var serviceProvider = new ServiceCollection()
                                  .AddLogging(builder => builder
                                              .AddConfiguration(config.GetSection("Logging"))
                                              .AddConsole())
                                  .AddSingleton(config)
                                  .AddTransient <CoyoteHandler>()
                                  .AddTransient <PostAnalyzer>()
                                  .BuildServiceProvider();

            ch = serviceProvider.GetService <CoyoteHandler>();
            pa = serviceProvider.GetService <PostAnalyzer>();

            logger = serviceProvider.GetService <ILoggerFactory>().CreateLogger("Eleia");
            logger.LogInformation("Eleia is running...");

            if (postComments && (username == null || password == null))
            {
                logger.LogError("Username or password is not provided, but posting comments is set. Exiting.");
                Thread.Sleep(100);
                Environment.Exit(1);
            }

            logger.LogDebug("Will use username: {0}, will post comments: {1}, will use real 4programmers.net: {2}", username, postComments, !Endpoints.IsDebug);

            if (postComments)
            {
                ch.Login(username, password).Wait();
            }

            if (timeBetweenUpdates <= 0)
            {
                AnalyzeNewPosts().Wait();
                logger.LogDebug("Single run completed");
            }
            else if (timeBetweenUpdates > 0)
            {
                while (true)
                {
                    AnalyzeNewPosts().Wait();
                    logger.LogDebug("Going to sleep for {0} minutes", timeBetweenUpdates);
                    Thread.Sleep(TimeSpan.FromMinutes(timeBetweenUpdates));
                }
            }
        }