Example #1
0
        public void CheckResponse(Site site, SiteResponse lastResponse)
        {
            try
            {
                if (lastResponse == null)
                {
                    _logger.LogInfo(site.SiteName + " - Checking changes (first time).");
                }
                else
                {
                    _logger.LogInfo(site.SiteName + " - Checking changes.");
                }

                string       html = SiteHtmlUtil.GetSiteHtml(site);
                SiteResponse currentResponse;
                string       html_to_text = SiteHtmlUtil.StripHTMLAdvanced(html);
                //string currentResponseHash = HashUtil.CalculateMD5Hash(html);
                string currentResponseHash = HashUtil.CalculateMD5Hash(html_to_text);

                //if (lastResponse == null || !lastResponse.Contents.Equals(html))
                if (lastResponse == null || !lastResponse.Hash.Equals(currentResponseHash))
                {
                    NotificationHelper notificationHelper = new NotificationHelper();
                    currentResponse            = new SiteResponse();
                    currentResponse.Site       = site;
                    currentResponse.CheckDate  = DateTime.Now;
                    currentResponse.StatusCode = "200";
                    currentResponse.Contents   = html;
                    //currentResponse.HtmlContents = html;
                    //currentResponse.Contents = current_response_html_to_text;
                    currentResponse.Hash = currentResponseHash;
                    currentResponse.Diff = notificationHelper.GetDiff(currentResponse, lastResponse);
                    site.SiteResponses.Add(currentResponse);
                    _siteResponseRepository.Save(currentResponse);
                    _siteRepository.SaveOrUpdate(site);

                    if (lastResponse != null)
                    {
                        string message = site.SiteName + " has changes - Sending notification email.";
                        _logger.LogInfo(message);
                        Notification notification = notificationHelper.CreateNotification(site, currentResponse, lastResponse);
                        _notificationsRepository.SaveOrUpdate(notification);
                        _logger.LogSiteChanged(notification, site, message, currentResponse.Diff);
                    }
                }
                else
                {
                    _logger.LogInfo(site.SiteName + " doesn't have changes.");
                    currentResponse            = lastResponse;
                    currentResponse.CheckDate  = DateTime.Now;
                    currentResponse.StatusCode = "200";
                    _siteResponseRepository.SaveOrUpdate(currentResponse);
                }
            }
            catch (Exception ex)
            {
                LogErrorGettingSiteResponse(site, ex);
            }
        }