Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version 1.1.0 #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 12 additions & 1 deletion src/eduhack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,25 @@ 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;
})
.parse(process.argv);

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" }));
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
78 changes: 46 additions & 32 deletions src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ export default async function (
url: string,
debug: boolean,
marking: boolean,
web: boolean
web: boolean,
timeout: string
): Promise<void> {
let load = ora("Setting up").start();

const browser = await puppeteer.launch({
headless: !web,
dumpio: debug,
slowMo: 10,
});
const page = (await browser.pages())[0];

Expand All @@ -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) => {
Expand Down Expand Up @@ -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<string, any>;

// 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<string, any>;

// 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();
}