Last active
November 25, 2020 01:05
-
-
Save ClementGre/181b84d72f19f37f504320f4664667b4 to your computer and use it in GitHub Desktop.
Serveur web nodeJs qui permet de se connecter à son compte pronote et de voir toutes les données disponibles. Montre les fonctionnalités de pronote-api de Litarvan.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const http = require('http'); | |
const pronote = require('pronote-api'); | |
const app = http.createServer(function (request, response) { | |
console.log('----- RECEIVED A REQUEST -----'); | |
var body = ""; | |
var isBodyGenerated = false; | |
var isResponseEnded = false; | |
body += '<html style="scroll-behavior: smooth;">' | |
body += 'Log you into to Pronote :<br><br>'; | |
body += '<form method="post">'; | |
body += '<input type="text" placeholder="pronote url" id="url" name="url"><br>'; | |
body += '<input type="text" placeholder="username" id="username" name="username"><br>'; | |
body += '<input type="password" placeholder="password" id="password" name="password"><br>'; | |
body += '<button type="submit">Login</button>'; | |
body += '</form> <br><br><br><br><br>'; | |
if(request.method === "POST"){ | |
request.on("data", function(chunk){ | |
var data = chunk.toString().split('&'); | |
var url = undefined; | |
var username = undefined; | |
var password = undefined; | |
data.forEach((postItem) => { | |
try{ | |
if(postItem.startsWith("url=")){ | |
url = decodeURIComponent(postItem.split("=")[1]); | |
}else if(postItem.startsWith("username=")){ | |
username = decodeURIComponent(postItem.split("=")[1]); | |
}else if(postItem.startsWith("password=")){ | |
password = decodeURIComponent(postItem.split("=")[1]); | |
} | |
}catch(e){ | |
//console.error(e); | |
body += "<h2 color='red'>An error occured, please retry.</h2><br>"; | |
} | |
}); | |
if(url != undefined && username != undefined && password != undefined){ | |
if(url != '' && username != '' && password != ''){ | |
login(url, username, password, (session, pronoteData) => { | |
if(pronoteData != undefined && session != undefined){ | |
//body += getPeriodBy(null, null, null)+""; | |
body += '<table><td style="word-wrap: break-word; max-width: 33vw; border-left: 1px solid gray; vertical-align: top;">' | |
body += getSumary() + getPronoteFieldString(pronoteData.timetable, 0, "Timetable"); | |
body += getSumary() + getPronoteFieldString(pronoteData.marks, 0, "Marks"); | |
body += getSumary() + getPronoteFieldString(pronoteData.contents, 0, "Contents"); | |
body += getSumary() + getPronoteFieldString(pronoteData.evaluations, 0, "Evaluations"); | |
body += getSumary() + getPronoteFieldString(pronoteData.absences, 0, "Absences") + getSumary(); | |
body += '</td><td style="word-wrap: break-word; max-width: 33vw; border-left: 1px solid gray; vertical-align: top;">' | |
body += getSumary() + getPronoteFieldString(pronoteData.homeworks, 0, "Homeworks"); | |
body += getSumary() + getPronoteFieldString(pronoteData.infos, 0, "Infos"); | |
body += getSumary() + getPronoteFieldString(pronoteData.menu, 0, "Menu"); | |
body += getSumary() + getPronoteFieldString(session.user, 0, "User"); | |
body += getSumary() + getPronoteFieldString(session.params, 0, "Params") + getSumary(); | |
body += '</td><td style="word-wrap: break-word; max-width: 33vw; border-left: 1px solid gray; vertical-align: top;">' | |
body += getSumary() + getPronoteFieldString(session, 0, "Session") + getSumary(); | |
body += '</td></table>' | |
}else{ | |
body += '<h2 style="color: red;">Login fails. Try to re-login.</h2><br>'; | |
} | |
isBodyGenerated = true; | |
if(isResponseEnded){ | |
response.writeHead(200, {'Content-Type': 'text/html'}); | |
response.end(body + "</html>"); | |
console.log("Response ended by callback") | |
} | |
}).catch(err => { | |
if(err.code === pronote.errors.WRONG_CREDENTIALS.code){ | |
body += '<h2 style="color: red;">The url, username or password is not matching an user</h2><br>'; | |
}else{ | |
console.log("The connection with the pronote server isn't working."); | |
console.error(err); | |
body += '<h2 style="color: red;">The connection with the pronote server is not working.</h2><br>'; | |
} | |
isBodyGenerated = true; | |
if(isResponseEnded){ | |
response.writeHead(200, {'Content-Type': 'text/html'}); | |
response.end(body + "</html>"); | |
console.log("Response ended by callback error") | |
} | |
}); | |
}else{ | |
body += '<h2 style="color: red;">You must fill all the fields.</h2><br>'; | |
isBodyGenerated = true; | |
} | |
}else{ | |
body += '<h2 style="color: red;">You must fill all the fields.</h2><br>'; | |
isBodyGenerated = true; | |
} | |
}); | |
request.on('end', function () { | |
isResponseEnded = true; | |
if(isBodyGenerated){ | |
response.writeHead(200, {'Content-Type': 'text/html'}); | |
response.end(body + "</html>"); | |
console.log("Response ended by end event") | |
} | |
}); | |
}else{ | |
response.writeHead(200, {'Content-Type': 'text/html'}); | |
response.end(body); | |
} | |
}); | |
app.listen(8080); | |
function getPronoteFieldString(data, level, name){ | |
if(data == undefined || data == null) return '<p style="margin: 0; margin-left: ' + ((level+1)*15) + ';"><b>undefined:</b></p>'; | |
if(typeof(data) == 'object' && !(data instanceof Date)){ | |
var string = '<h' + (level+1) + ' style="margin: 0; font-weight: 700; text-decoration: underline; margin-left: ' + (level*15) + ';">' + name + "</h" + (level+1) + ">"; | |
if(level == 0) string = '<br><h' + (level+1) + ' id="section-' + name + '" style="margin: 0; color: red;">' + name + "</h" + (level+1) + ">"; | |
for(const [key, value] of Object.entries(data)){ | |
string += getPronoteFieldString(value, level+1, key); | |
} | |
return string; | |
}else{ | |
return '<p style="margin: 0; margin-left: ' + ((level+1)*15) + ';"><b>' + name + ":</b> " + data + "</p>"; | |
} | |
} | |
function getSumary(){ | |
return `<hr> | |
<a href="#section-Timetable">Timetable</a>   | |
<a href="#section-Marks"> Marks</a>   | |
<a href="#section-Contents">Contents</a>   | |
<a href="#section-Evaluations">Evaluations</a>   | |
<a href="#section-Absences">Absences</a>   | |
<a href="#section-Homeworks">Homeworks</a>   | |
<a href="#section-Infos">Infos</a>   | |
<a href="#section-Menu">Menu</a>   | |
<a href="#section-User">User</a>   | |
<a href="#section-Params">Params</a>   | |
<a href="#section-Session">Session</a>   | |
<hr>` | |
} | |
async function login(url, username, password, callback){ | |
console.log("trying to login with username=" + username); | |
var session = await pronote.login(url, username, password/*, cas*/); | |
session.setKeepAlive(false); | |
var pronoteData = {}; | |
pronoteData.currentPeriod = session.user.tabsPeriods[0].defaultPeriod.name; | |
var fromDate = new Date(); | |
fromDate.setDate(fromDate.getDate() - 1); | |
var toDate = new Date(); | |
toDate.setDate(toDate.getDate() + 2); | |
pronoteData.timetable = await session.timetable(fromDate, toDate); | |
pronoteData.marks = await session.marks(pronoteData.currentPeriod); | |
pronoteData.contents = await session.contents(fromDate, toDate); | |
pronoteData.evaluations = await session.evaluations(pronoteData.currentPeriod); | |
pronoteData.absences = await session.absences(pronoteData.currentPeriod); | |
pronoteData.homeworks = await session.homeworks(fromDate, toDate); | |
pronoteData.infos = await session.infos(); | |
pronoteData.menu = await session.menu(fromDate, toDate); | |
callback(session, pronoteData); | |
return; | |
//console.log(session.user.name); // Affiche le nom de l'élève | |
//console.log(session.user.studentClass.name); // Affiche la classe de l'élève | |
const timetable = await session.timetable(); // Récupérer l'emploi du temps d'aujourd'hui | |
const marks = await session.marks(); // Récupérer les notes du trimestre | |
//console.log(`L'élève a ${timetable.length} cours aujourd'hui`); | |
//console.log(`et a pour l'instant une moyenne de ${marks.averages.student} ce trimestre.`); | |
// etc. les fonctions utilisables sont 'timetable', 'marks', 'contents', 'evaluations', 'absences', | |
// 'homeworks', 'infos', et 'menu', sans oublier les champs 'user' et 'params' qui regorgent d'informations. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment