public void SendEmail(List <string> leagueNames) { SmtpClient smtpClient = GenerateClient(); MailMessage mail = GenerateMail(leagueNames); Bet365Logger.Log($"Sending email about: '{string.Join("\n", leagueNames)}' league(s)..."); var retryCount = _retryCount; while (true) { try { smtpClient.Send(mail); break; } catch (Exception ex) { Bet365Logger.Log($"Exception occurred on email sending. Reason: {ex.Message} Retry will be processed. {retryCount} retries remaining."); if (retryCount == 0) { throw; } Thread.Sleep(_retryTimeout); retryCount--; } } Bet365Logger.Log($"Email is successfully sent."); }
public void Ping(ChromeDriver driver, List <string> targetLeagues) { driver.Navigate().Refresh(); driver.Navigate().GoToUrl(driver.Url); var currentCheckLeagues = ParseWithRetry(driver); if (!currentCheckLeagues.Any()) { throw new Exception("List of leagues on page is empty."); } var addedLeagues = _addedLeaguesProvider.GetAddedLeagues(currentCheckLeagues); if (!addedLeagues.Any()) { Bet365Logger.Log($"There were no new leagues lately."); return; } Bet365Logger.Log($"Added leagues: \n{string.Join("\n", addedLeagues)}"); var addedLeaguesThatAreTargetOnes = addedLeagues.Intersect(targetLeagues, StringComparer.OrdinalIgnoreCase).ToList(); if (addedLeaguesThatAreTargetOnes.Any()) { _mailSender.SendEmail(addedLeaguesThatAreTargetOnes); return; } Bet365Logger.Log($"None of added leagues are the target"); }
public void TriggerProcess() { if (_isStarted) { return; } lock (_lockObject) { if (_isStarted) { return; } try { _isStarted = true; _chromeDriver = _driverGenerator.GetNavigatedDriver(); _timer.Change(0, _retryTimeout); } catch (Exception e) { Bet365Logger.Log($"Exception occured during monitoring starting. Reason: {e.Message}, StackTrace: {e.StackTrace}"); } } }
public void Stop() { if (!_isStarted) { return; } lock (_lockObject) { if (!_isStarted) { return; } try { _isStarted = false; _timer.Change(Timeout.Infinite, Timeout.Infinite); _chromeDriver.Close(); } catch (Exception e) { Bet365Logger.Log($"Exception occured during monitoring stopping. Reason: {e.Message}, StackTrace: {e.StackTrace}"); } finally { if (_chromeDriver != null) { _chromeDriver.Dispose(); } TargetLeagues = new List <string>(); _pagePinger.ClearLeagues(); } } }
public MainWindow() { Bet365Logger.Log("Bet365Monitor starting..."); InitializeComponent(); _mailSender = new MailSender(EmailSendingRetryTimeout, EmailSendingRetryCount); _driverGenerator = new DriverGenerator(WebPageUrlDk, SportDivClass, ChoosenSport, ChoosenLanguage, LeagueNameDivClass); _addedLeaguesProvider = new AddedLeaguesProvider(); _basketballLeaguesParser = new BasketballLeaguesParser(LeagueNameDivClass); _pagePinger = new PagePinger(_addedLeaguesProvider, _mailSender, _basketballLeaguesParser, WebPageUrlDk); _trigger = new MonitoringTrigger(_pagePinger, _driverGenerator, MonitoringTriggerRetryTimeout); _validEmailRegex = new Regex(ValidEmailPattern, RegexOptions.IgnoreCase); _disableElement.Add(EmailListBox); _disableElement.Add(EmailTextBox); _disableElement.Add(AddEmailButton); _disableElement.Add(RemoveEmailButton); _disableElement.Add(ClearEmailsButton); _disableElement.Add(LeaguesListBox); _disableElement.Add(LeagueTextBox); _disableElement.Add(AddLeagueButton); _disableElement.Add(RemoveLeagueButton); _disableElement.Add(ClearLeaguesButton); Bet365Logger.Log("Bet365Monitor successfully started."); }
private void StopButton_Click(object sender, RoutedEventArgs e) { Mouse.OverrideCursor = Cursors.Wait; Bet365Logger.Log("Monitoring stopping..."); ProgramStatusLabel.Content = "Stopping"; _trigger.Stop(); EnableFormElements(); StartButton.IsEnabled = true; StopButton.IsEnabled = false; ProgramStatusLabel.Foreground = _stoppedBrush; ProgramStatusLabel.Content = "Stopped"; Bet365Logger.Log("Monitoring successfully stopped."); Mouse.OverrideCursor = null; }
private void StartButton_Click(object sender, RoutedEventArgs e) { Bet365Logger.Log("Monitoring starting..."); if (EmailListBox.Items.Count == 0) { if (string.IsNullOrWhiteSpace(EmailTextBox.Text)) { MessageBox.Show("No emails added.\nAdd at least one email and try again!", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning); return; } if (!_validEmailRegex.IsMatch(EmailTextBox.Text)) { MessageBox.Show("No emails added.\nInvalid email!", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning); return; } EmailListBox.Items.Add(EmailTextBox.Text.Trim()); EmailTextBox.Text = string.Empty; } if (LeaguesListBox.Items.Count == 0) { if (string.IsNullOrWhiteSpace(LeagueTextBox.Text)) { LeagueTextBox.Text = string.Empty; MessageBox.Show("No target leagues added.\nAdd at least one league and try again!", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning); return; } LeaguesListBox.Items.Add(LeagueTextBox.Text.Trim()); LeagueTextBox.Text = string.Empty; } Mouse.OverrideCursor = Cursors.Wait; _mailSender.ToEmailAddresses = EmailListBox.Items.Cast <string>().ToList();; _trigger.TargetLeagues = LeaguesListBox.Items.Cast <string>().ToList(); _trigger.TriggerProcess(); ProgramStatusLabel.Content = "Running..."; ProgramStatusLabel.Foreground = _runningBrush; StartButton.IsEnabled = false; StopButton.IsEnabled = true; DisableFormElements(); Mouse.OverrideCursor = null; Bet365Logger.Log("Monitoring successfully started."); }
public List <string> ParseWithRetry(ChromeDriver driver) { var numberOfRetries = 8; for (var i = 0; i < numberOfRetries; i++) { var leagueNames = _basketballLeaguesParser.Parse(driver.PageSource); if (leagueNames.Any()) { return(leagueNames.ToList()); } Thread.Sleep(TimeSpan.FromSeconds(1)); } Bet365Logger.Log($"Searching for leagues finished after {numberOfRetries} retries."); return(new List <string>()); }
private List <IWebElement> WaitUntilElementsShowsUp(ChromeDriver driver, string elementClassName) { var numberOfRetries = 20; Bet365Logger.Log($"Searching for element with name '{elementClassName}'."); for (var i = 0; i < numberOfRetries; i++) { var elements = driver.FindElementsByClassName(elementClassName); if (elements.Any()) { Bet365Logger.Log($"Searching for element(s) with name '{elementClassName}' finished after {i} retries."); return(elements.ToList()); } Thread.Sleep(TimeSpan.FromSeconds(1)); } throw new NoSuchElementException($"Cannot find element with class name {elementClassName} after {numberOfRetries} retries."); }
private void OnTimerPingCallback(object state) { try { _pagePinger.Ping(_chromeDriver, TargetLeagues); } catch (Exception e) { var errorMessageBuilder = new StringBuilder(); errorMessageBuilder.AppendLine($"{e.GetType().Name} occured during page pinging."); errorMessageBuilder.AppendLine($"Reason: {e.Message}"); errorMessageBuilder.AppendLine($"StackTrace: {e.StackTrace}"); if (e.InnerException != null) { errorMessageBuilder.AppendLine($"Additional reason: {(e.InnerException.Message)}"); } Bet365Logger.Log(errorMessageBuilder.ToString()); } }