-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
123 lines (107 loc) · 4.01 KB
/
app.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
const puppeteer = require("puppeteer-core");
const fs = require("fs").promises;
const path = require("path");
const login = require("./login.js");
const checkin = require("./checkin.js");
const question = require("./question.js");
const configs = require("./config.json");
const LOGIN_URL = "https://auth.1point3acres.com/login";
const CHECKIN_URL = "https://www.1point3acres.com/bbs/dsu_paulsign-sign.html";
// clear out screenshots before run.
async function run(config) {
try {
await fs
.readdir(path.join(__dirname, "./screenshots"))
.then(async (files) => {
for (const file of files) {
if(file === ".gitignore") continue;
await fs.unlink(
path.join(__dirname, "./screenshots", file)
);
}
});
} catch (err) {
console.log("can't remove previous screenshots.");
console.log(err);
}
const username = config.username;
const password = config.password;
const token = config.witToken;
console.log(`-------------working on user ${username}------------`);
// const browser = await puppeteer.launch();
const browser = await puppeteer.launch({
headless: true,
executablePath: "/usr/bin/chromium-browser",
args: [
"--no-sandbox",
"--disable-setuid-sandbox",
"--disable-web-security",
],
userDataDir: path.join(__dirname, './.userDataDir', username)
});
const page = await browser.newPage();
await page.setDefaultNavigationTimeout(0);
// page.on("console", (consoleObj) => console.log(consoleObj.text()));
// // read cookies and set cookies to page.
// try {
// const cookiesString = await fs.readFile(
// path.join(__dirname, `./.cookies/${username}`)
// );
// const cookies = JSON.parse(cookiesString);
// await page.setCookie(...cookies);
// } catch (err) {
// console.log("can't set cookie.", err);
// }
try {
// -------LOG IN--------
console.log("-------------go to login page------------");
await page.goto(LOGIN_URL);
await page.screenshot({
path: path.join(__dirname, "./screenshots/login-loaded.png"),
});
console.log("-------------now at login page------------");
if (page.url() === LOGIN_URL)
await login(page, token, username, password);
console.log("-------------logged in------------");
// -------LOG IN--------
// -------Check IN--------
console.log("-------------go to checkin page------------");
await page.goto(CHECKIN_URL);
console.log("-------------now at checkin page------------");
await checkin(page, token);
console.log("-------------checked in------------");
// -------Check IN--------
// -------Questionnaire--------
console.log("-------------go to questionnaire page------------");
await page.waitForSelector(
`a[onclick^="showWindow('pop','plugin.php?id=ahome_dayquestion:pop')"]`
);
try {
await page.click(
`a[onclick^="showWindow('pop','plugin.php?id=ahome_dayquestion:pop')"]`
);
await page.waitForSelector("#myform");
await page.screenshot({
path: path.join(
__dirname,
"./screenshots/daily-question-loaded.png"
),
});
console.log("-------------now at questionnaire page------------");
await question(page, token);
} catch (err) {
console.log("Already answered question!");
}
console.log("-------------question answered------------");
// -------Questionnaire--------
} catch (err) {
console.trace(err);
} finally {
await browser.close();
}
}
(async () => {
for (let cfg of configs) {
await run(cfg);
}
})();