summaryrefslogtreecommitdiff
path: root/alarm/node_modules/pronote-api/src/data
diff options
context:
space:
mode:
authorMinteck <contact@minteck.org>2022-10-18 08:59:09 +0200
committerMinteck <contact@minteck.org>2022-10-18 08:59:09 +0200
commit2c4ae43e688a9873e86211ea0e7aeb9ba770dd77 (patch)
tree17848d95522dab25d3cdeb9c4a6450e2a234861f /alarm/node_modules/pronote-api/src/data
parent108525534c28013cfe1897c30e4565f9893f3766 (diff)
downloadpluralconnect-2c4ae43e688a9873e86211ea0e7aeb9ba770dd77.tar.gz
pluralconnect-2c4ae43e688a9873e86211ea0e7aeb9ba770dd77.tar.bz2
pluralconnect-2c4ae43e688a9873e86211ea0e7aeb9ba770dd77.zip
Update
Diffstat (limited to 'alarm/node_modules/pronote-api/src/data')
-rw-r--r--alarm/node_modules/pronote-api/src/data/dates.js82
-rw-r--r--alarm/node_modules/pronote-api/src/data/files.js14
-rw-r--r--alarm/node_modules/pronote-api/src/data/html.js16
-rw-r--r--alarm/node_modules/pronote-api/src/data/id.js33
-rw-r--r--alarm/node_modules/pronote-api/src/data/objects.js44
-rw-r--r--alarm/node_modules/pronote-api/src/data/periods.js24
-rw-r--r--alarm/node_modules/pronote-api/src/data/types.js87
7 files changed, 300 insertions, 0 deletions
diff --git a/alarm/node_modules/pronote-api/src/data/dates.js b/alarm/node_modules/pronote-api/src/data/dates.js
new file mode 100644
index 0000000..52377ba
--- /dev/null
+++ b/alarm/node_modules/pronote-api/src/data/dates.js
@@ -0,0 +1,82 @@
+function toPronoteWeek(session, date)
+{
+ const firstWeek = toUTCWeek(session.params.firstDay);
+ const week = toUTCWeek(date);
+
+ if (week >= firstWeek) {
+ return week - firstWeek + 1;
+ }
+
+ return 52 - (firstWeek - week) + 1; // Trust me this works
+}
+
+function toUTCWeek(date)
+{
+ const firstDay = new Date((new Date()).getFullYear(), 0, 1);
+ return Math.ceil((((date - firstDay) / 86400000) + firstDay.getDay() + 1) / 7);
+}
+
+function toPronoteDay(session, date)
+{
+ return Math.ceil((date - session.params.firstDay) / 86400000);
+}
+
+function fromPronoteDay(session, day)
+{
+ const date = new Date(session.params.firstDay.getTime());
+ date.setDate(date.getDate() + day - 1);
+
+ return date;
+}
+
+function toPronoteDate(date)
+{
+ return `${date.getDate()}/${date.getMonth() + 1}/${date.getFullYear()} ` +
+ `${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`;
+}
+
+function fromPronoteHours(hours)
+{
+ return ~~hours[0] + ~~hours.substring(2) / 60;
+}
+
+function parseDate(string)
+{
+ const date = new Date();
+ const split = string.split(' ');
+
+ const day = split[0].split('/');
+
+ date.setFullYear(~~day[2], (~~day[1]) - 1, ~~day[0]);
+ date.setMilliseconds(0);
+
+ if (split.length > 1)
+ {
+ const time = split[1].split(':');
+
+ date.setHours(~~time[0]);
+ date.setMinutes(~~time[1]);
+ date.setSeconds(~~time[2]);
+ }
+ else
+ {
+ date.setHours(0);
+ date.setMinutes(0);
+ date.setSeconds(0);
+ }
+
+ return date;
+}
+
+module.exports = {
+ toPronoteWeek,
+ toUTCWeek,
+
+ toPronoteDay,
+ fromPronoteDay,
+
+ toPronoteDate,
+ parseDate,
+
+ fromPronoteHours
+};
diff --git a/alarm/node_modules/pronote-api/src/data/files.js b/alarm/node_modules/pronote-api/src/data/files.js
new file mode 100644
index 0000000..aea0e8e
--- /dev/null
+++ b/alarm/node_modules/pronote-api/src/data/files.js
@@ -0,0 +1,14 @@
+const { toPronote } = require('./objects');
+const { cipher } = require('../cipher');
+
+const EXTERNAL_FILES_FOLDER = 'FichiersExternes/';
+
+function getFileURL(session, { id, name, type })
+{
+ const fileID = cipher(session, JSON.stringify(toPronote({ id, type })));
+ const fileName = encodeURIComponent(encodeURIComponent(name)); // *Clown emoji*
+
+ return session.server + EXTERNAL_FILES_FOLDER + fileID + '/' + fileName + '?Session=' + session.id;
+}
+
+module.exports = { getFileURL };
diff --git a/alarm/node_modules/pronote-api/src/data/html.js b/alarm/node_modules/pronote-api/src/data/html.js
new file mode 100644
index 0000000..33a4f0d
--- /dev/null
+++ b/alarm/node_modules/pronote-api/src/data/html.js
@@ -0,0 +1,16 @@
+const { stripHtml } = require('string-strip-html');
+
+function fromHTML(text)
+{
+ if (!text) {
+ if (text === undefined) {
+ return null;
+ }
+
+ return text;
+ }
+
+ return stripHtml(text).result;
+}
+
+module.exports = fromHTML;
diff --git a/alarm/node_modules/pronote-api/src/data/id.js b/alarm/node_modules/pronote-api/src/data/id.js
new file mode 100644
index 0000000..2c3c2d3
--- /dev/null
+++ b/alarm/node_modules/pronote-api/src/data/id.js
@@ -0,0 +1,33 @@
+const { h64 } = require('xxhashjs');
+
+function withId(obj, fields, extraData)
+{
+ const result = {};
+ for (const field of fields) {
+ result[field] = obj[field];
+ }
+
+ if (extraData) {
+ result.__extraData = extraData;
+ }
+
+ return {
+ id: h64(JSON.stringify(result), 0).toString(16),
+ ...obj
+ };
+}
+
+function checkDuplicates(objs)
+{
+ for (const obj of objs) {
+ const duplicates = objs.filter(o => o.id === obj.id);
+
+ if (duplicates.length > 1) {
+ duplicates.forEach((d, i) => d.id = d.id.substring(0, d.id.length - 1) + i);
+ }
+ }
+
+ return objs;
+}
+
+module.exports = { withId, checkDuplicates };
diff --git a/alarm/node_modules/pronote-api/src/data/objects.js b/alarm/node_modules/pronote-api/src/data/objects.js
new file mode 100644
index 0000000..5cc985e
--- /dev/null
+++ b/alarm/node_modules/pronote-api/src/data/objects.js
@@ -0,0 +1,44 @@
+function fromPronote({ N, L, G, ...obj } = {}, fn = null, gName = 'type') {
+ const result = {};
+
+ if (typeof fn === 'string') {
+ gName = fn;
+ fn = null;
+ }
+
+ if (N) {
+ result.id = N;
+ }
+ if (L) {
+ result.name = L;
+ }
+ if (G !== undefined && gName) {
+ result[gName] = G;
+ }
+
+ return {
+ ...result,
+ ...(fn ? fn(G && gName ? obj : { G, ...obj }) : {})
+ };
+}
+
+function toPronote({ id, name, type } = {}) {
+ const result = {};
+
+ if (id) {
+ result.N = id;
+ }
+ if (name) {
+ result.L = name;
+ }
+ if (type) {
+ result.G = type;
+ }
+
+ return result;
+}
+
+module.exports = {
+ fromPronote,
+ toPronote
+};
diff --git a/alarm/node_modules/pronote-api/src/data/periods.js b/alarm/node_modules/pronote-api/src/data/periods.js
new file mode 100644
index 0000000..a13039d
--- /dev/null
+++ b/alarm/node_modules/pronote-api/src/data/periods.js
@@ -0,0 +1,24 @@
+function getPeriodBy(session, period, type = null)
+{
+ const Type = ['trimester', 'semester', 'year'];
+ const periods = session.params.periods;
+ if (!type || Type.indexOf(type) === -1) {
+ type = 'trimester'
+ }
+ if (!period) {
+ const now = Date.now();
+ return periods.find(p => now >= p.from && now <= p.to && p.kind === type) || periods[5];
+ } else if (typeof period === 'string') {
+ for (const p of periods) {
+ if (p.name === period) {
+ return p;
+ }
+ }
+ }
+
+ return period;
+}
+
+module.exports = {
+ getPeriodBy
+};
diff --git a/alarm/node_modules/pronote-api/src/data/types.js b/alarm/node_modules/pronote-api/src/data/types.js
new file mode 100644
index 0000000..a77e35f
--- /dev/null
+++ b/alarm/node_modules/pronote-api/src/data/types.js
@@ -0,0 +1,87 @@
+const { fromPronote } = require('./objects');
+
+function parseDate(str)
+{
+ const date = new Date();
+ const split = str.split(' ');
+
+ const day = split[0].split('/');
+
+ date.setFullYear(~~day[2], (~~day[1]) - 1, ~~day[0]);
+ date.setMilliseconds(0);
+
+ if (split.length > 1) {
+ const time = split[1].split(':');
+
+ date.setHours(~~time[0]);
+ date.setMinutes(~~time[1]);
+ date.setSeconds(~~time[2]);
+ } else {
+ date.setHours(0);
+ date.setMinutes(0);
+ date.setSeconds(0);
+ }
+
+ return date;
+}
+
+function parseRange(str)
+{
+ const content = str.substring(1, str.length - 1).split(',');
+ const result = [];
+
+ for (const val of content) {
+ if (val.includes('..')) {
+ const index = val.indexOf('..');
+ for (let i = ~~val.substring(0, index); i <= ~~val.substring(index + 2); i++) {
+ result.push(i);
+ }
+ } else {
+ result.push(~~val);
+ }
+ }
+
+ return result;
+}
+
+function parse({ _T: type, V: value } = {}, f = null, g = 'type')
+{
+ if (!value) {
+ if (value === undefined) {
+ return null;
+ }
+
+ return value;
+ }
+
+ switch (type) {
+ case 7: // Date
+ return parseDate(value);
+ case 8: // ? (Range)
+ case 11: // ? (Range)
+ case 26: // ? (Range)
+ return parseRange(value);
+ case 10: // Mark / Number
+ value = value.replace('|', '-');
+
+ if (value.indexOf(',') !== -1) {
+ return parseFloat(value.replace(',', '.'));
+ }
+
+ return ~~value;
+ case 21: // HTML content
+ case 23: // URL
+ case 24: // Object (includes Array)
+ case 25: // Resource
+ default:
+ if (f !== false && value.map) {
+ return value.map(o => fromPronote(o, f, g));
+ } else if (f !== false && (value.N || value.L)) {
+ return fromPronote(value, f, g);
+ }
+
+ return value;
+ }
+}
+
+module.exports = parse;