Something went wrong. Try again.
A project designed to automate the process of fetching result screenshots from the MGU result portal, sending them to students via email as…
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343let { chromium } = require("playwright");let fs = require('fs');let path = require('path');let chalk = require('chalk');let { saveData, checkQid, generateQid } = require('./database.js');
module.exports = {
fetchResult : async (student_id, exam_id) => {
let qid = generateQid(student_id, exam_id); let result = await checkQid(exam_id, qid);
if (result) { console.log("--- [fetchResult] --- Data already present in database. Skipping data fetch... \n"); return; } else { console.log(`--- [fetchResult] --- Fetching result for ${student_id}`); }
// launch a new browser instance let browser = await chromium.launch({ executablePath: process.env.CHROMIUM_PATH, headless: true }); let page = await browser.newPage(); await page.setViewportSize({ width: 1000, height: 850 }); // go to the result page await page.goto("https://dsdc.mgu.ac.in/exQpMgmt/index.php/public/ResultView_ctrl/"); await page.selectOption('select#exam_id', exam_id); await page.fill('#prn', student_id); await page.click('button#btnresult');
// wait for the result table to load try { await page.waitForSelector('div#mgu_btech_contentholder table:nth-child(2)', { visible: true, timeout: 10000 }); await page.waitForSelector('div#mgu_btech_contentholder table:nth-child(3)', { visible: true, timeout: 10000 }); console.log(chalk.greenBright(`--- [fetchResult] --- Result loaded for ${student_id} \n`)); } catch (error) { console.log(chalk.redBright(`--- [fetchResult] --- Result not found for ${student_id} \n`)); await browser.close(); return; }
// // wait for 2 seconds // await page.waitForTimeout(1000);
// // get the result table let studentDetails = await page.$('div#mgu_btech_contentholder table:nth-child(2)'); let resultTable = await page.$('div#mgu_btech_contentholder table:nth-child(3)'); // // get the result table html let studentDetailsHTML = await studentDetails.innerHTML(); let resultTableHTML = await resultTable.innerHTML(); // // create folder if it doesn't exist with the name as exam_id inside public/analytics if (fs.existsSync(path.join(__dirname, `../public/analytics/${student_id}`), { recursive: true })) { fs.rm(path.join(__dirname, `../public/analytics/${student_id}`), { recursive: true }); } fs.mkdirSync(path.join(__dirname, `../public/analytics/${student_id}`), { recursive: true }); // // write the result table html to a file fs.writeFileSync(path.join(__dirname, `../public/analytics/${student_id}/result.html`), resultTableHTML); fs.writeFileSync(path.join(__dirname, `../public/analytics/${student_id}/details.html`), studentDetailsHTML);
// take a screenshot of the result page await page.screenshot({ path: `public/screenshots/${student_id}.png`, });
// close the browser instance ASAP await browser.close();
return qid;
},
processResult : async (student_id, exam_id, qid) => {
// check if there is a folder with the name as student_id inside public/analytics // if not then skip data processing
if (!fs.existsSync(path.join(__dirname, `../public/analytics/${student_id}`))) { console.log(chalk.redBright(`--- [processResult] --- Result not found for ${student_id} \n`)); return; }
console.log(`--- [processResult] --- Processing result for ${student_id}`);
let allSubjects = []; let semester = {}; let flag = false;
// get table html from the file let html = fs.readFileSync(path.join(__dirname, `../public/analytics/${student_id}/result.html`), 'utf8'); // remove comments and blank lines from the html let htmlWithoutComments = html.replace(/<!--[\s\S]*?-->/g, ''); htmlWithoutComments = htmlWithoutComments.replace(/^\s*[\r\n]/gm, '');
// remove height="30" from the html // Because this pattern was causing descrepencieswhile searching for <td>...</td> (Semester Result) htmlWithoutComments = htmlWithoutComments.replace(/ height="30"/g, '');
// loop through the html, following the pattern related to <tr>...</tr> // This continues until SEMESTER RESULT is found in the first <td> of the <tr> // Starts from 3rd <tr> because the first 2 <tr> are not needed as they are the headers
for (let i = 3; flag == false; i++) {
let pattern = '<tbody>[\\s\\S]*?'; for (let j = 0; j < i-1; j++) { pattern = pattern + '<tr>[\\s\\S]*?'; } pattern = pattern + '<tr>([\\s\\S]*?)<\\/tr>'; // console.log(pattern);
// get the first <tr> from the html (row) let row = htmlWithoutComments.match(pattern)[1]; // console.log(row);
// remove <strong> and </strong> from the html (row) row = row.replace(/<strong>/g, ''); row = row.replace(/<\/strong>/g, ''); // remove all \n and \t from the html (row) row = row.replace(/\n/g, ''); row = row.replace(/\t/g, '');
// // get the string between > and </td> (column) and store it in an array let column = row.match(/>(.*?)<\/td>/g); // console.log(column[0]);
// if column[0] has SEMESTER RESULT then toggle the looping variable if (column[0].includes('SEMESTER RESULT')) { flag = true; // break; column[8] = column[8].replace(/<span style="color:#390;">/g, ''), column[8] = column[8].replace(/<span style="color:#F00;">/g, ''), column[8] = column[8].replace(/<\/span>/g, '') }
// remove <span style="color:green;" from the beginning and </span from the end, if its not the last row !flag ? column[12] = column[12].replace(/<span style="color:green;">/g, '') : null; !flag ? column[12] = column[12].replace(/<span style="color:#F00;">/g, '') : null; !flag ? column[12] = column[12].replace(/<\/span>/g, '') : null;
// remove SCPA / SGPA: from the beginning and tailing space from the end, if its the last row flag ? column[2] = column[2].replace(/SCPA: /g, '') : null; flag ? column[2] = column[2].trim() : null;
// loop through the column array and remove the > and </td> from each element for (let i = 0; i < column.length; i++) { column[i] = column[i].replace(/<\/?td>/g, ''); column[i] = column[i].replace(/>/g, '');
// also removespace from the beginning and end of the string // replace & with & // remove column[i] = column[i].trim(); column[i] = column[i].replace(/&/g, '&'); column[i] = column[i].replace(/ /g, '');
// replace blank string with null if (column[i] == '') { column[i] = null; } else if (column[i] == '---') { column[i] = null; } } // console.log(column);
// convert the column array to an object and push it to allSubjects array // if its the last row then create a semester object and save details if (!flag) {
let subject = { course_code : column[0], course : column[1], credit : column[2] == null ? null : parseInt(column[2]), external : { esa : column[3] == null ? null : parseInt(column[3]), max : column[4] == null ? null : parseInt(column[4]), }, internal : { isa : column[5] == null ? null : parseInt(column[5]), max : column[6] == null ? null : parseInt(column[6]), }, total : column[7] == null ? null : parseInt(column[7]), max : column[8] == null ? null : parseInt(column[8]), grade : column[9], grade_points : column[10] == null ? null : parseFloat(column[10]), credit_points : column[11] == null ? null : parseFloat(column[11]), result : column[12] }; allSubjects.push(subject); } else { semester = { credit : column[1] == null ? null : parseInt(column[1]), scpa : column[2] == null ? null : parseFloat(column[2]), total : column[3] == null ? null : parseInt(column[3]), max : column[4] == null ? null : parseInt(column[4]), grade : column[5], grade_points : column[6] == null ? null : parseInt(column[6]), credit_points : column[7] == null ? null : parseInt(column[7]), result : column[8] } }
}
// call processExamDetails() to get the general student/exam details let studentDetails = await module.exports.processExamDetails(student_id, exam_id);
semester = { ...studentDetails, result : { ...semester, subjects : allSubjects }, } // console.log(semester);
// save the semester object to a json file fs.writeFileSync(path.join(__dirname, `../public/analytics/${student_id}/semester.json`), JSON.stringify(semester));
// save the semester object to the database await saveData(semester, exam_id, qid);
},
processExamDetails : async (student_id) => {
let details = [];
// get table html from the file let html = fs.readFileSync(path.join(__dirname, `../public/analytics/${student_id}/details.html`), 'utf8');
// get the html between the inner <tbody> and </tbody> let table = html.match(/<table[\s\S]*?<\/table>/g)[0]; table = table.match(/<tbody[\s\S]*?<\/tbody>/g)[0]; // console.log(table);
// remove all <strong> and </strong> from the html table = table.replace(/<strong>/g, ''); table = table.replace(/<\/strong>/g, ''); // console.log(table);
for (let i = 1; i < 5; i++) {
// create a pattern to get the <tr> (rows) from the html let pattern = '<tbody>[\\s\\S]*?'; for (let j = 0; j < i-1; j++) { pattern = pattern + '<tr>[\\s\\S]*?'; } pattern = pattern + '<tr>([\\s\\S]*?)<\\/tr>'; // console.log(pattern);
// get the first <tr> from the html (row) // get the string between > and </td> (column) and store it in an array let row = table.match(pattern)[1]; let column = row.match(/>(.*?)<\/td>/g);
// remove > and </td> from the beginning and end of the string // remove blank spaces from the beginning and end of the string // replace & with & column[2] = column[2].replace(/<\/?td>/g, ''); column[2] = column[2].replace(/>/g, ''); column[2] = column[2].trim(); column[2] = column[2].replace(/&/g, '&');
// convert the uppercase string to title case if (column[2] == column[2].toUpperCase()) { column[2] = column[2].replace(/\w\S*/g, (txt) => { return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase(); }); } details.push(column[2]); }
// create an object with the details array var studentDetails = { prn : details[0], name : details[1], programme : details[2], exam_centre : details[3] }; // console.log(studentDetails); return studentDetails; },
};