Skip to content
This repository has been archived by the owner on Jun 18, 2024. It is now read-only.

Commit

Permalink
[:recycle:] update messages and delays
Browse files Browse the repository at this point in the history
  • Loading branch information
mimshins committed Dec 10, 2022
1 parent 7286cc9 commit 4bc6ca5
Showing 1 changed file with 15 additions and 18 deletions.
33 changes: 15 additions & 18 deletions bin/Bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
saveLocalCopy,
submitLoginForm,
timer,
wait,
waitAndNavigate,
waitForRedirects
} from "./utils";
Expand Down Expand Up @@ -89,22 +88,22 @@ export default class Bot {
}

private async _login(page: Page): Promise<void> {
let spinner = new Spinner("\t. Navigating to the login page...");
const spinner = new Spinner("\t. Navigating to the login page...");

spinner.start();
await waitAndNavigate(page, page.goto("https://www.figma.com/login"));
spinner.stop();

const checkRecent = () =>
page.url().includes("https://www.figma.com/files/recent");
const checkRecent = (url: string) =>
url.includes("https://www.figma.com/files") && url.includes("/recent");

if (checkRecent()) {
if (checkRecent(page.url())) {
log(chalk.red("\t.") + chalk.bold(` Bot is already logged in.`));
return;
}

try {
spinner = new Spinner("\t. Submitting the login form...");
spinner.message("\t. Submitting the login form...");

spinner.start();
await waitAndNavigate(
Expand All @@ -119,7 +118,8 @@ export default class Bot {
throw new AuthorizationError(e as Error);
}

if (checkRecent()) {
const pageUrl = page.url();
if (checkRecent(pageUrl)) {
log(chalk.red("\t.") + chalk.bold(` Bot successfully logged in.`));

if (this._cookiesProvider) {
Expand All @@ -129,13 +129,11 @@ export default class Bot {
SESSION_DATA.cookies = cookies;
await this._cookiesProvider.setCookies(cookies);
}
} else if (page.url() === "https://www.figma.com/login") {
} else if (pageUrl === "https://www.figma.com/login") {
const error = await parseLoginFormError(page);
throw new AuthorizationError(error || "unknown error");
} else {
throw new AuthorizationError(
`Unexpectedly redirected to "${page.url()}"`
);
throw new AuthorizationError(`Unexpectedly redirected to "${pageUrl}"`);
}
}

Expand All @@ -151,10 +149,11 @@ export default class Bot {

if (!cookies) throw new Error("No cached cookies found.");

log(chalk.red("\t.") + chalk.bold(` Restoring the cached cookies...`));
log(chalk.red("\t.") + chalk.bold(` Setting the cached cookies...`));
await page.setCookie(...cookies);

const spinner = new Spinner("\t. Waiting for the redirection...");

spinner.start();
await waitForRedirects(page);
spinner.stop();
Expand All @@ -172,25 +171,23 @@ export default class Bot {
const page: Page = await this._browser.newPage();
page.setDefaultNavigationTimeout(60 * 1000);

let spinner = new Spinner(`\t. Navigating to the file(${file.name})...`);
const spinner = new Spinner(`\t. Navigating to the file(${file.name})...`);

log(chalk.red(">") + chalk.bold(` Backuping the file(${file.name})...`));
log(chalk.red(">") + chalk.bold(` Backing up the file(${file.name})...`));

spinner.start();
await waitAndNavigate(page, goToFilePage(page, file.id));
spinner.stop();

spinner = new Spinner("\t. Waiting for the page to be loaded...");
spinner.message("\t. Waiting for the page to be loaded...");

spinner.start();
await wait(this._interactionDelay);
await page.waitForFunction(
() => !document.querySelector('[class*="progress_bar--outer"]')
);
spinner.stop();

log(chalk.red("\t.") + chalk.bold(` Setting the download behaviour...`));
await wait(this._interactionDelay);
log(chalk.red("\t.") + chalk.bold(` Setting the download behavior...`));

const client = await page.target().createCDPSession();
await client.send("Page.setDownloadBehavior", {
Expand Down

0 comments on commit 4bc6ca5

Please sign in to comment.