-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSisuDataQuery.cs
More file actions
384 lines (348 loc) · 15.2 KB
/
SisuDataQuery.cs
File metadata and controls
384 lines (348 loc) · 15.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using HtmlAgilityPack;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
#pragma warning disable
class SisuDataQuery
{
public static List<Institution> listInstitutions = new List<Institution>();
public static List<Course> listIesCourses = new List<Course>();
public static Course course;
public static CourseWeights courseWeights;
public static MinGrades minGrades;
const int columnWidth = 35;
public class Institution
{
public Institution(string coIes, string sgIes, string noIes, string noMunicipio)
{
this.institutionCode = coIes;
this.institutionSg = sgIes;
this.institutionName = noIes;
this.institutionCity = noMunicipio;
}
public string? institutionCode { get; set; }
public string? institutionSg { get; set; }
public string? institutionName { get; set; }
public string? institutionCity { get; set; }
public override string ToString()
{
return $"| {institutionCode,-10} | {institutionSg,-15} | {institutionName,-columnWidth} | {institutionCity,-25} |";
}
}
public class Course
{
public Course(string co_oferta, string co_curso, string no_curso, string no_grau, string no_turno, string no_campus, string no_municipio_campus)
{
this.offerCode = co_oferta;
this.courseCode = co_curso;
this.courseName = no_curso;
this.courseDegree = no_grau;
this.courseShift = no_turno;
this.campusName = no_campus;
this.campusCity = no_municipio_campus;
}
public string? offerCode { get; set; }
public string? courseCode { get; set; }
public string? courseName { get; set; }
public string? courseDegree { get; set; }
public string? courseShift { get; set; }
public string? campusName { get; set; }
public string? campusCity { get; set; }
public override string ToString()
{
return $"| {courseCode,-10} | {courseName,-columnWidth} | {courseDegree,-20} | {courseShift,-15} | {campusName,-30} | {campusCity,-25} |";
}
}
public class CourseWeights
{
public CourseWeights(string cnPeso, string chPeso, string lngPeso, string mtmPeso, string redPeso)
{
this.natSciWeight = cnPeso;
this.hmnSciWeight = chPeso;
this.langSciWeight = lngPeso;
this.mtmWeight = mtmPeso;
this.disWeight = redPeso;
}
public string? natSciWeight { get; set; }
public string? hmnSciWeight { get; set; }
public string? langSciWeight { get; set; }
public string? mtmWeight { get; set; }
public string? disWeight { get; set; }
public override string ToString()
{
string header = $"| {"Peso CN",-15} | {"Peso CH",-15} | {"Peso Linguagens",-15} | {"Peso Matemática",-15} | {"Peso Redação",-15} |\n";
string values = $"| {natSciWeight,-15} | {hmnSciWeight,-15} | {langSciWeight,-15} | {mtmWeight,-15} | {disWeight,-15} |";
return header + new string('-', header.Length - 1) + "\n" + values;
}
}
public class MinGrades
{
public MinGrades(string cnMin, string chMin, string lngMin, string mtmMin, string redMin)
{
this.natSciMin = cnMin;
this.hmnSciMin = chMin;
this.langSciMin = lngMin;
this.mtmMin = mtmMin;
this.disMin = redMin;
}
public string? natSciMin { get; set; }
public string? hmnSciMin { get; set; }
public string? langSciMin { get; set; }
public string? mtmMin { get; set; }
public string? disMin { get; set; }
public override string ToString()
{
string header = $"| {"Nota Mínima CN",-20} | {"Nota Mínima CH",-20} | {"Nota Mínima Ling",-20} | {"Nota Mínima Mat",-20} | {"Nota Mínima Red",-20} |\n";
string values = $"| {natSciMin,-20} | {hmnSciMin,-20} | {langSciMin,-20} | {mtmMin,-20} | {disMin,-20} |";
return header + new string('-', header.Length - 1) + "\n" + values;
}
}
static void Main(string[] args)
{
Console.WriteLine("=============== SISU DATA QUERY ===============\n");
var options = new ChromeOptions();
options.AddArgument("--headless");
IWebDriver driver = new ChromeDriver(options);
GetIes(driver);
bool exit = false;
while (!exit)
{
Console.WriteLine("\nEscolha uma opção:");
Console.WriteLine("1. Procurar por instituição");
Console.WriteLine("2. Sair");
Console.Write("Opção: ");
string choice = Console.ReadLine();
switch (choice)
{
case "1":
SearchInstitution(driver);
break;
case "2":
exit = true;
break;
default:
Console.WriteLine("Opção inválida. Tente novamente.");
break;
}
}
driver.Quit();
}
public static void SearchInstitution(IWebDriver driver)
{
Console.WriteLine("\n**INSIRA A SIGLA DA INSTITUIÇÃO DESEJADA:");
string siglaIes = Console.ReadLine();
Institution selectedInstitution = null;
foreach (Institution i in listInstitutions)
{
if (siglaIes.ToUpper().Trim() == i.institutionSg)
{
selectedInstitution = i;
Console.WriteLine(new string('-', 120));
Console.WriteLine($"| {"Código",-10} | {"Sigla",-15} | {"Nome",-columnWidth} | {"Município",-25} |");
Console.WriteLine(new string('-', 120));
Console.WriteLine(i);
Console.WriteLine(new string('-', 120));
Console.WriteLine("\nProcurando cursos da instituição...\n");
getCourses(driver, i.institutionCode);
break;
}
}
if (selectedInstitution != null)
{
SearchCourse(driver);
}
else
{
Console.WriteLine("Instituição não encontrada.");
}
}
public static void SearchCourse(IWebDriver driver)
{
Console.WriteLine("\n**INSIRA O CÓDIGO DO CURSO:");
string courseCode = Console.ReadLine();
course = null;
foreach (Course c in listIesCourses)
{
if (courseCode.Trim() == c.courseCode)
{
course = c;
Console.WriteLine(new string('-', 160));
Console.WriteLine($"| {"Código",-10} | {"Nome",-columnWidth} | {"Grau",-20} | {"Turno",-15} | {"Campus",-30} | {"Município",-25} |");
Console.WriteLine(new string('-', 160));
Console.WriteLine(c);
Console.WriteLine(new string('-', 160));
Console.WriteLine("\nRecolhendo informações do curso...\n");
getCourseWeights(driver, course.offerCode);
getMinGrades(driver, course.offerCode);
break;
}
}
if (course == null)
{
Console.WriteLine("Curso não encontrado.");
}
}
public static void GetIes(IWebDriver driver)
{
driver.Navigate().GoToUrl("https://sisu-api.sisu.mec.gov.br/api/v1/oferta/instituicoes/uf");
System.Threading.Thread.Sleep(2000);
var htmlContent = driver.PageSource;
var htmlDocument = new HtmlDocument();
htmlDocument.LoadHtml(htmlContent);
var preNode = htmlDocument.DocumentNode.SelectSingleNode("//pre");
if (preNode != null)
{
var jsonContent = preNode.InnerText;
var data = JsonConvert.DeserializeObject<Dictionary<string, object>>(jsonContent);
foreach (var state in data)
{
var stateValue = state.Value?.ToString();
if (stateValue != null)
{
var institutions = JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(stateValue);
foreach (var institution in institutions)
{
var coIes = institution.GetValueOrDefault("co_ies");
var sgIes = institution.GetValueOrDefault("sg_ies");
var noIes = institution.GetValueOrDefault("no_ies");
var noMunicipio = institution.GetValueOrDefault("no_municipio");
if (coIes != null && noIes != null && sgIes != null && noMunicipio != null)
{
Institution _institution = new Institution(coIes, sgIes, noIes, noMunicipio);
listInstitutions.Add(_institution);
}
}
}
}
}
else
{
Console.WriteLine("Não foi encontrado o elemento <pre> no HTML.");
}
}
public static void getCourses(IWebDriver driver, string iesCode)
{
listIesCourses.Clear();
driver.Navigate().GoToUrl($"https://sisu-api.sisu.mec.gov.br/api/v1/oferta/instituicao/{iesCode}");
System.Threading.Thread.Sleep(2000);
var htmlContent = driver.PageSource;
var htmlDocument = new HtmlDocument();
htmlDocument.LoadHtml(htmlContent);
var preNode = htmlDocument.DocumentNode.SelectSingleNode("//pre");
if (preNode != null)
{
var jsonContent = preNode.InnerText;
var cleanedJsonContent = CleanJson(jsonContent);
if (!string.IsNullOrEmpty(cleanedJsonContent))
{
var data = JsonConvert.DeserializeObject<Dictionary<string, object>>(cleanedJsonContent);
data.Remove("search_rule");
Console.WriteLine(new string('-', 160));
Console.WriteLine($"| {"Código",-10} | {"Nome",-columnWidth} | {"Grau",-20} | {"Turno",-15} | {"Campus",-30} | {"Município",-25} |");
Console.WriteLine(new string('-', 160));
for (int i = 0; i < data.Count; i++)
{
var courseKey = i.ToString();
if (data.ContainsKey(courseKey))
{
var courseValue = data[courseKey]?.ToString();
if (courseValue != null)
{
var courseDetails = JsonConvert.DeserializeObject<Dictionary<string, string>>(courseValue);
var coOferta = courseDetails.GetValueOrDefault("co_oferta");
var coCurso = courseDetails.GetValueOrDefault("co_curso");
var noCurso = courseDetails.GetValueOrDefault("no_curso");
var noGrau = courseDetails.GetValueOrDefault("no_grau");
var noTurno = courseDetails.GetValueOrDefault("no_turno");
var noCampus = courseDetails.GetValueOrDefault("no_campus");
var noMunicipioCampus = courseDetails.GetValueOrDefault("no_municipio_campus");
if (coCurso != null && noCurso != null && noTurno != null && noCampus != null && noMunicipioCampus != null)
{
Course _course = new Course(coOferta, coCurso, noCurso, noGrau, noTurno, noCampus, noMunicipioCampus);
listIesCourses.Add(_course);
Console.WriteLine(_course);
}
}
}
}
Console.WriteLine(new string('-', 160));
}
}
}
public static void getCourseWeights(IWebDriver driver, string offerCode)
{
driver.Navigate().GoToUrl($"https://sisu-api.sisu.mec.gov.br/api/v1/oferta/{offerCode}/modalidades");
System.Threading.Thread.Sleep(2000);
var htmlContent = driver.PageSource;
var htmlDocument = new HtmlDocument();
htmlDocument.LoadHtml(htmlContent);
var preNode = htmlDocument.DocumentNode.SelectSingleNode("//pre");
if (preNode != null)
{
var jsonContent = preNode.InnerText;
var cleanedJsonContent = CleanJson(jsonContent);
if (!string.IsNullOrEmpty(cleanedJsonContent))
{
var data = JsonConvert.DeserializeObject<Dictionary<string, object>>(cleanedJsonContent);
if (data.ContainsKey("oferta"))
{
var oferta = data["oferta"] as JObject;
if (oferta != null)
{
var nuPesoCn = oferta["nu_peso_cn"]?.ToString();
var nuPesoCh = oferta["nu_peso_ch"]?.ToString();
var nuPesoL = oferta["nu_peso_l"]?.ToString();
var nuPesoM = oferta["nu_peso_m"]?.ToString();
var nuPesoR = oferta["nu_peso_r"]?.ToString();
courseWeights = new CourseWeights(nuPesoCn, nuPesoCh, nuPesoL, nuPesoM, nuPesoR);
Console.WriteLine("Pesos:");
Console.WriteLine(courseWeights);
}
}
}
}
}
public static void getMinGrades(IWebDriver driver, string offerCode)
{
driver.Navigate().GoToUrl($"https://sisu-api.sisu.mec.gov.br/api/v1/oferta/{offerCode}/modalidades");
System.Threading.Thread.Sleep(2000);
var htmlContent = driver.PageSource;
var htmlDocument = new HtmlDocument();
htmlDocument.LoadHtml(htmlContent);
var preNode = htmlDocument.DocumentNode.SelectSingleNode("//pre");
if (preNode != null)
{
var jsonContent = preNode.InnerText;
var cleanedJsonContent = CleanJson(jsonContent);
if (!string.IsNullOrEmpty(cleanedJsonContent))
{
var data = JsonConvert.DeserializeObject<Dictionary<string, object>>(cleanedJsonContent);
if (data.ContainsKey("oferta"))
{
var oferta = data["oferta"] as JObject;
if (oferta != null)
{
var nMinCn = oferta["nu_nmin_cn"]?.ToString();
var nMinCh = oferta["nu_nmin_ch"]?.ToString();
var nMinL = oferta["nu_nmin_l"]?.ToString();
var nMinM = oferta["nu_nmin_m"]?.ToString();
var nMinR = oferta["nu_nmin_r"]?.ToString();
minGrades = new MinGrades(nMinCn, nMinCh, nMinL, nMinM, nMinR);
Console.WriteLine("\nNotas Mínimas:");
Console.WriteLine(minGrades);
}
}
}
}
}
public static string CleanJson(string jsonContent)
{
var trimmedJson = jsonContent.Trim();
if (trimmedJson.EndsWith(","))
{
trimmedJson = trimmedJson.Substring(0, trimmedJson.Length - 1);
}
return trimmedJson;
}
}