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
const debug = require("debug")("edupage:log");
const error = require("debug")("edupage:error");
const RawData = require("../lib/RawData");
const Edupage = require("./Edupage");
const {FatalError, ParseError} = require("./exceptions");
debug.log = console.log.bind(console);
/**
* @typedef {import("../lib/RawData").RawDataObject} RawDataObject
*/
class ASC extends RawData {
/**
* Creates an instance of ASC.
* @param {RawDataObject} [data={}] Raw data to initialize the instance with.
* @param {Edupage} [edupage=null] Edupage instance to use.
* @memberof ASC
*/
constructor(data = {}, edupage = null) {
super(data);
/**
* Edupage instance associated to this object.
* @type {Edupage | string}
*/
this.edupage = edupage;
/**
* Userstring of the currently logged in user.
* @type {string}
*/
this.loggedUser = data.req_props.loggedUser;
/**
* Rights of the currently logged in user.
* @type {any[]}
*/
this.loggedUserRights = data.req_props.loggedUserRights;
/**
* Timezone for the school.
* @type {string}
*/
this.timezone = data.req_props.timezone;
/**
* Weekend days for the school.
* @type {number[]}
*/
this.weekendDays = data.req_props.weekendDays;
/**
* Edupage server.
* @type {string}
* @example "edupage61"
*/
this.server = data.server;
/**
* Full name of the school.
* @type {string}
*/
this.schoolName = data.school_name;
/**
* Language code of the currently logged in user.
* @type {string}
*/
this.lang = data.lang;
/**
* Country code of the school.
* @type {string}
*/
this.schoolCountry = data.school_country;
/**
* Turnover of the school in format "MM-DD"
* @type {string}
* @example "08-01"
*/
this.schoolyearTurnover = data.schoolyear_turnover;
/**
* Secure hash used for API calls
* @type {string}
* @example "94c3f4d3"
*/
this.gsecHash = data.gsechash;
/**
* Id used for API calls
* @type {string}
*/
this.gpid = null;
/**
* List of currently available gpid ids.
* @type {string[]}
*/
this.gpids = [];
/**
* First day of the week (0 = Sunday, 1 = Monday, ...).
* @type {number}
*/
this.firstDayOfWeek = data.firstDayOfWeek;
}
/**
* Parses the HTML page and returns raw ASC data.
* @static
* @param {string} html HTML page to parse.
* @return {RawDataObject} Parsed data.
* @memberof ASC
*/
static parse(html) {
const data = {};
const matches = [...html.matchAll(/ASC\.([a-zA-Z0-9_$]+)\s?=\s?([\s\S]+?);/g)];
if(!matches.length) return FatalError.throw(new ParseError("Failed to parse ASC data from html"), {html});
for(const [match, key, value] of matches) {
if(value.startsWith("function")) continue;
try {
data[key] = JSON.parse(value);
} catch(e) {
return FatalError.throw(new ParseError("Failed to parse JSON from ASC html"), {html, matches, match, key, value, e});
}
}
return data;
}
}
module.exports = ASC;