diff --git a/README.MD b/README.MD index 9b73264..66c1ff8 100644 --- a/README.MD +++ b/README.MD @@ -2,7 +2,7 @@ Web automation cli package used to get exam data. Written in node.js and typescript and mainly the pupeteer package. [![npm](https://img.shields.io/npm/v/edu-exam-cli?color=green)](https://www.npmjs.com/package/edu-exam-cli) -[![Stable](https://github.com/lampask/educli/workflows/Stable/badge.svg?branch=master)](https://github.com/lampask/educli/actions?query=workflow%3AStable) +[![Stable](https://github.com/lampask/educli/workflows/Stable/badge.svg?branch=master&event=push)](https://github.com/lampask/educli/actions?query=workflow%3AStable) [![Develop](https://github.com/lampask/educli/workflows/Develop/badge.svg?branch=develop)](https://github.com/lampask/educli/actions?query=workflow%3ADevelop) [![GitHub](https://img.shields.io/github/license/lampask/EduCli)](https://opensource.org/licenses/MIT) ## Installation diff --git a/src/eduhack.ts b/src/eduhack.ts index bcb59a9..204eb9b 100644 --- a/src/eduhack.ts +++ b/src/eduhack.ts @@ -20,6 +20,11 @@ program .option("-d, --debug", "run in debug mode") .option("-m, --marking", "get the point count for each question") .option("-w, --web", "simulate browser in separate window") + .option( + "-t, --timeout [ms]", + "modify default waiting time in ms for content loading", + "3000" + ) .action(function (url) { web_url = url; }) @@ -27,7 +32,13 @@ program if (web_url) { if (is_valid_url(web_url)) { - web_process(web_url, program.debug, program.marking, program.web); + web_process( + web_url, + program.debug, + program.marking, + program.web, + program.timeout + ); } } else { lolcat.fromString(figlet.textSync("edu-cli", { horizontalLayout: "full" })); diff --git a/src/utils.ts b/src/utils.ts index cb6e113..86d0c18 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -12,7 +12,7 @@ const url_pattern = new RegExp( ); // fragment locator const edupage_pattern = new RegExp( - "^(http://www.|https://www.|http://|https://)?[a-z0-9]+([-.]edupage+).(com)?(/.*)?$" + "^(http://www.|https://www.|http://|https://)?[a-z0-9]+([-.]edupage+).(org)?(/.*)?$" ); export function is_valid_url(url: string): boolean { diff --git a/src/web.ts b/src/web.ts index c20b43d..0e74ce1 100644 --- a/src/web.ts +++ b/src/web.ts @@ -10,14 +10,14 @@ export default async function ( url: string, debug: boolean, marking: boolean, - web: boolean + web: boolean, + timeout: string ): Promise { let load = ora("Setting up").start(); const browser = await puppeteer.launch({ headless: !web, dumpio: debug, - slowMo: 10, }); const page = (await browser.pages())[0]; @@ -39,6 +39,8 @@ export default async function ( } }); + await page.waitFor(parseInt(timeout) / 2); + //Step 2 - Check logged status let success = false; await page.title().then(async (title) => { @@ -66,36 +68,48 @@ export default async function ( //await page.waitForNavigation(); load.text = "Waiting for contents to load"; + await page.waitFor(parseInt(timeout)); + let exam_data: CardHolderModel = {}; - setTimeout(async () => { - await page.evaluate("ETestUtils.cardsData").then((data) => { - if (data !== {} || typeof data == "object") { - load.text = "Data obtained"; - load.succeed(); - exam_data = data as Record; - - // Step 4 - Process the data - let i = 0; - let message = ""; - Object.values(exam_data).forEach((card_data) => { - message += chalk.red(`Test Question ${++i}`); - const formatted_output = card_data.description!!; - message += formatted_output.replace( - "aid", - chalk.green("Correct answer is: ") - ); - message += - i < Object.keys(exam_data).length - ? "---------------------------\n" - : ""; + await page.evaluate("ETestUtils.cardsData").then((data) => { + if (data !== {} || typeof data == "object") { + load.text = "Data obtained"; + load.succeed(); + exam_data = data as Record; + + // Step 4 - Process the data + let i = 0; + let message = ""; + Object.values(exam_data).forEach((card_data) => { + message += chalk.red(`Test Question ${++i}`); + const formatted_output = card_data.description; + message += formatted_output.replace( + "aid", + chalk.green("Correct answer is: ") + ); + card_data.content.widgets.forEach((widget: any) => { + if (widget.widgetClass === "ConnectAnswerETestWidget") { + widget.props.pairs.forEach((pair: any) => { + message += chalk.green( + `${pair.l.replace(/(<([^>]+)>)/gi, "")} <=> ${pair.r.replace( + /(<([^>]+)>)/gi, + "" + )} \n` + ); + }); + } }); - console.log(boxen(message, { padding: 1, margin: 1 })); - } else { - load.text = "An error occured while obtaining data"; - load.fail(); - } - }); - await browser.close(); - process.exit(); - }, 5000); + message += + i < Object.keys(exam_data).length + ? "---------------------------\n" + : ""; + }); + console.log(boxen(message, { padding: 1, margin: 1 })); + } else { + load.text = "An error occured while obtaining data"; + load.fail(); + } + }); + await browser.close(); + process.exit(); }