summaryrefslogtreecommitdiff
path: root/alarm/node_modules/pronote-api/bin/fetch.js
blob: d9e88e7b0f13fbeefeedc735531cb958571219a8 (plain)
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
#!/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);
    }
});