Ejemplo n.º 1
0
        private static void ParseCourseInfo(string htmlResponse, ref Disciplinas resposta, string curso)
        {
            HtmlDocument htmlDoc = new HtmlDocument();

            htmlDoc.LoadHtml(htmlResponse);

            Dictionary <string, List <Disciplina> > response = new Dictionary <string, List <Disciplina> >();

            HtmlNodeCollection numberPeriodosNode = htmlDoc.DocumentNode.SelectNodes("//*[@class='tableTitle']");

            List <string> periods = new List <string>();

            if (numberPeriodosNode != null)
            {
                foreach (HtmlNode node in numberPeriodosNode)
                {
                    string   texto         = Remover.ReplaceMultipleSpacesByPipe(Remover.RemoveSeparators(node.InnerText));
                    string[] splittedTexto = texto.Split('|');
                    if (splittedTexto.Length == 3)
                    {
                        if (splittedTexto[1].Length == 10)
                        {
                            periods.Add(splittedTexto[1]);
                        }
                        if (splittedTexto[1].Length == 11)
                        {
                            periods.Add(splittedTexto[1]);
                        }
                    }
                }
            }

            HtmlNodeCollection periodosNode = htmlDoc.DocumentNode.SelectNodes("//*[@class='table'][1]//table//table//tr");

            if (periodosNode != null)
            {
                List <Disciplina> disciplinas = new List <Disciplina>();
                Disciplina        disciplina  = new Disciplina();
                List <Dia>        dias        = new List <Dia>();
                Dia dia     = new Dia();
                int periodo = 0;
                foreach (HtmlNode node in periodosNode)
                {
                    string texto = Remover.ReplaceMultipleSpacesByPipe(Remover.RemoveSeparators(node.InnerText));

                    if (texto.Contains("Professor"))
                    {
                        if (!String.IsNullOrWhiteSpace(disciplina.Código))
                        {
                            disciplina.Dias = dias;
                            disciplinas.Add(disciplina);

                            response.Add(periods[periodo], disciplinas);

                            periodo++;
                            dias        = new List <Dia>();
                            disciplina  = new Disciplina();
                            disciplinas = new List <Disciplina>();
                        }
                        continue;
                    }
                    string[] splittedText = texto.Split('|');

                    if (String.IsNullOrWhiteSpace(splittedText[1]) && dias.Count != 0)
                    {
                        dia = new Dia
                        {
                            DiaDaSemana = splittedText[4],
                            Horário     = splittedText[5]
                        };

                        if (!String.IsNullOrWhiteSpace(splittedText[6]))
                        {
                            dia.SegundoProfessor = splittedText[6];
                        }
                        dias.Add(dia);
                        dia = new Dia();
                    }

                    if (!String.IsNullOrWhiteSpace(splittedText[1]) && dias.Count != 0)
                    {
                        disciplina.Dias = dias;
                        dias            = new List <Dia>();

                        disciplinas.Add(disciplina);
                        disciplina = new Disciplina();
                    }

                    if (splittedText.Length == 8 && !String.IsNullOrWhiteSpace(splittedText[1]))
                    {
                        disciplina.Código    = splittedText[1];
                        disciplina.Número    = splittedText[2];
                        disciplina.Nome      = splittedText[3];
                        disciplina.Professor = splittedText[6];

                        dia.DiaDaSemana = splittedText[4];
                        dia.Horário     = splittedText[5];

                        dias.Add(dia);
                        dia = new Dia();
                        continue;
                    }
                    if (splittedText.Length == 9 && !String.IsNullOrWhiteSpace(splittedText[1]))
                    {
                        disciplina.Código    = splittedText[1];
                        disciplina.Número    = splittedText[2];
                        disciplina.Nome      = splittedText[3] + " " + splittedText[4];
                        disciplina.Professor = splittedText[7];

                        dia.DiaDaSemana = splittedText[5];
                        dia.Horário     = splittedText[6];

                        dias.Add(dia);
                        dia = new Dia();
                        continue;
                    }
                }

                if (dias.Count != 0)
                {
                    disciplina.Dias = dias;
                    disciplinas.Add(disciplina);
                    response.Add(periods[periodo], disciplinas);
                }
            }

            HtmlNodeCollection ComplementaresNode = htmlDoc.DocumentNode.SelectNodes("//*[@class='table'][2]//table//table//tr");

            if (ComplementaresNode != null)
            {
                List <Disciplina> disciplinas = new List <Disciplina>();
                Disciplina        disciplina  = new Disciplina();
                List <Dia>        dias        = new List <Dia>();
                Dia dia = new Dia();

                foreach (HtmlNode node in ComplementaresNode)
                {
                    string texto = Remover.ReplaceMultipleSpacesByPipe(Remover.RemoveSeparators(node.InnerText));

                    if (texto.Contains("Professor"))
                    {
                        continue;
                    }
                    string[] splittedText = texto.Split('|');

                    if (String.IsNullOrWhiteSpace(splittedText[1]) && dias.Count != 0)
                    {
                        dia = new Dia
                        {
                            DiaDaSemana = splittedText[4],
                            Horário     = splittedText[5]
                        };

                        if (!String.IsNullOrWhiteSpace(splittedText[6]))
                        {
                            dia.SegundoProfessor = splittedText[6];
                        }
                        dias.Add(dia);
                        dia = new Dia();
                    }

                    if (!String.IsNullOrWhiteSpace(splittedText[1]) && dias.Count != 0)
                    {
                        disciplina.Dias = dias;
                        dias            = new List <Dia>();

                        disciplinas.Add(disciplina);
                        disciplina = new Disciplina();
                    }

                    if (splittedText.Length == 8 && !String.IsNullOrWhiteSpace(splittedText[1]))
                    {
                        disciplina.Código    = splittedText[1];
                        disciplina.Número    = splittedText[2];
                        disciplina.Nome      = splittedText[3];
                        disciplina.Professor = splittedText[6];

                        dia.DiaDaSemana = splittedText[4];
                        dia.Horário     = splittedText[5];

                        dias.Add(dia);
                        dia = new Dia();
                        continue;
                    }
                    if (splittedText.Length == 9 && !String.IsNullOrWhiteSpace(splittedText[1]))
                    {
                        disciplina.Código    = splittedText[1];
                        disciplina.Número    = splittedText[2];
                        disciplina.Nome      = splittedText[3] + " " + splittedText[4];
                        disciplina.Professor = splittedText[7];

                        dia.DiaDaSemana = splittedText[5];
                        dia.Horário     = splittedText[6];

                        dias.Add(dia);
                        dia = new Dia();
                        continue;
                    }
                }

                if (dias.Count != 0)
                {
                    disciplina.Dias = dias;
                    disciplinas.Add(disciplina);
                }

                response.Add("Complementares", disciplinas);
            }

            resposta.Cursos.Add(curso, response);
            Result data = new Result
            {
                Curso       = curso,
                Disciplinas = response
            };

            MongoHandler.SaveOnMongo(data);
        }