Exemplo n.º 1
0
        public static Disciplinas GetAllClassesInfo()
        {
            string classUrl = "https://siga.ufrj.br/sira/gradeHoraria/{0}";

            Disciplinas response = new Disciplinas
            {
                Cursos = new Dictionary <string, Dictionary <string, List <Disciplina> > >()
            };

            foreach (string cursos in Courses.Keys)
            {
                string formatedUrl = string.Format(classUrl, Courses[cursos]);

                Result storedData = MongoHandler.IsOnMongo(cursos);
                if (storedData != null)
                {
                    response.Cursos.Add(cursos, storedData.Disciplinas);
                    continue;
                }

                HttpWebRequest webReq = (HttpWebRequest)HttpWebRequest.Create(formatedUrl);

                webReq.KeepAlive              = true;
                webReq.UserAgent              = "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36";
                webReq.Accept                 = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9";
                webReq.ContentType            = "text/html";
                webReq.Host                   = "siga.ufrj.br";
                webReq.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

                webReq.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate, br");
                webReq.Headers.Add(HttpRequestHeader.AcceptLanguage, "pt-BR,pt;q=0.9,en-US;q=0.8,en;q=0.7");

                string htmlResponse = String.Empty;

                HttpWebResponse htmlContent = (HttpWebResponse)webReq.GetResponse();

                using (StreamReader reader = new StreamReader(htmlContent.GetResponseStream(), Encoding.GetEncoding("iso-8859-1")))
                {
                    htmlResponse = reader.ReadToEnd();
                }

                htmlResponse = HttpUtility.HtmlDecode(htmlResponse);

                if (String.IsNullOrWhiteSpace(htmlResponse))
                {
                    response.Erro = "Não foi possível obter as disciplinas. Tente novamente!";
                    return(response);
                }

                ParseCourseInfo(htmlResponse, ref response, cursos);
            }

            if (response.Cursos.Count == 0)
            {
                response.Erro = "Curso não implementado.";
            }

            return(response);
        }