From b5ead8ea21129830596f7058775c97e9832813e9 Mon Sep 17 00:00:00 2001 From: Ralf Vogler Date: Thu, 20 Jul 2023 16:10:50 +0200 Subject: [PATCH] SCREENSHOTS_DIR=0 to disable screenshots, fixes #172 --- config.js | 2 +- epic-games.js | 8 +++++--- gog.js | 8 ++++---- prime-gaming.js | 13 ++++++------- unrealengine.js | 10 +++++----- util.js | 3 +++ 6 files changed, 24 insertions(+), 20 deletions(-) diff --git a/config.js b/config.js index 32974e1..d863d8d 100644 --- a/config.js +++ b/config.js @@ -20,7 +20,7 @@ export const cfg = { get dir() { // avoids ReferenceError: Cannot access 'dataDir' before initialization return { browser: process.env.BROWSER_DIR || dataDir('browser'), // for multiple accounts or testing - screenshots: process.env.SCREENSHOTS_DIR || dataDir('screenshots'), // if not wanted: /dev/null + screenshots: process.env.SCREENSHOTS_DIR || dataDir('screenshots'), // set to 0 to disable screenshots } }, // auth epic-games diff --git a/epic-games.js b/epic-games.js index 37310ea..314b349 100644 --- a/epic-games.js +++ b/epic-games.js @@ -2,9 +2,11 @@ import { firefox } from 'playwright-firefox'; // stealth plugin needs no outdate import { authenticator } from 'otplib'; import path from 'path'; import { existsSync, writeFileSync } from 'fs'; -import { jsonDb, datetime, stealth, filenamify, prompt, notify, html_game_list, handleSIGINT } from './util.js'; +import { resolve, jsonDb, datetime, stealth, filenamify, prompt, notify, html_game_list, handleSIGINT } from './util.js'; import { cfg } from './config.js'; +const screenshot = (...a) => resolve(cfg.dir.screenshots, 'epic-games', ...a); + const URL_CLAIM = 'https://store.epicgames.com/en-US/free-games'; const URL_LOGIN = 'https://www.epicgames.com/id/login?lang=en-US&noHostRedirect=true&redirectUrl=' + URL_CLAIM; @@ -218,13 +220,13 @@ try { console.log(e); // console.error(' Failed to claim! Try again if NopeCHA timed out. Click the extension to see if you ran out of credits (refill after 24h). To avoid captchas try to get a new IP or set a cookie from https://www.hcaptcha.com/accessibility'); console.error(' Failed to claim! To avoid captchas try to get a new IP address.'); - const p = path.resolve(cfg.dir.screenshots, 'epic-games', 'failed', `${game_id}_${filenamify(datetime())}.png`); + const p = screenshot('failed', `${game_id}_${filenamify(datetime())}.png`); await page.screenshot({ path: p, fullPage: true }); db.data[user][game_id].status = 'failed'; } notify_game.status = db.data[user][game_id].status; // claimed or failed - const p = path.resolve(cfg.dir.screenshots, 'epic-games', `${game_id}.png`); + const p = screenshot(`${game_id}.png`); if (!existsSync(p)) await page.screenshot({ path: p, fullPage: false }); // fullPage is quite long... } } diff --git a/gog.js b/gog.js index 1785663..cde7acb 100644 --- a/gog.js +++ b/gog.js @@ -1,8 +1,9 @@ import { firefox } from 'playwright-firefox'; // stealth plugin needs no outdated playwright-extra -import path from 'path'; -import { jsonDb, datetime, filenamify, prompt, notify, html_game_list, handleSIGINT } from './util.js'; +import { resolve, jsonDb, datetime, filenamify, prompt, notify, html_game_list, handleSIGINT } from './util.js'; import { cfg } from './config.js'; +const screenshot = (...a) => resolve(cfg.dir.screenshots, 'gog', ...a); + const URL_CLAIM = 'https://www.gog.com/en'; console.log(datetime(), 'started checking gog'); @@ -99,8 +100,7 @@ try { db.data[user][title] ||= { title, time: datetime(), url }; if (cfg.dryrun) process.exit(1); await page.locator('#giveaway:not(.is-loading)').waitFor(); // otherwise screenshot is sometimes with loading indicator instead of game title - const p = path.resolve(cfg.dir.screenshots, 'gog', `${filenamify(title)}.png`); - await banner.screenshot({ path: p }); // overwrites every time - only keep first? + await banner.screenshot({ path: screenshot(`${filenamify(title)}.png`) }); // overwrites every time - only keep first? // await banner.getByRole('button', { name: 'Add to library' }).click(); // instead of clicking the button, we visit the auto-claim URL which gives as a JSON response which is easier than checking the state of a button diff --git a/prime-gaming.js b/prime-gaming.js index d9fcd7e..1af1eb8 100644 --- a/prime-gaming.js +++ b/prime-gaming.js @@ -1,9 +1,10 @@ import { firefox } from 'playwright-firefox'; // stealth plugin needs no outdated playwright-extra import { authenticator } from 'otplib'; -import path from 'path'; -import { jsonDb, datetime, stealth, filenamify, prompt, notify, html_game_list, handleSIGINT } from './util.js'; +import { resolve, jsonDb, datetime, stealth, filenamify, prompt, notify, html_game_list, handleSIGINT } from './util.js'; import { cfg } from './config.js'; +const screenshot = (...a) => resolve(cfg.dir.screenshots, 'prime-gaming', ...a); + // const URL_LOGIN = 'https://www.amazon.de/ap/signin'; // wrong. needs some session args to be valid? const URL_CLAIM = 'https://gaming.amazon.com/home'; @@ -116,8 +117,7 @@ try { notify_games.push({ title, status: 'claimed', url: URL_CLAIM }); // const img = await (await card.$('img.tw-image')).getAttribute('src'); // console.log('Image:', img); - const p = path.resolve(cfg.dir.screenshots, 'prime-gaming', 'internal', `${filenamify(title)}.png`); - await card.screenshot({ path: p }); + await card.screenshot({ path: screenshot('internal', `${filenamify(title)}.png`) }); } console.log('Number of free unclaimed games (external stores):', await external.count()); // claim games in external/linked stores. Linked: origin.com, epicgames.com; Redeem-key: gog.com, legacygames.com, microsoft @@ -263,8 +263,7 @@ try { db.data[user][title].status = 'claimed'; } // save screenshot of potential code just in case - const p = path.resolve(cfg.dir.screenshots, 'prime-gaming', 'external', `${filenamify(title)}.png`); - await page.screenshot({ path: p, fullPage: true }); + await page.screenshot({ path: screenshot('external', `${filenamify(title)}.png`), fullPage: true }); // console.info(' Saved a screenshot of page to', p); } // await page.pause(); @@ -273,7 +272,7 @@ try { } if (notify_games.length) { // make screenshot of all games if something was claimed - const p = path.resolve(cfg.dir.screenshots, 'prime-gaming', `${filenamify(datetime())}.png`); + const p = screenshot(`${filenamify(datetime())}.png`); // await page.screenshot({ path: p, fullPage: true }); // fullPage does not make a difference since scroll not on body but on some element await page.keyboard.press('End'); // scroll to bottom to show all games await page.waitForTimeout(1000); // wait for fade in animation diff --git a/unrealengine.js b/unrealengine.js index 2993fea..830496c 100644 --- a/unrealengine.js +++ b/unrealengine.js @@ -4,9 +4,11 @@ import { firefox } from 'playwright-firefox'; // stealth plugin needs no outdate import { authenticator } from 'otplib'; import path from 'path'; import { writeFileSync } from 'fs'; -import { jsonDb, datetime, stealth, filenamify, prompt, notify, html_game_list, handleSIGINT } from './util.js'; +import { resolve, jsonDb, datetime, stealth, filenamify, prompt, notify, html_game_list, handleSIGINT } from './util.js'; import { cfg } from './config.js'; +const screenshot = (...a) => resolve(cfg.dir.screenshots, 'unrealengine', ...a); + const URL_CLAIM = 'https://www.unrealengine.com/marketplace/en-US/assets?count=20&sortBy=effectiveDate&sortDir=DESC&start=0&tag=4910'; const URL_LOGIN = 'https://www.epicgames.com/id/login?lang=en-US&noHostRedirect=true&redirectUrl=' + URL_CLAIM; @@ -176,15 +178,13 @@ try { console.log(e); // console.error(' Failed to claim! Try again if NopeCHA timed out. Click the extension to see if you ran out of credits (refill after 24h). To avoid captchas try to get a new IP or set a cookie from https://www.hcaptcha.com/accessibility'); console.error(' Failed to claim! To avoid captchas try to get a new IP address.'); - const p = path.resolve(cfg.dir.screenshots, 'unrealengine', 'failed', `${filenamify(datetime())}.png`); - await page.screenshot({ path: p, fullPage: true }); + await page.screenshot({ path: screenshot('failed', `${filenamify(datetime())}.png`), fullPage: true }); // db.data[user][id].status = 'failed'; notify_games.forEach(g => g.status = 'failed'); } // notify_game.status = db.data[user][game_id].status; // claimed or failed - const p = path.resolve(cfg.dir.screenshots, 'unrealengine', `${filenamify(datetime())}.png`); - if (notify_games.length) await page.screenshot({ path: p, fullPage: false }); // fullPage is quite long... + if (notify_games.length) await page.screenshot({ path: screenshot(`${filenamify(datetime())}.png`), fullPage: false }); // fullPage is quite long... console.log('Done'); } } catch (error) { diff --git a/util.js b/util.js index 2725745..e581684 100644 --- a/util.js +++ b/util.js @@ -7,6 +7,9 @@ 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 export const dataDir = s => path.resolve(__dirname, 'data', s); +// modified path.resolve to return null if first argument is '0', used to disable screenshots +export const resolve = (...a) => a.length && a[0] == '0' ? null : path.resolve(...a); + // json database import { Low } from 'lowdb'; import { JSONFile } from 'lowdb/node';