-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
163 lines (140 loc) · 5.08 KB
/
index.js
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
const puppeteer = require("puppeteer");
const fs = require("fs");
const ocr = require("./ocr");
require("dotenv").config();
const nilaiDosen = process.env.NILAI_DOSEN || 7;
const pausUsername = process.env.PAUS_USERNAME;
const pausPassword = process.env.PAUS_PASSWORD;
puppeteer
.launch({ headless: false })
.then(async (browser) => {
// Initialize
const page = await browser.newPage();
page.setViewport({
width: 1200,
height: 800,
});
await page
.goto("https://paus.unpad.ac.id/oauth/sign-in", {
waitUntil: ["networkidle2", "domcontentloaded"],
})
.then(() => console.log("Menunggu login..."));
async function fillSignInForm() {
// Insert username
await page.evaluate(
() => (document.querySelector("input[name = username]").value = "")
);
const username = await page.$("input[name = username]");
await username.type(pausUsername);
// Insert password
const pass = await page.$("input[name = password]");
await pass.type(pausPassword);
}
async function captchaSolve() {
// Prepare captcha image
const IMAGE_SELECTOR = ".captcha-img > img";
let imageHref = await page.evaluate((sel) => {
return document.querySelector(sel).getAttribute("src");
}, IMAGE_SELECTOR);
const imgPage = await browser.newPage();
var viewSource = await imgPage.goto(imageHref);
fs.writeFile("a.jpeg", await viewSource.buffer(), function (err) {
if (err) return console.log(err);
});
imgPage.close();
const captchaText = await ocr.captchaOCR();
const captcha = await page.$("input[name = captcha]");
await captcha.type(captchaText);
// Click sign in button
const button = await page
.$(".ui.form .right.aligned.field .button:last-child")
.catch((error) => console.error(error));
await button.evaluate((b) => b.click());
}
async function interceptSignIn() {
page
.waitForSelector(".ui.error.message", { visible: true })
.then(async () => {
await fillSignInForm();
await captchaSolve();
});
}
await fillSignInForm();
// If credentials not yet configured, skip captcha autosolver
if (ocr.isConfigured()) {
page.on("load", interceptSignIn);
await captchaSolve();
}
// Redirecting to permission page & accept it
page.waitForSelector(".account-name", { visible: true }).then(async () => {
console.log("Berhasil sign in");
page.off("load", interceptSignIn);
await page
.goto("https://students.unpad.ac.id/pacis/mhs_home", {
waitUntil: ["networkidle2", "domcontentloaded"],
})
.then(() => console.log("Berhasil masuk ke halaman beranda"));
// Click approve button
const button = await page
.$(".btn-approve")
.catch((error) => console.error(error));
await button
.evaluate((b) => b.click())
.then(() => console.log("Izin telah diterima."));
});
// If tagihan prompt still exists
page.waitForSelector("#next", { visible: true }).then(async () => {
await page.$eval(
"#next",
(b) => b.click()
);
}).catch ((error) => {
console.log("Lompat ke transkrip nilai page");
});
// Redirecting to transkrip nilai page
page.waitForSelector("#menu", { visible: true }).then(async () => {
const urlHalamanTranskrip =
"https://students.unpad.ac.id/pacis/akademik/transkrip_nilai";
await page
.goto(urlHalamanTranskrip, {
waitUntil: ["networkidle2", "domcontentloaded"],
})
.then(() => console.log("Berhasil ke halaman transkrip."));
const links = await page.$$eval(".tombol", (link) =>
link.map((a) => a.href)
);
for (let link of links) {
const newTab = await browser.newPage();
await newTab.goto(link);
await newTab
.waitForSelector("#formdata")
.then("Memasuki halaman kuisoner");
// Fill form
for (let i = 0; i < 9; i++) {
await newTab
.click(`#pilih_${174 + i}${nilaiDosen}`)
.then(() =>
console.log(
`--> Memilih nilai ${nilaiDosen} untuk pertanyaan ${i + 1}`
)
);
}
await newTab.$eval("#simpan", (b) => b.click());
setTimeout(async () => {
await newTab.$eval(
".ui-button.ui-widget.ui-state-default.ui-corner-all.ui-button-text-only",
(b) => b.click()
);
console.log("Berhasil mengisi kuisoner");
newTab.close();
}, 2000); // Here is disadvantage. The program have to wait button confirmation apperead, but i can't solve it cause questionnaire is already filled. Haha
}
page.reload({ waitUntil: ["networkidle0", "domcontentloaded"] });
console.log(`====================================`);
console.log(`${links.length} kuisoner telah diisi`);
console.log(`Have a good day! -fyfirman`);
});
})
.catch((error) => {
console.error(error);
});