-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
118 lines (101 loc) · 3.26 KB
/
main.ts
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
import fs from "node:fs/promises";
import * as cheerio from "cheerio";
const defaultHeaders = {
accept:
"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
"accept-language": "pt-BR,pt;q=0.9,en-US;q=0.8,en;q=0.7",
"cache-control": "max-age=0",
"content-type": "application/x-www-form-urlencoded",
"Referrer-Policy": "strict-origin-when-cross-origin",
};
const semestersHtml = await fetch(
"https://www.vestibulinhoetec.com.br/demanda/",
{
headers: {
...defaultHeaders,
Referer: "https://www.vestibulinhoetec.com.br/",
},
}
).then((response) => response.text());
const $semestersPage = cheerio.load(semestersHtml);
const semesters = $semestersPage("[name=ano-sem] option")
.toArray()
.map((e) => ({ value: e.attribs.value, label: $semestersPage(e).text() }))
.filter((semester) => semester.value)
// Take few semesters for speed
.slice(0, 5);
const unitsBySemester: Record<string, { label: string; value: string }[]> = {};
for (const semester of semesters) {
console.log("Carregando o semestre " + semester.label);
const unitsHtml = await fetch(
"https://www.vestibulinhoetec.com.br/demanda/demanda.asp",
{
headers: {
...defaultHeaders,
Referer: "https://www.vestibulinhoetec.com.br/demanda/",
},
body: "ano-sem=" + semester.value,
method: "POST",
}
).then((response) => response.text());
const $unitsPage = cheerio.load(unitsHtml);
const units = $unitsPage("#CodEtec option")
.toArray()
.map((e) => ({ value: e.attribs.value, label: $unitsPage(e).text() }))
.filter((unit) => unit.value);
unitsBySemester[semester.value] = units;
}
const foundDemands: {
semester: { label: string; value: string };
unit: { label: string; value: string };
course: string;
period: string;
subscriberCount: string;
vacancies: string;
demand: string;
}[] = [];
for (const [semester, units] of Object.entries(unitsBySemester)) {
for (const unit of units) {
const demandsHtml = await fetch(
"https://www.vestibulinhoetec.com.br/demanda/demanda.asp",
{
headers: {
...defaultHeaders,
Referer: "https://www.vestibulinhoetec.com.br/demanda/demanda.asp",
},
body:
"ano-sem=" +
semester +
"&CodEtec=" +
unit.value +
"&V_REQCodEtec=Selecione a Etec/Extensão de Etec",
method: "POST",
}
).then((response) => response.text());
const $demandsPage = cheerio.load(demandsHtml);
const parsedDemands = $demandsPage("table tbody tr:has(td)")
.toArray()
.map((row) => {
const [course, period, subscriberCount, vacancies, demand] =
$demandsPage(row)
.find("td")
.toArray()
.map((cell) => $demandsPage(cell).text());
return {
course,
period,
subscriberCount,
vacancies,
demand,
};
});
parsedDemands.forEach((parsedDemand) =>
foundDemands.push({
semester: semesters.find(({ value }) => value === semester)!,
unit,
...parsedDemand,
})
);
await fs.writeFile("dump.json", JSON.stringify(foundDemands, null, 2));
}
}