diff options
author | Minteck <contact@minteck.org> | 2023-02-23 19:34:56 +0100 |
---|---|---|
committer | Minteck <contact@minteck.org> | 2023-02-23 19:34:56 +0100 |
commit | 3d1cd02f27518f1a04374c7c8320cd5d82ede6e9 (patch) | |
tree | 75be5fba4368472fb11c8015aee026b2b9a71888 /includes/external/school/node_modules/pronote-api/bin/fetch.js | |
parent | 8cc1f13c17fa2fb5a4410542d39e650e02945634 (diff) | |
download | pluralconnect-3d1cd02f27518f1a04374c7c8320cd5d82ede6e9.tar.gz pluralconnect-3d1cd02f27518f1a04374c7c8320cd5d82ede6e9.tar.bz2 pluralconnect-3d1cd02f27518f1a04374c7c8320cd5d82ede6e9.zip |
Updated 40 files, added 37 files, deleted 1103 files and renamed 3905 files (automated)
Diffstat (limited to 'includes/external/school/node_modules/pronote-api/bin/fetch.js')
-rwxr-xr-x | includes/external/school/node_modules/pronote-api/bin/fetch.js | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/includes/external/school/node_modules/pronote-api/bin/fetch.js b/includes/external/school/node_modules/pronote-api/bin/fetch.js new file mode 100755 index 0000000..d9e88e7 --- /dev/null +++ b/includes/external/school/node_modules/pronote-api/bin/fetch.js @@ -0,0 +1,120 @@ +#!/usr/bin/env node + +/* eslint no-console: off */ + +const fs = require('fs').promises; +const pronote = require('..'); + +if (process.argv.length < 5) { + console.log('Syntax: pronote-fetch <URL> <username> <password> [cas(ex: none)] [AccountType (ex: Student)]'); + return; +} + +const [,, url, username, password, cas = 'none', accountType] = process.argv; + +async function fetch() +{ + let result; + switch (accountType) + { + case 'parent': + result = await parent(); + break; + default: + result = await student(); + break; + } + + await fs.writeFile('result.json', JSON.stringify(result, null, 4)); + + console.log('Wrote \'result.json\''); +} + +async function student() +{ + const session = await pronote.login(url, username, password, cas); + console.log(`Logged as student '${session.user.name}' (${session.user.studentClass.name})`); + + const { from, to } = getFetchDate(session); + + const timetable = await session.timetable(from, to); + const marks = await session.marks(); + const evaluations = await session.evaluations(); + const absences = await session.absences(); + const infos = await session.infos(); + const contents = await session.contents(from, to); + const homeworks = await session.homeworks(from, to); + const menu = await session.menu(from, to); + const files = await session.files(); + + return { + name: session.user.name, + studentClass: session.user.studentClass.name, + avatar: session.user.avatar, + + timetable, marks, evaluations, absences, + infos, contents, homeworks, menu, files + }; +} + +async function parent() +{ + const session = await pronote.loginParent(url, username, password, cas); + console.log(`Logged as parent '${session.user.name}' (${session.user.students.length} students)`); + + const { from, to } = getFetchDate(session); + + const students = []; + for (const student of session.user.students) { + console.log(`Fetching data of user '${student.name}' (${student.studentClass.name})`); + + const timetable = await session.timetable(student, from, to); + const marks = await session.marks(student); + const evaluations = await session.evaluations(student); + const absences = await session.absences(student); + const infos = await session.infos(student); + const contents = await session.contents(student, from, to); + const homeworks = await session.homeworks(student, from, to); + const menu = await session.menu(student, from, to); + const files = await session.files(student); + + students.push({ + name: student.name, + studentClass: student.studentClass.name, + avatar: student.avatar, + + timetable, marks, evaluations, absences, + infos, contents, homeworks, menu, files + }); + } + + return { + name: session.user.name, + students + }; +} + +function getFetchDate(session) +{ + let from = new Date(); + if (from < session.params.firstDay) { + from = session.params.firstDay; + } + + const to = new Date(from.getTime()); + to.setDate(to.getDate() + 15); + + return { from, to }; +} + +fetch().catch(err => { + if (err.code === pronote.errors.WRONG_CREDENTIALS.code) { + return console.error('Invalid credentials, did you chose the right CAS ?'); + } + + if (err.code !== undefined) { + console.error(`ERROR: [${err.code}] ${err.message}`); + } else { + console.error(err); + } +}); |