From d8e2093a0dc3f9c00b1b1cdd812395bbf179e231 Mon Sep 17 00:00:00 2001 From: Ralf Vogler Date: Tue, 5 Sep 2023 15:56:24 +0200 Subject: [PATCH] pg: INTERACTIVE=1 to confirm each claim or skip it --- config.js | 1 + prime-gaming.js | 5 ++++- util.js | 1 + 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/config.js b/config.js index e6a9438..2dd064d 100644 --- a/config.js +++ b/config.js @@ -9,6 +9,7 @@ export const cfg = { record: process.env.RECORD == '1', // `recordHar` (network) + `recordVideo` time: process.env.TIME == '1', // log duration of each step dryrun: process.env.DRYRUN == '1', // don't claim anything + interactive: process.env.INTERACTIVE == '1', // confirm to claim, default skip show: process.env.SHOW == '1', // run non-headless get headless() { return !this.debug && !this.show }, width: Number(process.env.WIDTH) || 1280, // width of the opened browser diff --git a/prime-gaming.js b/prime-gaming.js index 659bd26..21b18fc 100644 --- a/prime-gaming.js +++ b/prime-gaming.js @@ -1,6 +1,6 @@ import { firefox } from 'playwright-firefox'; // stealth plugin needs no outdated playwright-extra import { authenticator } from 'otplib'; -import { resolve, jsonDb, datetime, stealth, filenamify, prompt, notify, html_game_list, handleSIGINT } from './util.js'; +import { resolve, jsonDb, datetime, stealth, filenamify, prompt, confirm, notify, html_game_list, handleSIGINT } from './util.js'; import { cfg } from './config.js'; const screenshot = (...a) => resolve(cfg.dir.screenshots, 'prime-gaming', ...a); @@ -111,6 +111,7 @@ try { const title = await (await card.$('.item-card-details__body__primary')).innerText(); console.log('Current free game:', title); if (cfg.dryrun) continue; + if (cfg.interactive && !await confirm()) continue; await (await card.$('button:has-text("Claim")')).click(); db.data[user][title] ||= { title, time: datetime(), store: 'internal' }; notify_games.push({ title, status: 'claimed', url: URL_CLAIM }); @@ -133,6 +134,7 @@ try { await page.goto(url, { waitUntil: 'domcontentloaded' }); if (cfg.debug) await page.pause(); if (cfg.dryrun) continue; + if (cfg.interactive && !await confirm()) continue; await Promise.any([page.click('button:has-text("Get game")'), page.click('button:has-text("Claim now")'), page.click('button:has-text("Complete Claim")'), page.waitForSelector('div:has-text("Link game account")')]); // waits for navigation // TODO would be simpler than the below, but will block for linked stores without code @@ -324,6 +326,7 @@ try { console.log('Current DLC:', title); if (cfg.debug) await page.pause(); if (cfg.dryrun) continue; + if (cfg.interactive && !await confirm()) continue; db.data[user][title] ||= { title, time: datetime(), store: 'DLC', status: 'failed: need account linking' }; const notify_game = { title, url }; notify_games.push(notify_game); // status is updated below diff --git a/util.js b/util.js index 0b2dcbe..60ff4a5 100644 --- a/util.js +++ b/util.js @@ -98,6 +98,7 @@ const timeoutPlugin = timeout => enquirer => { // cancel prompt after timeout ms enquirer.use(timeoutPlugin(cfg.login_timeout)); // TODO may not want to have this timeout for all prompts; better extend Prompt and add a timeout prompt option // single prompt that just returns the non-empty value instead of an object export const prompt = o => enquirer.prompt({name: 'name', type: 'input', message: 'Enter value', ...o}).then(r => r.name).catch(_ => {}); +export const confirm = o => prompt({type: 'confirm', message: 'Continue?', ...o}) // notifications via apprise CLI import { exec } from 'child_process';