Exemple #1
0
        private string LoadData(MonitoringTask task)
        {
            var webDriver = WebDriver;

            webDriver.Url = task.Url;

            WaitHelper.WaitUntil(webDriver, TimeSpan.FromSeconds(60),
                                 ElementExists(task.SeleniumSelector()),
                                 $"Element '{task.Selector}' not found");

            if (!task.AllowEmptyContent)
            {
                try
                {
                    WaitHelper.WaitUntil(webDriver, TimeSpan.FromSeconds(20),
                                         d => !String.IsNullOrWhiteSpace(d.FindElement(task.SeleniumSelector()).Text),
                                         $"Element '{task.Selector}' is empty");
                }
                catch (WebDriverTimeoutException ex)
                {
                    Log(ex.Message);
                }
            }

            Thread.Sleep(3000);

            var element = webDriver.FindElement(task.SeleniumSelector());

            return(element.Text.Trim());
        }
Exemple #2
0
 public WebsiteState GetOrDefault(MonitoringTask task)
 {
     if (_state.ContainsKey(task.Id))
     {
         return(_state[task.Id]);
     }
     return(new WebsiteState(null, DateTime.MinValue));
 }
Exemple #3
0
        private void Process(MonitoringTask task)
        {
            if ((DateTime.Now - _stateContainer.GetOrDefault(task).LastUpdateTime) < TimeSpan.FromSeconds(task.MinDelay))
            {
                Log($"Skipping {task.UrlDomain} (min delay)");
                return;
            }

            Log($"Running {task.UrlDomain}");

            string text = LoadData(task);

            if (!task.AllowEmptyContent)
            {
                _tasksConsecutiveEmptyCounts[task.Id] = String.IsNullOrWhiteSpace(text) ? _tasksConsecutiveEmptyCounts[task.Id] + 1 : 0;

                if (String.IsNullOrWhiteSpace(text) &&
                    !_stateContainer.Matches(task, text)) // not already recorded this as change (optimization to avoid unnecessary delays)
                {
                    SaveBrowserError("empty");

                    text = LoadData(task);

                    // still empty
                    if (String.IsNullOrWhiteSpace(text))
                    {
                        SaveBrowserError("empty2");

                        // need to wait until the next check before reporting
                        if (task.SkipUntilNextCheckIfEmpty && _tasksConsecutiveEmptyCounts[task.Id] < 2)
                        {
                            return;
                        }
                    }
                }
            }

            if (!_stateContainer.Matches(task, text))
            {
                Log("Changed.");

                string before = _stateContainer.Get(task).Data;

                var diff = _differ.Diff(before, text);

                _stateContainer.Set(task, text);

                _mailer.Send($"Detected changes on {task.Url}\r\n{diff.InsertedCount} +, {diff.DeletedCount} -",
                             new [] { new MailAttachment($"{DateTime.Now:dd-MM-yyyy_HH-mm-ss}_{task.UrlDomain.Replace("www", "").Replace(".", "")}.diff", diff.DiffTextWithStats) });
            }
            else
            {
                _stateContainer.UpdateTime(task);
            }
        }
Exemple #4
0
        public bool Matches(MonitoringTask task, string data)
        {
            bool found = _state.TryGetValue(task.Id, out var it);

            if (!found)
            {
                Set(task, data);
                return(true);
            }

            return(it.Data == data);
        }
Exemple #5
0
 public void UpdateTime(MonitoringTask task)
 {
     Set(task, Get(task).Data);
 }
Exemple #6
0
        public void Set(MonitoringTask task, string data)
        {
            _state[task.Id] = WebsiteState.CreateNew(data);

            SaveState();
        }
Exemple #7
0
 public WebsiteState Get(MonitoringTask task)
 {
     return(_state[task.Id]);
 }
Exemple #8
0
 public static ProcessErrorStatus Success(MonitoringTask task) => new ProcessErrorStatus(null, task);
Exemple #9
0
 public static ProcessErrorStatus Fail(Exception error, MonitoringTask task) => new ProcessErrorStatus(error, task);
Exemple #10
0
 private ProcessErrorStatus(Exception error, MonitoringTask task)
 {
     Error = error;
     Task  = task;
 }