1
0
Fork 0

Update worker with better name management and event compare

This commit is contained in:
Ethanell 2020-12-12 21:52:55 +01:00
parent 22f17d88f2
commit 9940b11cf4
2 changed files with 44 additions and 46 deletions

View file

@ -2,12 +2,14 @@ const https = require("https");
const config = require("worker_threads").workerData;
const models = require("../models");
const reg_event = /(?:(?:BEGIN:VEVENT\nDTSTAMP:(?:[A-Z0-9]*?)\nDTSTART:([A-Z0-9]*?)\nDTEND:([A-Z0-9]*?)\nSUMMARY: {0,}([a-zéèàA-Z0-9-. \, \\/ô]*?)\nLOCATION:([a-zA-Zéèà0-9-. \,\\]*?)\nDESCRIPTION:(?:\\n){0,}(LP(?:[ a-zA-Z0-9]*))\\n((?:(?:[A-Z]*) (?:[A-Z]*)(?: (?:[A-Z]*)){0,}\\n){0,})(?:.*?)\nEND:VEVENT)|(?:BEGIN:VEVENT\nDTSTAMP:(?:[A-Z0-9]*?)\nDTSTART:([A-Z0-9]*?)\nDTEND:([A-Z0-9]*?)\nSUMMARY: {0,}((?:S(?:[A-Z0-9-]*)|M(?:[A-Z0-9-]*)(?:\/M(?:[A-Z0-9-]*)){0,}|Conférence)[a-zéèàA-Z0-9-. \, \\/]*?)\nLOCATION:([a-zA-Zéèà0-9-. \,\\]*?)\nDESCRIPTION:(?:\\n){0,}((?:(?:G[0-9]S[0-9]|S[0-9]|ASPE)\\n){0,})((?:(?:[A-Z]*) (?:[A-Z]*)(?: (?:[A-Z]*)){0,}\\n){0,})(?:.*?)\nEND:VEVENT))/gs;
const reg_event = /(?:(?:BEGIN:VEVENT\nDTSTAMP:(?:[A-Z0-9]*?)\nDTSTART:([A-Z0-9]*?)\nDTEND:([A-Z0-9]*?)\nSUMMARY: {0,}([a-zéèàA-Z0-9-. \, \\/ô]*?)\nLOCATION:([a-zA-Zéèà0-9-. \,\\]*?)\nDESCRIPTION:(?:\\n){0,}((?:(?:LP(?:[ a-zA-Z0-9\\]*?))\\n){1,})((?:(?:[A-Z]*) (?:[A-Z]*)(?: (?:[A-Z]*)){0,}\\n){0,})(?:.*?)\nEND:VEVENT)|(?:BEGIN:VEVENT\nDTSTAMP:(?:[A-Z0-9]*?)\nDTSTART:([A-Z0-9]*?)\nDTEND:([A-Z0-9]*?)\nSUMMARY: {0,}((?:S(?:[A-Z0-9-]*)|M(?:[A-Z0-9-]*)(?:\/M(?:[A-Z0-9-]*)){0,}|Conférence)[a-zéèàA-Z0-9-. \, \\/]*?)\nLOCATION:([a-zA-Zéèà0-9-. \,\\]*?)\nDESCRIPTION:(?:\\n){0,}((?:(?:G[0-9]S[0-9]|S[0-9]|ASPE)\\n){0,})((?:(?:[A-Z]*) (?:[A-Z]*)(?: (?:[A-Z]*)){0,}\\n){0,})(?:.*?)\nEND:VEVENT))/gs;
const reg_location = /((?:[SH0-9][0-9]{2})|(?:(?:Préfa |Amphi)[0-9]))/g;
const reg_teachers = /^(?:((?:[a-zA-Z]*[ -]{0,}){0,}) ([a-zA-Z]*))$/m;
const reg_date = /([0-9]{4})([0-9]{2})([0-9]{2})T([0-9]{2})([0-9]{2})([0-9]{2})Z/;
const reg_classe = /(?:(LP)[ -]{0,}(.*)|(?:(G[0-9])(S[0-9])))/;
const reg_group = /(?:(LP)[ -]{0,}(.*)|(?:(G[0-9])(S[0-9])))/;
const base_url = config["edt"];
const capitalizeFirstLetter = (string) => string.charAt(0).toUpperCase() + string.slice(1).toLowerCase();
function fetchEvents(days = 1, TS_Start = new Date()) {
return new Promise((resolve, reject) => {
@ -32,8 +34,8 @@ function fetchEvents(days = 1, TS_Start = new Date()) {
reg_event.lastIndex++;
}
let event = [];
let promotion = [];
let classe = [];
let semester = [];
let group = [];
/*
m = [
FullMatch,
@ -47,21 +49,30 @@ function fetchEvents(days = 1, TS_Start = new Date()) {
*/
if (m[1] !== undefined) {
// LPXXXX
classe.push(m[5]);
} else {
m.splice(1, 6);
// GXSX | SX | ASPE
let csplit = m[5].split("\\n");
csplit.pop();
csplit.forEach(e => {
if (e === "ASPE") promotion.push(e);
if (/^S[0-9]$/.test(e)) promotion.push(e);
if (/^G[0-9]S[0-9]$/.test(e)) classe.push(e);
});
}
let csplit = m[5].split("\\n");
csplit.pop();
csplit.forEach(e => {
if (e === "ASPE") {
semester.push(e);
}else if(/^S[0-9]$/.test(e)) {
semester.push(e);
}else{
let groupSplit = reg_group.exec(e);
if (groupSplit[1] === 'LP') {
group.push([groupSplit[2], groupSplit[1]]);
} else {
group.push([groupSplit[3], groupSplit[4]]);
}
}
});
event["title"] = m[3];
event["promotion"] = promotion;
event["class"] = classe;
event["semesters"] = semester;
event["groups"] = group;
/*
Date
*/
@ -83,7 +94,7 @@ function fetchEvents(days = 1, TS_Start = new Date()) {
/*
Teachers
*/
event['teachers'] = []
event['teachers'] = [];
let fullTeachers = m[6].split('\\n');
fullTeachers.forEach(e => {
if(e !== ""){
@ -128,11 +139,11 @@ function compare(a, b) {
}
function compareGroups(list1, list2) {
return compare(list1, list2.map(g => [g.number, g.Semester.name, g.Semester.year]));
return compare(list1.map(g => g[0] + " " + g[1]), list2.map(g => g.number + " " + g.Semester.name));
}
function compareTeachers(list1, list2) {
return compare(list1, list2.map(t => t.lastName.toUpperCase() + " " + t.firstName.toUpperCase()));
return compare(list1.map(t => t[0].toUpperCase() + " " + t[1].toUpperCase()), list2.map(t => t.lastName.toUpperCase() + " " + t.firstName.toUpperCase()));
}
function compareSemesters(list1, list2) {
@ -147,13 +158,14 @@ async function updateDatabase() {
include: {model: models.Semester, required: true}
}, models.User, models.Semester]
})) {
if (!events.find(e => (e.title === event.name && e.startDate.getTime() === event.startDate.getTime() &&
let ev = events.find(e => (e.title === event.name && e.startDate.getTime() === event.startDate.getTime() &&
e.endDate.getTime() === event.endDate.getTime() && e.locations.join(", ") === event.locations &&
compareGroups(e.class, event.Groups) && compareTeachers(e.teachers, event.Users) &&
compareSemesters(e.semesters, event.Semesters))))
compareGroups(e.groups, event.Groups) && compareTeachers(e.teachers, event.Users) &&
compareSemesters(e.semesters, event.Semesters)));
if (!ev)
await event.destroy();
else
delete events[events.indexOf(event)];
events = events.filter(e => e !== ev);
}
for (let event of events) {
@ -166,12 +178,12 @@ async function updateDatabase() {
let teachers = [];
for (let teacher of event.teachers) {
let t = await models.User.findOne({where: {permissions: 2, lastName: teacher[0], firstName: teacher[1]}});
let t = await models.User.findOne({where: {permissions: 2, lastName: teacher[0].toUpperCase(), firstName: capitalizeFirstLetter(teacher[1])}});
if (!t)
t = await models.User.create({
email: teacher[1].toLowerCase().replace(" ", "-") + "." + teacher[0].toLowerCase().replace(" ", "-") + "@univ-lyon1.fr",
firstName: teacher[1],
lastName: teacher[0],
email: teacher[1].toLowerCase().replaceAll(" ", "-") + "." + teacher[0].toLowerCase().replaceAll(" ", "-") + "@univ-lyon1.fr",
firstName: capitalizeFirstLetter(teacher[1]),
lastName: teacher[0].toUpperCase(),
permissions: 2,
passwordHash: Math.round((Math.pow(36, 12 + 1) - Math.random() * Math.pow(36, 12))).toString(36).slice(1)
});
@ -180,7 +192,7 @@ async function updateDatabase() {
await e.addUsers(teachers);
let semesters = [];
for (let semester of event.promotion) {
for (let semester of event.semesters) {
let s = await models.Semester.findOne({where: {name: semester, year: event.startDate.getFullYear()}});
if (!s)
s = await models.Semester.create({
@ -192,32 +204,18 @@ async function updateDatabase() {
await e.addSemesters(semesters);
let groups = [];
for (let group of event.class) {
let rGroup = reg_classe.exec(group);
let semesterName;
let groupName;
if (rGroup[1] !== undefined) {
// LP
semesterName = 'LP';
groupName = rGroup[2];
} else {
// GXSX
semesterName = rGroup[4];
groupName = rGroup[3];
}
let s = await models.Semester.findOne({where: {name: semesterName, year: event.startDate.getFullYear()}});
for (let group of event.groups) {
let s = await models.Semester.findOne({where: {name: group[1], year: event.startDate.getFullYear()}});
if (!s)
s = await models.Semester.create({
name: semesterName,
name: group[1],
year: event.startDate.getFullYear()
});
let g = await models.Group.findOne({where: {number: groupName, SemesterId: s.id}});
let g = await models.Group.findOne({where: {number: group[0], SemesterId: s.id}});
if (!g)
g = await models.Group.create({
number: groupName,
number: group[0],
SemesterId: s.id
});
groups.push(g);

View file

@ -17,7 +17,7 @@ const mailClient = new SMTPClient(process.env.NODE_ENV === "test" ? {} : require
/**
* Render all Sass to css
*/
css = sass.renderSync({
let css = sass.renderSync({
file: "sass/style.sass",
includePaths: ["sass/"],
outputStyle: "compressed"