-
Notifications
You must be signed in to change notification settings - Fork 2
/
bot.js
154 lines (104 loc) · 4.96 KB
/
bot.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
require('dotenv').config();
const chalk = require('chalk');
const puppeteer = require('puppeteer-extra');
const StealthPlugin = require('puppeteer-extra-plugin-stealth');
puppeteer.use(StealthPlugin());
puppeteer.use(require('puppeteer-extra-plugin-anonymize-ua')());
const get_user_data = async(username, password, link) => {
console.log(chalk.yellowBright('API GET method...'));
const browser = await puppeteer.launch({
headless: true,
args: ['--no-sandbox']
});
const page = await browser.newPage();
console.log(chalk.yellow('Browser launched...'));
await page.goto(link);
await page.waitForSelector('input[type=text]');
await page.waitForSelector('#id_39');
console.log('Selector appeared');
console.log(chalk.greenBright('On pronote...'));
try {
await login(page, username, password);
} catch (error) {
console.log('error during loging');
throw error;
}
console.log('logged in');
//const average = await get_user_average(page, username);
const average=20;
await page.waitForSelector('#id_107id_64');
await page.click('#id_107id_64');
await page.waitFor(300);
const averages = {};
averages['Moyennes Matières Elève'] = await get_user_grades(page, username);
await browser.close();
const data = {...average, ...averages };
return data;
};
const login = async(page, username, password) => {
await page.type('input[type=text]', username);
await page.type('input[type=password]', password);
await page.click('#id_39');
console.log(chalk.greenBright(`Logged in with ${username} account...`));
}
module.exports = login;
const get_user_average = async(page, username) => {
console.log(chalk.yellowBright('Starting new process...'));
await page.waitForSelector('#id_107id_64');
await page.click('#id_107id_64');
await page.waitFor(300);
const user_average_element = await page.$x("/html/body/div[4]/div[1]/div[2]/table/tbody/tr/td[1]/div/div/div[2]/div[1]/div[2]/div/div/div[1]/span/span");
let user_average = await page.evaluate(el => el.textContent, user_average_element[0]);
const class_average_element = await page.$x('/html/body/div[4]/div[1]/div[2]/table/tbody/tr/td[1]/div/div/div[2]/div[1]/div[2]/div/div/div[2]/span/span');
let class_average = await page.evaluate(el => el.textContent, class_average_element[0]);
let user_average_parsed = user_average.substring(0, user_average.length - 4);
let class_average_parsed = class_average.substring(0, user_average.length - 4);
let average_parsed_object = {};
average_parsed_object['Moyenne Générale Eleve'] = user_average_parsed;
average_parsed_object['Moyenne Génrale Classe'] = class_average_parsed;
console.log(chalk.yellow('End of process.'));
return average_parsed_object;
}
const get_user_grades = async(page, username) => {
console.log(chalk.yellowBright('Starting new process...'));
await page.waitForXPath('/html/body/div[4]/div[1]/div[2]/table/tbody/tr/td[1]/div/div/div[2]/div[1]/div[1]/div[2]/div/div/table/tbody/tr[8]/td[2]/div/div/div/div/div/div[2]');
const table_xpath = await page.$x('/html/body/div[4]/div[1]/div[2]/table/tbody/tr/td[1]/div/div/div[2]/div[1]/div[1]/div[2]/div/div/table');
const number_of_rows = await page.evaluate(table => table.rows.length, table_xpath[0]);
let subject_xpath = '';
let subject = '';
let subjects = [];
let position = [];
for (let i = 1; i <= number_of_rows; i++) {
subject_xpath = await page.$x(`/html/body/div[4]/div[1]/div[2]/table/tbody/tr/td[1]/div/div/div[2]/div[1]/div[1]/div[2]/div/div/table/tbody/tr[${i}]/td[2]/div/div/div/div/div/div[2]`);
subject = await page.evaluate(el => el.textContent, subject_xpath[0]);
if (subject.includes('/') || subject.includes("Aujourd'hui") || subject.includes("Hier")) {
continue;
} else {
subjects.push(subject);
position.push(i);
i++;
}
}
let subject_average_grade_xpath = '';
let subject_average_grade = '';
let subject_average_grades = [];
for (let i = 1; i <= number_of_rows; i++) {
subject_average_grade_xpath = await page.$x(`/html/body/div[4]/div[1]/div[2]/table/tbody/tr/td[1]/div/div/div[2]/div[1]/div[1]/div[2]/div/div/table/tbody/tr[${i}]/td[2]/div/div/div/div/div/div[1]`);
try {
subject_average_grade = await page.evaluate(el => el.textContent, subject_average_grade_xpath[0]);
} catch (e) {
console.log(e);
continue;
}
if (position.includes(i)) {
subject_average_grades.push(subject_average_grade);
} else {
continue;
}
}
const subject_average_grades_object = {};
subjects.forEach((key, i) => subject_average_grades_object[key] = subject_average_grades[i]);
console.log(chalk.yellow('End of process.'));
return subject_average_grades_object;
}
module.exports = get_user_data;