From 59450ed05ca402025cfd25e9e2cb5fb728667eb1 Mon Sep 17 00:00:00 2001 From: Ralf Vogler Date: Thu, 5 May 2022 23:49:55 +0200 Subject: [PATCH] mv userDataDir data/browser; mv screenshots data/ --- .dockerignore | 5 +---- .gitignore | 5 +---- epic-games.js | 7 ++++--- package.json | 2 +- prime-gaming.js | 6 +++--- util.js | 11 +++++++++-- 6 files changed, 19 insertions(+), 17 deletions(-) diff --git a/.dockerignore b/.dockerignore index 35412b7..ffd3c43 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,9 +1,6 @@ -userDataDir** node_modules -screenshots +data .gitignore **Dockerfile** .dockerignore -.env -auth.json diff --git a/.gitignore b/.gitignore index b2ff24b..902b281 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,2 @@ node_modules/ -auth.json -.env -userDataDir/ -screenshots/ +data/ diff --git a/epic-games.js b/epic-games.js index f422d91..214ba93 100644 --- a/epic-games.js +++ b/epic-games.js @@ -1,6 +1,7 @@ import { chromium } from 'playwright'; // stealth plugin needs no outdated playwright-extra 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 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; // 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 // channel: 'chrome', // https://playwright.dev/docs/browsers#google-chrome--microsoft-edge headless: false, @@ -98,7 +99,7 @@ for (let i = 1; i <= n; i++) { console.log('Claimed successfully!'); } catch (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 }); 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? diff --git a/package.json b/package.json index b5fc415..bca64ee 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "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", "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": { "@playwright/test": "^1.20.1", diff --git a/prime-gaming.js b/prime-gaming.js index 911f1f0..ebc6347 100644 --- a/prime-gaming.js +++ b/prime-gaming.js @@ -1,6 +1,6 @@ import { chromium } from 'playwright'; // stealth plugin needs no outdated playwright-extra 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 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 // 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 headless, 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 ', ''); console.log('External store:', store); // 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 }); console.info('Saved a screenshot of page to', p); // print code if external store is not connected diff --git a/util.js b/util.js index d971ba1..9c6d48e 100644 --- a/util.js +++ b/util.js @@ -1,8 +1,15 @@ // https://stackoverflow.com/questions/46745014/alternative-for-dirname-in-node-js-when-using-es6-modules import path from 'node:path'; import { fileURLToPath } from 'node:url'; -export const __filename = fileURLToPath(import.meta.url); -export const __dirname = path.dirname(__filename); +// not the same since these will give the absolute paths for this file instead of for the file using them +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 const newStealthContext = async (browser, contextOptions = {}, debug = false) => {