public async static Task StartTest() { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); List <Match> matches = new List <Match>(); await Task.Run(async() => { matches = await api.GetMatchesOfSomeLeagues(Leagues.ProLeagueMen | Leagues.TTCupMen | Leagues.WinCupMen, DateTime.Now, 4); }); stopwatch.Stop(); Console.WriteLine("Выполнение парсинга заняло " + stopwatch.Elapsed.ToString(@"mm\:ss")); Console.WriteLine($"Всего получено информации о {matches.Count} матчах. Из низ информацию о очных имеют: {matches.Where(m=>m.H2HMatches!=null).Count()}."); }
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); } }); }