Ralf Vogler 2023-02-22 10:26:32 +01:00
parent 21ed2794f4
commit 38c5402df0
4 changed files with 16 additions and 24 deletions

View file

@ -2,7 +2,7 @@ import { firefox } from 'playwright-firefox'; // stealth plugin needs no outdate
import { authenticator } from 'otplib'; import { authenticator } from 'otplib';
import path from 'path'; import path from 'path';
import { existsSync, writeFileSync } from 'fs'; 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'; import { cfg } from './config.js';
const URL_CLAIM = 'https://store.epicgames.com/en-US/free-games'; 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'); const db = await jsonDb('epic-games.json');
db.data ||= {}; db.data ||= {};
let exit = false; handleSIGINT();
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;
});
// https://www.nopecha.com extension source from https://github.com/NopeCHA/NopeCHA/releases/tag/0.1.16 // 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 // const ext = path.resolve('nopecha'); // used in Chromium, currently not needed in Firefox
@ -193,8 +189,8 @@ try {
} }
} catch (error) { } catch (error) {
console.error(error); // .toString()? console.error(error); // .toString()?
process.exitCode = 1; process.exitCode ||= 1;
if (error.message && !exit) if (error.message && process.exitCode != 130)
notify(`epic-games failed: ${error.message.split('\n')[0]}`); notify(`epic-games failed: ${error.message.split('\n')[0]}`);
} finally { } finally {
await db.write(); // write out json db await db.write(); // write out json db

12
gog.js
View file

@ -1,6 +1,6 @@
import { firefox } from 'playwright-firefox'; // stealth plugin needs no outdated playwright-extra import { firefox } from 'playwright-firefox'; // stealth plugin needs no outdated playwright-extra
import path from 'path'; 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'; import { cfg } from './config.js';
const URL_CLAIM = 'https://www.gog.com/en'; const URL_CLAIM = 'https://www.gog.com/en';
@ -10,11 +10,7 @@ console.log(datetime(), 'started checking gog');
const db = await jsonDb('gog.json'); const db = await jsonDb('gog.json');
db.data ||= {}; db.data ||= {};
let exit = false; handleSIGINT();
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;
});
// https://playwright.dev/docs/auth#multi-factor-authentication // https://playwright.dev/docs/auth#multi-factor-authentication
const context = await firefox.launchPersistentContext(cfg.dir.browser, { const context = await firefox.launchPersistentContext(cfg.dir.browser, {
@ -138,8 +134,8 @@ try {
} }
} catch (error) { } catch (error) {
console.error(error); // .toString()? console.error(error); // .toString()?
process.exitCode = 1; process.exitCode ||= 1;
if (error.message && !exit) if (error.message && process.exitCode != 130)
notify(`gog failed: ${error.message.split('\n')[0]}`); notify(`gog failed: ${error.message.split('\n')[0]}`);
} finally { } finally {
await db.write(); // write out json db await db.write(); // write out json db

View file

@ -1,7 +1,7 @@
import { firefox } from 'playwright-firefox'; // stealth plugin needs no outdated playwright-extra import { firefox } from 'playwright-firefox'; // stealth plugin needs no outdated playwright-extra
import { authenticator } from 'otplib'; import { authenticator } from 'otplib';
import path from 'path'; 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'; import { cfg } from './config.js';
// const URL_LOGIN = 'https://www.amazon.de/ap/signin'; // wrong. needs some session args to be valid? // 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'); const db = await jsonDb('prime-gaming.json');
db.data ||= {}; db.data ||= {};
let exit = false; handleSIGINT();
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;
});
// https://playwright.dev/docs/auth#multi-factor-authentication // https://playwright.dev/docs/auth#multi-factor-authentication
const context = await firefox.launchPersistentContext(cfg.dir.browser, { const context = await firefox.launchPersistentContext(cfg.dir.browser, {
@ -170,8 +166,8 @@ try {
await page.locator(games_sel).screenshot({ path: p }); await page.locator(games_sel).screenshot({ path: p });
} catch (error) { } catch (error) {
console.error(error); // .toString()? console.error(error); // .toString()?
process.exitCode = 1; process.exitCode ||= 1;
if (error.message && !exit) if (error.message && process.exitCode != 130)
notify(`prime-gaming failed: ${error.message.split('\n')[0]}`); notify(`prime-gaming failed: ${error.message.split('\n')[0]}`);
} finally { } finally {
await db.write(); // write out json db await db.write(); // write out json db

View file

@ -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 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 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 // stealth with playwright: https://github.com/berstend/puppeteer-extra/issues/454#issuecomment-917437212
// gets userAgent and then removes "Headless" from it // gets userAgent and then removes "Headless" from it