diff --git a/epic-games.js b/epic-games.js index d9bfb27..812af6b 100644 --- a/epic-games.js +++ b/epic-games.js @@ -2,7 +2,7 @@ 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 } from './util.js'; +import { jsonDb, datetime, stealth, filenamify, prompt, notify, html_game_list, handleSIGINT } from './util.js'; import { cfg } from './config.js'; const URL_CLAIM = 'https://store.epicgames.com/en-US/free-games'; @@ -13,11 +13,7 @@ console.log(datetime(), 'started checking epic-games'); const db = await jsonDb('epic-games.json'); db.data ||= {}; -let exit = false; -process.on('SIGINT', () => { // e.g. when killed by Ctrl-C - console.log('\nInterrupted by SIGINT. Exit! Exception shows where the script was:\n'); - exit = true; -}); +handleSIGINT(); // https://www.nopecha.com extension source from https://github.com/NopeCHA/NopeCHA/releases/tag/0.1.16 // const ext = path.resolve('nopecha'); // used in Chromium, currently not needed in Firefox @@ -193,8 +189,8 @@ try { } } catch (error) { console.error(error); // .toString()? - process.exitCode = 1; - if (error.message && !exit) + process.exitCode ||= 1; + if (error.message && process.exitCode != 130) notify(`epic-games failed: ${error.message.split('\n')[0]}`); } finally { await db.write(); // write out json db diff --git a/gog.js b/gog.js index b0a582c..4128e89 100644 --- a/gog.js +++ b/gog.js @@ -1,6 +1,6 @@ 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 } from './util.js'; +import { jsonDb, datetime, filenamify, prompt, notify, html_game_list, handleSIGINT } from './util.js'; import { cfg } from './config.js'; const URL_CLAIM = 'https://www.gog.com/en'; @@ -10,11 +10,7 @@ console.log(datetime(), 'started checking gog'); const db = await jsonDb('gog.json'); db.data ||= {}; -let exit = false; -process.on('SIGINT', () => { // e.g. when killed by Ctrl-C - console.log('\nInterrupted by SIGINT. Exit! Exception shows where the script was:\n'); - exit = true; -}); +handleSIGINT(); // https://playwright.dev/docs/auth#multi-factor-authentication const context = await firefox.launchPersistentContext(cfg.dir.browser, { @@ -138,8 +134,8 @@ try { } } catch (error) { console.error(error); // .toString()? - process.exitCode = 1; - if (error.message && !exit) + process.exitCode ||= 1; + if (error.message && process.exitCode != 130) notify(`gog failed: ${error.message.split('\n')[0]}`); } finally { await db.write(); // write out json db diff --git a/prime-gaming.js b/prime-gaming.js index 4075062..7405f91 100644 --- a/prime-gaming.js +++ b/prime-gaming.js @@ -1,7 +1,7 @@ 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 } from './util.js'; +import { jsonDb, datetime, stealth, filenamify, prompt, notify, html_game_list, handleSIGINT } from './util.js'; import { cfg } from './config.js'; // const URL_LOGIN = 'https://www.amazon.de/ap/signin'; // wrong. needs some session args to be valid? @@ -12,11 +12,7 @@ console.log(datetime(), 'started checking prime-gaming'); const db = await jsonDb('prime-gaming.json'); db.data ||= {}; -let exit = false; -process.on('SIGINT', () => { // e.g. when killed by Ctrl-C - console.log('\nInterrupted by SIGINT. Exit! Exception shows where the script was:\n'); - exit = true; -}); +handleSIGINT(); // https://playwright.dev/docs/auth#multi-factor-authentication const context = await firefox.launchPersistentContext(cfg.dir.browser, { @@ -170,8 +166,8 @@ try { await page.locator(games_sel).screenshot({ path: p }); } catch (error) { console.error(error); // .toString()? - process.exitCode = 1; - if (error.message && !exit) + process.exitCode ||= 1; + if (error.message && process.exitCode != 130) notify(`prime-gaming failed: ${error.message.split('\n')[0]}`); } finally { await db.write(); // write out json db diff --git a/util.js b/util.js index cd05832..1666a58 100644 --- a/util.js +++ b/util.js @@ -24,6 +24,10 @@ export const datetime = (d = new Date()) => d.toISOString().replace('T', ' ').re export const datetimeLocal = (d = new Date()) => datetime(new Date(d.getTime() - new Date().getTimezoneOffset() * 60000)); export const filenamify = s => s.replaceAll(':', '.').replace(/[^a-z0-9 _\-.]/gi, '_'); // alternative: https://www.npmjs.com/package/filenamify - On Unix-like systems, / is reserved. On Windows, <>:"/\|?* along with trailing periods are reserved. +export const handleSIGINT = () => process.on('SIGINT', () => { // e.g. when killed by Ctrl-C + console.error('\nInterrupted by SIGINT. Exit!'); // Exception shows where the script was:\n'); // killed before catch in docker... + process.exitCode = 130; // 128+SIGINT to indicate to parent that process was killed +}); // stealth with playwright: https://github.com/berstend/puppeteer-extra/issues/454#issuecomment-917437212 // gets userAgent and then removes "Headless" from it