Exemple #1
0
        /// <summary>
        /// Defines the entry point of the application.
        /// </summary>
        /// <param name="args">The arguments.</param>
        static void Main(string[] args)
        {
            var    url        = "http://localhost:9696/";
            string webHookUrl = "";

            if (args.Length > 0)
            {
                MainUrl    = webHookUrl = args[0];
                GrafanaUrl = args[1];
            }

            // Our web server is disposable.
            using (var server = CreateWebServer(url))
            {
                // Once we've registered our modules and configured them, we call the RunAsync() method.
                server.RunAsync();

                //var browser = new System.Diagnostics.Process()
                //{
                //    StartInfo = new System.Diagnostics.ProcessStartInfo(url) { UseShellExecute = true }
                //};
                //browser.Start();

                var result = TelegramBotManager.UpdateWebHook(webHookUrl + "/api/message").Result;
                // Wait for any key to be pressed before disposing of our web server.
                // In a service, we'd manage the lifecycle of our web server using
                // something like a BackgroundWorker or a ManualResetEvent.
                Console.ReadKey(true);
            }
        }
        private static async Task VerifyWatches(int metricId, float value)
        {
            IEnumerable <Watch> watches = await DataService.Watches.WatchesByMetric(metricId);

            if (watches.Count() > 0)
            {
                int chatid = await DataService.Users.UserIdByMetric(metricId);

                foreach (Watch watch in watches)
                {
                    if (watch.Sent == 0 && JintService.EvaluateExpression(watch.Expression, value))
                    {
                        await TelegramBotManager.SendMessageSameKeyboard(chatid, watch.Message);

                        watch.Sent = 1;
                        await DataService.Watches.UpdateWatch(watch);
                    }
                }
            }
        }
 private async void Start(object sender, EventArgs e)
 {
     await Task.Run(async() =>
     {
         Invoke(new MethodInvoker(delegate
         {
             startButton.Enabled = false;
             stopButton.Enabled  = true;
         }));
         IsWorking = true;
         telegram  = new TelegramBotManager(AppSettings.ApiTelegramTokenKey);
         await telegram.LoadSubs();
         InitDriver();
         bool flag          = true;
         DateTime lastParse = DateTime.Now;
         Leagues l          = 0;
         if (AppSettings.ProLeague)
         {
             l |= Leagues.ProLeagueMen;
         }
         if (AppSettings.TTCup)
         {
             l |= Leagues.TTCupMen;
         }
         if (AppSettings.WinCup)
         {
             l |= Leagues.WinCupMen;
         }
         if (AppSettings.SetkaCup)
         {
             l |= Leagues.SetkaCupMen;
         }
         List <Match> matches = new List <Match>();
         try
         {
             matches = await api.GetMatchesOfSomeLeagues(l, DateTime.Now, AppSettings.MinH2HCount);
             if (DateTime.Now.TimeOfDay <= AppSettings.StartPosting)
             {
                 flag = false;
             }
         }
         catch (Exception exception)
         {
             logger.Error($"Ошибка - {exception.Message} при получении списка матчей. Повтор c в 3 часа ночи.");
         }
         matches = matches.Where(m => m.H2HMatches.Count >= AppSettings.MinH2HCount).OrderBy(m => m.StartTime).ToList();
         FeelDataGrid(matches);
         while (IsWorking)
         {
             bool error = false;
             if (flag && DateTime.Now.TimeOfDay >= TimeSpan.FromHours(3) && DateTime.Now.TimeOfDay <= TimeSpan.FromHours(5))
             {
                 try
                 {
                     matches   = await api.GetMatchesOfSomeLeagues(l, DateTime.Now, AppSettings.MinH2HCount);
                     flag      = false;
                     lastParse = DateTime.Now;
                 }
                 catch (Exception exception)
                 {
                     error = true;
                     logger.Error($"Ошибка - {exception.Message} при получении списка матчей. Повтор c перезапуском хрома.");
                     if (!IsWorking)
                     {
                         return;
                     }
                 }
                 matches = matches.Where
                           (
                     m =>
                     m.H2HMatches.Count >= AppSettings.H2HCountCheck &&
                     m.StartTime.TimeOfDay >= AppSettings.StartPosting &&
                     m.StartTime.TimeOfDay <= AppSettings.EndPosting
                           )
                           .OrderBy(m => m.StartTime).ToList();
                 FeelDataGrid(matches);
             }
             if (error)
             {
                 if (!IsWorking)
                 {
                     return;
                 }
                 driver.Quit();
                 InitDriver();
                 flag = true;
                 continue;
             }
             for (int i = 0; i < matches.Count; i++)
             {
                 if (matches[i].StartTime == DateTime.MinValue)
                 {
                     continue;
                 }
                 var startTime   = matches[i].StartTime.ToUnixTime();
                 startTime      -= 60 * AppSettings.MinutesBeforePost;
                 var startTimeDt = startTime.ToDateTime();
                 if (startTimeDt <= DateTime.Now && startTimeDt.TimeOfDay > AppSettings.StartPosting && startTimeDt.TimeOfDay < AppSettings.EndPosting)
                 {
                     await telegram.SendToSubs(matches[i], AppSettings);
                 }
                 if (matches[i].Posted)
                 {
                     matches.RemoveAt(i);
                 }
             }
             if (DateTime.Now - lastParse >= TimeSpan.FromHours(12))
             {
                 flag = true;
             }
             FeelDataGrid(matches);
             Thread.Sleep(60 * 1000);
         }
     });
 }