use lowdb for data/epic-games.json
This commit is contained in:
parent
87df6d0e26
commit
0d847c479a
1 changed files with 92 additions and 69 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
import { chromium } from 'playwright'; // stealth plugin needs no outdated playwright-extra
|
import { chromium } from 'playwright'; // stealth plugin needs no outdated playwright-extra
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import { dirs, stealth } from './util.js';
|
import { dirs, jsonDb, datetime, stealth } from './util.js';
|
||||||
|
|
||||||
const debug = process.env.PWDEBUG == '1'; // runs non-headless and opens https://playwright.dev/docs/inspector
|
const debug = process.env.PWDEBUG == '1'; // runs non-headless and opens https://playwright.dev/docs/inspector
|
||||||
|
|
||||||
|
|
@ -10,6 +10,15 @@ const TIMEOUT = 20 * 1000; // 20s, default is 30s
|
||||||
const SCREEN_WIDTH = Number(process.env.SCREEN_WIDTH) - 80 || 1280;
|
const SCREEN_WIDTH = Number(process.env.SCREEN_WIDTH) - 80 || 1280;
|
||||||
const SCREEN_HEIGHT = Number(process.env.SCREEN_HEIGHT) || 1280;
|
const SCREEN_HEIGHT = Number(process.env.SCREEN_HEIGHT) || 1280;
|
||||||
|
|
||||||
|
const db = await jsonDb('epic-games.json');
|
||||||
|
db.data ||= { claimed: [], runs: [] };
|
||||||
|
const run = {
|
||||||
|
startTime: datetime(),
|
||||||
|
endTime: null,
|
||||||
|
n: null, // unclaimed games at beginning
|
||||||
|
c: 0, // claimed games at end
|
||||||
|
};
|
||||||
|
|
||||||
// https://playwright.dev/docs/auth#multi-factor-authentication
|
// https://playwright.dev/docs/auth#multi-factor-authentication
|
||||||
const context = await chromium.launchPersistentContext(dirs.browser, {
|
const context = await chromium.launchPersistentContext(dirs.browser, {
|
||||||
// chrome will not work in linux arm64, only chromium
|
// chrome will not work in linux arm64, only chromium
|
||||||
|
|
@ -32,7 +41,9 @@ await stealth(context);
|
||||||
if (!debug) context.setDefaultTimeout(TIMEOUT);
|
if (!debug) context.setDefaultTimeout(TIMEOUT);
|
||||||
|
|
||||||
const page = context.pages().length ? context.pages()[0] : await context.newPage(); // should always exist
|
const page = context.pages().length ? context.pages()[0] : await context.newPage(); // should always exist
|
||||||
console.log('userAgent:', await page.evaluate(() => navigator.userAgent));
|
console.debug('userAgent:', await page.evaluate(() => navigator.userAgent));
|
||||||
|
|
||||||
|
try {
|
||||||
await page.goto(URL_CLAIM, { waitUntil: 'domcontentloaded' }); // default 'load' takes forever
|
await page.goto(URL_CLAIM, { waitUntil: 'domcontentloaded' }); // default 'load' takes forever
|
||||||
// Accept cookies to get rid of banner to save space on screen. Will only appear for a fresh context, so we don't await, but let it time out if it does not exist and catch the exception. clickIfExists by checking selector's count > 0 did not work.
|
// Accept cookies to get rid of banner to save space on screen. Will only appear for a fresh context, so we don't await, but let it time out if it does not exist and catch the exception. clickIfExists by checking selector's count > 0 did not work.
|
||||||
page.click('button:has-text("Accept All Cookies")').catch(_ => {}); // _ => console.info('Cookies already accepted')
|
page.click('button:has-text("Accept All Cookies")').catch(_ => {}); // _ => console.info('Cookies already accepted')
|
||||||
|
|
@ -50,7 +61,7 @@ console.log('Signed in.');
|
||||||
const game_sel = 'a:has-text("Free Now")';
|
const game_sel = 'a:has-text("Free Now")';
|
||||||
await page.waitForSelector(game_sel);
|
await page.waitForSelector(game_sel);
|
||||||
// const games = await page.$$(game_sel); // 'Element is not attached to the DOM' after navigation; had `for (const game of games) { await game.click(); ... }
|
// const games = await page.$$(game_sel); // 'Element is not attached to the DOM' after navigation; had `for (const game of games) { await game.click(); ... }
|
||||||
const n = await page.locator(game_sel).count();
|
const n = run.n = await page.locator(game_sel).count();
|
||||||
console.log('Number of free games:', n);
|
console.log('Number of free games:', n);
|
||||||
for (let i = 1; i <= n; i++) {
|
for (let i = 1; i <= n; i++) {
|
||||||
await page.click(`:nth-match(${game_sel}, ${i})`);
|
await page.click(`:nth-match(${game_sel}, ${i})`);
|
||||||
|
|
@ -90,10 +101,12 @@ for (let i = 1; i <= n; i++) {
|
||||||
// }
|
// }
|
||||||
// await page.waitForTimeout(3000);
|
// await page.waitForTimeout(3000);
|
||||||
await page.waitForSelector('text=Thank you for buying'); // EU: wait, non-EU: wait again
|
await page.waitForSelector('text=Thank you for buying'); // EU: wait, non-EU: wait again
|
||||||
|
db.data.claimed.push({title, time: datetime()});
|
||||||
|
run.c++;
|
||||||
console.log('Claimed successfully!');
|
console.log('Claimed successfully!');
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log(e);
|
console.log(e);
|
||||||
const p = path.resolve(dirs.screenshots, 'epic-games', `${new Date().toISOString()}.png`);
|
const p = path.resolve(dirs.screenshots, 'epic-games', `${datetime()}.png`);
|
||||||
await page.screenshot({ path: p, fullPage: true });
|
await page.screenshot({ path: p, fullPage: true });
|
||||||
console.info('Saved a screenshot of hcaptcha challenge to', p);
|
console.info('Saved a screenshot of hcaptcha challenge to', p);
|
||||||
console.error('Got hcaptcha challenge. To avoid it, get a link from https://www.hcaptcha.com/accessibility'); // TODO save this link in config and visit it daily to set accessibility cookie to avoid captcha challenge?
|
console.error('Got hcaptcha challenge. To avoid it, get a link from https://www.hcaptcha.com/accessibility'); // TODO save this link in config and visit it daily to set accessibility cookie to avoid captcha challenge?
|
||||||
|
|
@ -105,5 +118,15 @@ for (let i = 1; i <= n; i++) {
|
||||||
await page.waitForSelector(game_sel);
|
await page.waitForSelector(game_sel);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} catch(error) {
|
||||||
|
console.error(error);
|
||||||
|
run.error = error.toString();
|
||||||
|
} finally {
|
||||||
|
// write out json db
|
||||||
|
run.endTime = datetime();
|
||||||
|
db.data.runs.push(run);
|
||||||
|
await db.write();
|
||||||
|
|
||||||
// await context.waitForEvent("close");
|
// await context.waitForEvent("close");
|
||||||
await context.close();
|
await context.close();
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue