/*Registra a consulta no banco*/
        private static void RegistrarConsulta(HtmlDocument htmlDocument, FontePesquisa fp)
        {
            RService.Log("(RegistrarConsulta) " + Name + ": Verificando registros... " + "at {0}", Path.GetTempPath() + Name + ".txt");
            try
            {
                string htmlTratado = Regex.Replace(htmlDocument.DocumentNode.InnerHtml, @"\s*\n*", "");
                string valorRegex  = string.Format(@"{0}", fp.Regex);

                MatchCollection mt       = StringHandle.GetMatches(htmlTratado, valorRegex);
                string          conteudo = mt != null ? mt[0].Value : string.Empty;

                FontePesquisaRobot fpr = new FontePesquisaRobot(fp.Id);
                fpr.DataHoraPesquisa = DateTime.Now;
                fpr.Conteudo         = conteudo;

                /*caso tenha alteração no site*/
                if (!conteudo.Equals(fp.UltimoConteudo))
                {
                    if (fp.UltimoConteudo != null)
                    {
                        RService.Log("(RegistrarConsulta) " + Name + ": Houve alteração na fonte de pesquisa, gerado Warning... at {0}", Path.GetTempPath() + Name + ".txt");
                        fpr.Status = (byte)StatusAlerta.Warning;
                        FontePesquisaRobotController.Criar(fpr);

                        NumAlteracoes++;
                    }

                    fp.UltimoConteudo = conteudo;
                    FontePesquisaController.Atualizar(fp);
                }
                /*caso não tenha alteração no site*/
                else
                {
                    RService.Log("(RegistrarConsulta) " + Name + ": Não houve alteração na fonte de pesquisa... at {0}", Path.GetTempPath() + Name + ".txt");
                }
            }
            catch (Exception e)
            {
                RService.Log("Exception (RegistrarConsulta)" + Name + ":" + e.Message + " / " + e.StackTrace + " / " + e.InnerException + " at {0}", Path.GetTempPath() + Name + ".txt");
            }
        }
Пример #2
0
        private static void RegistrarConsulta(FontePesquisa fp)
        {
            RService.Log("(RegistrarConsulta) " + Name + ": Verificando registros... " + "at {0}", Path.GetTempPath() + Name + ".txt");
            try
            {
                //Analisa a Regex para definir qual caminho seguir, o que exige mais de um clique ou o que ele consegue capturar o conteúdo de primeira
                content = SitesComMaisProcedimentos(fp);

                if (fp.Regex.Contains(';'))
                {
                    string[] element = fp.Regex.Split(';');
                    int      last    = element.Count() - 1;

                    web.Navigate().GoToUrl(fp.Link);
                    Thread.Sleep(3000);

                    for (int i = 0; i < last; i++)
                    {
                        if (element[i].Contains("frame"))
                        {
                            web.SwitchTo().Frame(web.FindElement(By.XPath(element[i])));
                        }
                        else
                        {
                            web.FindElement(By.XPath(element[i])).Click();
                        }
                    }

                    content = web.FindElement(By.XPath(element[last])).GetAttribute("innerHTML").ToString();
                }
                else
                {
                    web.Navigate().GoToUrl(fp.Link);
                    Thread.Sleep(3000);

                    content = web.FindElement(By.XPath(fp.Regex)).GetAttribute("innerHTML").ToString();
                }
                if (content.Contains("�") || content.Contains("\""))
                {
                    content = content.Replace("�", "");
                    content = content.Replace("\"", "");
                }

                fpr = new FontePesquisaRobot(fp.Id);
                fpr.DataHoraPesquisa = DateTime.Now;
                fpr.Conteudo         = content;

                //Usando o valor obtido no if-else anterior, é registrada a alteração no banco
                if (!content.Equals(fp.UltimoConteudo))
                {
                    if (fp.UltimoConteudo != null)
                    {
                        RService.Log("(RegistrarConsulta) " + Name + ": Houve alteração na fonte de pesquisa, gerado Warning... at {0}", Path.GetTempPath() + Name + ".txt");

                        fpr.Status = (byte)StatusAlerta.Warning;
                        FontePesquisaRobotController.Criar(fpr);
                        NumAlteracoes++;
                    }

                    fp.UltimoConteudo = content;
                    FontePesquisaController.Atualizar(fp);
                }
                else
                {
                    RService.Log("(RegistrarConsulta) " + Name + ": Não houve alteração na fonte de pesquisa at {0}", Path.GetTempPath() + Name + ".txt");
                }
            }
            catch (Exception e)
            {
                RService.Log("Exception (RegistrarConsulta) " + Name + ": " + e.Message + " / " + e.StackTrace + " / " + e.InnerException + " at {0}", Path.GetTempPath() + Name + ".txt");
            }
        }