예제 #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());
        }
예제 #2
0
 public WebsiteState GetOrDefault(MonitoringTask task)
 {
     if (_state.ContainsKey(task.Id))
     {
         return(_state[task.Id]);
     }
     return(new WebsiteState(null, DateTime.MinValue));
 }
예제 #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);
            }
        }
예제 #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);
        }
예제 #5
0
 public void UpdateTime(MonitoringTask task)
 {
     Set(task, Get(task).Data);
 }
예제 #6
0
        public void Set(MonitoringTask task, string data)
        {
            _state[task.Id] = WebsiteState.CreateNew(data);

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