mv userDataDir data/browser; mv screenshots data/

This commit is contained in:
Ralf Vogler 2022-05-05 23:49:55 +02:00
parent bba71efbc9
commit 59450ed05c
6 changed files with 19 additions and 17 deletions

View file

@ -1,9 +1,6 @@
userDataDir**
node_modules node_modules
screenshots data
.gitignore .gitignore
**Dockerfile** **Dockerfile**
.dockerignore .dockerignore
.env
auth.json

5
.gitignore vendored
View file

@ -1,5 +1,2 @@
node_modules/ node_modules/
auth.json data/
.env
userDataDir/
screenshots/

View file

@ -1,6 +1,7 @@
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 { __dirname, stealth } from './util.js'; import { dirs, 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
const URL_CLAIM = 'https://store.epicgames.com/en-US/free-games'; const URL_CLAIM = 'https://store.epicgames.com/en-US/free-games';
@ -10,7 +11,7 @@ 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;
// https://playwright.dev/docs/auth#multi-factor-authentication // https://playwright.dev/docs/auth#multi-factor-authentication
const context = await chromium.launchPersistentContext(path.resolve(__dirname, 'userDataDir'), { 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
// channel: 'chrome', // https://playwright.dev/docs/browsers#google-chrome--microsoft-edge // channel: 'chrome', // https://playwright.dev/docs/browsers#google-chrome--microsoft-edge
headless: false, headless: false,
@ -98,7 +99,7 @@ for (let i = 1; i <= n; i++) {
console.log('Claimed successfully!'); console.log('Claimed successfully!');
} catch (e) { } catch (e) {
console.log(e); console.log(e);
const p = `screenshots/${new Date().toISOString()}.png`; const p = path.resolve(dirs.screenshots, `${new Date().toISOString()}.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?

View file

@ -3,7 +3,7 @@
"login": "npx playwright open --save-storage=auth.json https://www.epicgames.com/login", "login": "npx playwright open --save-storage=auth.json https://www.epicgames.com/login",
"codegen": "npx playwright codegen --load-storage=auth.json https://www.epicgames.com/store/en-US/free-games", "codegen": "npx playwright codegen --load-storage=auth.json https://www.epicgames.com/store/en-US/free-games",
"docker:build": "docker build --tag free-games-claimer .", "docker:build": "docker build --tag free-games-claimer .",
"docker:epic-games": "rimraf userDataDir/SingletonLock && cross-env-shell docker run --rm -it -p 5900:5900 -p 6080:6080 -v \"$INIT_CWD/userDataDir\":/fgc/userDataDir --name free-games-claimer free-games-claimer" "docker:epic-games": "rimraf data/browser/SingletonLock && cross-env-shell docker run --rm -it -p 5900:5900 -p 6080:6080 -v \"$INIT_CWD/data\":/fgc/data --name free-games-claimer free-games-claimer"
}, },
"devDependencies": { "devDependencies": {
"@playwright/test": "^1.20.1", "@playwright/test": "^1.20.1",

View file

@ -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 { __dirname, stealth } from './util.js'; import { dirs, stealth } from './util.js';
const debug = process.env.PWDEBUG == '1'; // runs headful and opens https://playwright.dev/docs/inspector const debug = process.env.PWDEBUG == '1'; // runs headful and opens https://playwright.dev/docs/inspector
const show = process.argv.includes('show', 2); const show = process.argv.includes('show', 2);
@ -11,7 +11,7 @@ const URL_CLAIM = 'https://gaming.amazon.com/home';
const TIMEOUT = 20 * 1000; // 20s, default is 30s const TIMEOUT = 20 * 1000; // 20s, default is 30s
// https://playwright.dev/docs/auth#multi-factor-authentication // https://playwright.dev/docs/auth#multi-factor-authentication
const context = await chromium.launchPersistentContext(path.resolve(__dirname, 'userDataDir'), { const context = await chromium.launchPersistentContext(dirs.browser, {
// channel: 'chrome', // https://playwright.dev/docs/browsers#google-chrome--microsoft-edge, chrome will not work on arm64 linux, only chromium which is the default // channel: 'chrome', // https://playwright.dev/docs/browsers#google-chrome--microsoft-edge, chrome will not work on arm64 linux, only chromium which is the default
headless, headless,
viewport: { width: 1280, height: 1280 }, viewport: { width: 1280, height: 1280 },
@ -84,7 +84,7 @@ for (const card of games) {
const store = store_text.toLowerCase().replace('full game for pc on ', ''); const store = store_text.toLowerCase().replace('full game for pc on ', '');
console.log('External store:', store); console.log('External store:', store);
// save screenshot of potential code just in case // save screenshot of potential code just in case
const p = `screenshots/${title.replace(/[^a-z0-9]/gi, '_')}.png`; const p = path.resolve(dirs.screenshots, `${title.replace(/[^a-z0-9]/gi, '_')}.png`);
await page.screenshot({ path: p, fullPage: true }); await page.screenshot({ path: p, fullPage: true });
console.info('Saved a screenshot of page to', p); console.info('Saved a screenshot of page to', p);
// print code if external store is not connected // print code if external store is not connected

11
util.js
View file

@ -1,8 +1,15 @@
// https://stackoverflow.com/questions/46745014/alternative-for-dirname-in-node-js-when-using-es6-modules // https://stackoverflow.com/questions/46745014/alternative-for-dirname-in-node-js-when-using-es6-modules
import path from 'node:path'; import path from 'node:path';
import { fileURLToPath } from 'node:url'; import { fileURLToPath } from 'node:url';
export const __filename = fileURLToPath(import.meta.url); // not the same since these will give the absolute paths for this file instead of for the file using them
export const __dirname = path.dirname(__filename); const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// explicit object instead of Object.fromEntries since the built-in type would loose the keys, better type: https://dev.to/svehla/typescript-object-fromentries-389c
const dataDir = s => path.resolve(__dirname, 'data', s);
export const dirs = {
browser: dataDir('browser'),
screenshots: dataDir('screenshots'),
};
// stealth with playwright: https://github.com/berstend/puppeteer-extra/issues/454#issuecomment-917437212 // stealth with playwright: https://github.com/berstend/puppeteer-extra/issues/454#issuecomment-917437212
const newStealthContext = async (browser, contextOptions = {}, debug = false) => { const newStealthContext = async (browser, contextOptions = {}, debug = false) => {