Merge pull request #49 from vogler/notifications/apprise

Notifications via apprise
This commit is contained in:
Ralf Vogler 2023-01-25 19:03:12 +01:00 committed by GitHub
commit af46be3a52
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 87 additions and 17 deletions

View file

@ -9,7 +9,7 @@ SHELL ["/bin/bash", "-o", "pipefail", "-c"]
ARG DEBIAN_FRONTEND=noninteractive
ENV PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD true
# Install up-to-date node & npm, deps for virtual screen & noVNC, browser.
# Install up-to-date node & npm, deps for virtual screen & noVNC, browser, pip for apprise.
# Playwright needs --with-deps for firefox.
RUN apt-get update \
&& apt-get install -y curl \
@ -22,6 +22,7 @@ RUN apt-get update \
tini \
novnc websockify \
dos2unix \
python3-pip \
&& npx playwright install --with-deps firefox \
&& apt-get clean \
&& rm -rf \
@ -32,6 +33,7 @@ RUN apt-get update \
/var/tmp/*
RUN ln -s /usr/share/novnc/vnc_auto.html /usr/share/novnc/index.html
RUN pip install apprise
WORKDIR /fgc
COPY package*.json ./

View file

@ -30,6 +30,7 @@ Data is stored in the volume `fgc`.
1. [Install Node.js](https://nodejs.org/en/download)
2. Clone/download this repository and `cd` into it in a terminal
3. Run `npm install && npx playwright install firefox`
4. Run `pip install apprise` to install [apprise](https://github.com/caronc/apprise) if you want notifications
This downloads Firefox to a cache in home ([doc](https://playwright.dev/docs/browsers#managing-browser-binaries)).
If you are missing some dependencies for the browser on your system, you can use `sudo npx playwright install firefox --with-deps`.
@ -63,6 +64,7 @@ Available options/variables and their default values:
| WIDTH | 1280 | Width of the opened browser (and screen vor VNC in Docker). |
| HEIGHT | 1280 | Height of the opened browser (and screen vor VNC in Docker). |
| VNC_PASSWORD | | VNC password for Docker. No password used by default! |
| NOTIFY | | Notification services to use (Pushover, Slack, Telegram...), see below. |
| EMAIL | | Default email for any login. |
| PASSWORD | | Default password for any login. |
| EG_EMAIL | | Epic Games email for login. Overrides EMAIL. |
@ -80,6 +82,12 @@ See `config.js` for all options.
On Linux/macOS you can prefix the variables you want to set, for example `EMAIL=foo@bar.baz SHOW=1 node epic-games` will show the browser and skip asking you for your login email.
For Docker you can pass variables using `-e VAR=VAL`, for example `docker run -e EMAIL=foo@bar.baz ...` or using `--env-file` (see [docs](https://docs.docker.com/engine/reference/commandline/run/#set-environment-variables--e---env---env-file)). If you are using [docker compose](https://docs.docker.com/compose/environment-variables/), you can put them in the `environment:` section.
### Notifications
The scripts will try to send notifications for successfully claimed games and any errors like needing to log in or encountered captchas (should not happen).
[apprise](https://github.com/caronc/apprise) is used for notifications and offers many services including Pushover, Slack, Telegram, SMS, Email, desktop and custom notifications.
You just need to set `NOTIFY` to the notifications services you want to use, e.g. `NOTIFY='mailto://myemail:mypass@gmail.com' 'pbul://o.gn5kj6nfhv736I7jC3cj3QLRiyhgl98b'` - refer to their list of services and [examples](https://github.com/caronc/apprise#command-line-usage).
### Automatic login, two-factor authentication
If you set the options for email, password and OTP key, there will be no prompts and logins should happen automatically. This is optional since all stores should stay logged in since cookies are refreshed.
To get the OTP key, it is easiest to follow the store's guide for adding an authenticator app. You should also scan the shown QR code with your favorite app to have an alternative method for 2FA.
@ -166,6 +174,6 @@ Added OTP generation via otplib for automatic login, even with 2FA.
---
Logo with smaller aspect ratio (for Telegram bot etc.):
Logo with smaller aspect ratio (for Telegram bot etc.): 👾 - [emojipedia](https://emojipedia.org/alien-monster/)
![logo-fgc](https://user-images.githubusercontent.com/493741/214589922-093d6557-6393-421c-b577-da58ff3671bc.png)

View file

@ -11,6 +11,7 @@ export const cfg = {
height: Number(process.env.HEIGHT) || 1280, // height of the opened browser
timeout: (Number(process.env.TIMEOUT) || 20) * 1000, // 20s, default for playwright is 30s
novnc_port: process.env.NOVNC_PORT, // running in docker if set
notify: process.env.NOTIFY, // apprise notification services
// auth epic-games
eg_email: process.env.EG_EMAIL || process.env.EMAIL,
eg_password: process.env.EG_PASSWORD || process.env.PASSWORD,

View file

@ -2,7 +2,7 @@ import { firefox } from 'playwright'; // stealth plugin needs no outdated playwr
import { authenticator } from 'otplib';
import path from 'path';
import { existsSync, writeFileSync } from 'fs';
import { dirs, jsonDb, datetime, stealth, filenamify } from './util.js';
import { dirs, jsonDb, datetime, stealth, filenamify, notify } from './util.js';
import { cfg } from './config.js';
import prompts from 'prompts'; // alternatives: enquirer, inquirer
@ -59,6 +59,8 @@ if (!cfg.debug) context.setDefaultTimeout(cfg.timeout);
const page = context.pages().length ? context.pages()[0] : await context.newPage(); // should always exist
// console.debug('userAgent:', await page.evaluate(() => navigator.userAgent));
const notify_games = [];
try {
await context.addCookies([{name: 'OptanonAlertBoxClosed', value: new Date(Date.now() - 5*24*60*60*1000).toISOString(), domain: '.epicgames.com', path: '/'}]); // Accept cookies to get rid of banner to save space on screen. Set accept time to 5 days ago.
@ -82,6 +84,7 @@ try {
await page.click('button[type="submit"]');
page.waitForSelector('#h_captcha_challenge_login_prod iframe').then(() => {
console.log('Got a captcha! You may have to solve it in the browser if the NopeCHA extension fails to do so.');
notify('epic-games: got captcha during login. Please check.');
}).catch(_ => { });
// handle MFA, but don't await it
page.waitForNavigation({ url: '**/id/login/mfa**'}).then(async () => {
@ -93,6 +96,7 @@ try {
}).catch(_ => { });
} else {
console.log('Waiting for you to login in the browser.');
notify('epic-games: no longer signed in and not enough options set for automatic login.');
}
await page.waitForNavigation({ url: URL_CLAIM });
context.setDefaultTimeout(cfg.timeout);
@ -127,6 +131,8 @@ try {
const game_id = page.url().split('/').pop();
db.data[user][game_id] ||= { title, time: datetime(), url: page.url() }; // this will be set on the initial run only!
console.log('Current free game:', title);
const notify_game = {title, url, status: 'failed'};
notify_games.push(notify_game); // status is updated below
if (btnText.toLowerCase() == 'in library') {
console.log(' Already in library! Nothing to claim.');
@ -148,6 +154,7 @@ try {
// skip game if unavailable in region, https://github.com/vogler/free-games-claimer/issues/46 TODO check games for account's region
if (await iframe.locator(':has-text("unavailable in your region")').count() > 0) {
console.error(' This product is unavailable in your region!');
db.data[user][game_id].status = notify_game.status = 'unavailable-in-region';
continue;
}
await iframe.locator('button:has-text("Place Order")').click();
@ -169,7 +176,7 @@ try {
}).catch(_ => { }); // may time out if not shown
await page.waitForSelector('text=Thank you for buying'); // EU: wait, non-EU: wait again = no-op
db.data[user][game_id].status = 'claimed';
db.data[user][game_id].time = datetime(); // claimed time overwrites failed time
db.data[user][game_id].time = datetime(); // claimed time overwrites failed/dryrun time
console.log(' Claimed successfully!');
context.setDefaultTimeout(cfg.timeout);
} catch (e) {
@ -183,11 +190,18 @@ try {
const p = path.resolve(dirs.screenshots, 'epic-games', `${game_id}.png`);
if (!existsSync(p)) await page.screenshot({ path: p, fullPage: false }); // fullPage is quite long...
}
notify_game.status = db.data[user][game_id].status;
}
} catch (error) {
console.error(error); // .toString()?
if (error.message && !error.message.contains('Target closed')) // e.g. when killed by Ctrl-C
notify(`epic-games failed: ${error.message}`);
} finally {
await db.write(); // write out json db
if (notify_games.filter(g => g.status != 'existed').length) { // don't notify if all were already claimed; TODO don't notify if killed?
const list = notify_games.map(g => `- <a href="${g.url}">${g.title}</a> (${g.status})<br>`);
notify(`epic-games:<br>${list}`);
}
}
await writeFileSync(path.resolve(dirs.browser, 'cookies.json'), JSON.stringify(await context.cookies()));
await context.close();

14
gog.js
View file

@ -1,6 +1,6 @@
import { firefox } from 'playwright'; // stealth plugin needs no outdated playwright-extra
import path from 'path';
import { dirs, jsonDb, datetime, filenamify } from './util.js';
import { dirs, jsonDb, datetime, filenamify, notify } from './util.js';
import { cfg } from './config.js';
import prompts from 'prompts'; // alternatives: enquirer, inquirer
@ -29,6 +29,8 @@ if (!cfg.debug) context.setDefaultTimeout(cfg.timeout);
const page = context.pages().length ? context.pages()[0] : await context.newPage(); // should always exist
// console.debug('userAgent:', await page.evaluate(() => navigator.userAgent));
const notify_games = [];
try {
await context.addCookies([{name: 'CookieConsent', value: '{stamp:%274oR8MJL+bxVlG6g+kl2we5+suMJ+Tv7I4C5d4k+YY4vrnhCD+P23RQ==%27%2Cnecessary:true%2Cpreferences:true%2Cstatistics:true%2Cmarketing:true%2Cmethod:%27explicit%27%2Cver:1%2Cutc:1672331618201%2Cregion:%27de%27}', domain: 'www.gog.com', path: '/'}]); // to not waste screen space when non-headless
@ -61,12 +63,13 @@ try {
await page.waitForTimeout(1000); // TODO wait for something else below?
});
} else {
console.log('Waiting for you to login in the browser.');
notify('gog: no longer signed in and not enough options set for automatic login.');
if (cfg.headless) {
console.log('Please run `node gog show` to login in the opened browser.');
await context.close(); // not needed?
process.exit(1);
}
console.log('Waiting for you to login in the browser.');
}
// await page.waitForNavigation(); // TODO was blocking
if (!cfg.debug) context.setDefaultTimeout(cfg.timeout);
@ -111,6 +114,7 @@ try {
}
}
db.data[user][title].status ||= status;
notify_games.push({ title, url, status });
console.log("Unsubscribe from 'Promotions and hot deals' newsletter");
await page.goto('https://www.gog.com/en/account/settings/subscriptions');
@ -118,7 +122,13 @@ try {
}
} catch (error) {
console.error(error); // .toString()?
if (!error.message.contains('Target closed')) // e.g. when killed by Ctrl-C
notify(`prime-gaming failed: ${error.message}`);
} finally {
await db.write(); // write out json db
if (notify_games.filter(g => g.status != 'existed').length) { // don't notify if all were already claimed; TODO don't notify if killed?
const list = notify_games.map(g => `- <a href="${g.url}">${g.title}</a> (${g.status})<br>`);
notify(`gog:<br>${list}`);
}
}
await context.close();

View file

@ -1,7 +1,7 @@
import { firefox } from 'playwright'; // stealth plugin needs no outdated playwright-extra
import { authenticator } from 'otplib';
import path from 'path';
import { dirs, jsonDb, datetime, stealth, filenamify } from './util.js';
import { dirs, jsonDb, datetime, stealth, filenamify, notify } from './util.js';
import { cfg } from './config.js';
import prompts from 'prompts'; // alternatives: enquirer, inquirer
@ -41,6 +41,8 @@ if (!cfg.debug) context.setDefaultTimeout(cfg.timeout);
const page = context.pages().length ? context.pages()[0] : await context.newPage(); // should always exist
// console.debug('userAgent:', await page.evaluate(() => navigator.userAgent));
const notify_games = [];
try {
await page.goto(URL_CLAIM, { waitUntil: 'domcontentloaded' }); // default 'load' takes forever
// need to wait for some elements to exist before checking if signed in or accepting cookies:
@ -60,7 +62,7 @@ try {
await page.click('input[type="submit"]');
page.waitForNavigation({ url: '**/ap/signin**'}).then(async () => { // TODO check for wrong credentials
console.error(await page.locator('.a-alert-content').first().innerText());
}).catch(_ => { });
});
// handle MFA, but don't await it
page.waitForNavigation({ url: '**/ap/mfa**'}).then(async () => {
console.log('Two-Step Verification - enter the One Time Password (OTP), e.g. generated by your Authenticator App');
@ -70,12 +72,13 @@ try {
await page.click('input[type="submit"]');
}).catch(_ => { });
} else {
console.log('Waiting for you to login in the browser.');
notify('prime-gaming: no longer signed in and not enough options set for automatic login.');
if (cfg.headless) {
console.log('Please run `node prime-gaming show` to login in the opened browser.');
await context.close(); // not needed?
process.exit(1);
}
console.log('Waiting for you to login in the browser.');
}
await page.waitForNavigation({ url: 'https://gaming.amazon.com/home?signedIn=true' });
if (!cfg.debug) context.setDefaultTimeout(cfg.timeout);
@ -108,6 +111,7 @@ try {
await card.screenshot({ path: p });
await (await card.$('button:has-text("Claim game")')).click();
db.data[user][title] ||= { title, time: datetime(), store: 'internal' };
notify_games.push({ title, status: 'claimed', url: URL_CLAIM });
// await page.pause();
}
// claim games in external/linked stores. Linked: origin.com, epicgames.com; Redeem-key: gog.com, legacygames.com, microsoft
@ -129,6 +133,10 @@ try {
// 3 Full PC Games on Legacy Games
const store = store_text.toLowerCase().replace(/.* on /, '');
console.log(' External store:', store);
const url = page.url().split('?')[0];
db.data[user][title] ||= { title, time: datetime(), url, store };
const notify_game = {title, url, status: `failed - link ${store}`};
notify_games.push(notify_game); // status is updated below
if (await page.locator('div:has-text("Link game account")').count()) {
console.error(' Account linking is required to claim this offer!');
} else {
@ -139,16 +147,18 @@ try {
'legacy games': 'https://www.legacygames.com/primedeal',
'microsoft games': 'https://redeem.microsoft.com',
};
let code;
if (store in redeem) { // did not work for linked origin: && !await page.locator('div:has-text("Successfully Claimed")').count()
code = await page.inputValue('input[type="text"]');
const code = await page.inputValue('input[type="text"]');
console.log(' Code to redeem game:', code);
if (store == 'legacy games') { // may be different URL like https://legacygames.com/primeday/puzzleoftheyear/
redeem[store] = await (await page.$('li:has-text("Click here") a')).getAttribute('href');
}
console.log(' URL to redeem game:', redeem[store]);
db.data[user][title].code = code;
notify_game.status = `<a href="${redeem[store]}">redeem</a> ${code} on ${store}`;
} else {
notify_game.status = `claimed on ${store}`;
}
db.data[user][title] ||= { title, time: datetime(), store, code, url: page.url() };
// save screenshot of potential code just in case
const p = path.resolve(dirs.screenshots, 'prime-gaming', 'external', `${filenamify(title)}.png`);
await page.screenshot({ path: p, fullPage: true });
@ -163,7 +173,13 @@ try {
await page.locator(games_sel).screenshot({ path: p });
} catch (error) {
console.error(error); // .toString()?
if (error.message && !error.message.contains('Target closed')) // e.g. when killed by Ctrl-C
notify(`prime-gaming failed: ${error.message}`);
} finally {
await db.write(); // write out json db
if (notify_games.length) { // list should only include claimed games
const list = notify_games.map(g => `- <a href="${g.url}">${g.title}</a> (${g.status})<br>`);
notify(`prime-gaming:<br>${list}`);
}
}
await context.close();

19
util.js
View file

@ -76,3 +76,22 @@ export const stealth = async (context) => {
await context.addInitScript(evasion.cb, evasion.a);
}
};
// notifications via apprise CLI
import { exec } from 'child_process';
import { cfg } from './config.js';
export const notify = (html) => {
if (!cfg.notify) return;
exec(`apprise ${cfg.notify} -i html -b '${html}'`, (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
if (error.message.includes('command not found')) {
console.info('Run `pip install apprise`. See https://github.com/vogler/free-games-claimer#notifications');
}
return;
}
if (stderr) console.error(`stderr: ${stderr}`);
if (stdout) console.log(`stdout: ${stdout}`);
});
}