use lowdb for data/prime-gaming.json

This commit is contained in:
Ralf Vogler 2022-07-13 15:49:03 +02:00
parent 80e2a693d7
commit 18de5fdfa8
4 changed files with 112 additions and 25 deletions

43
package-lock.json generated
View file

@ -7,6 +7,7 @@
"devDependencies": {
"@playwright/test": "^1.20.1",
"cross-env": "^7.0.3",
"lowdb": "^3.0.0",
"playwright": "^1.20.1",
"puppeteer-extra-plugin-stealth": "^2.9.0"
}
@ -2031,6 +2032,21 @@
"node": ">=8"
}
},
"node_modules/lowdb": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/lowdb/-/lowdb-3.0.0.tgz",
"integrity": "sha512-9KZRulmIcU8fZuWiaM0d5e2/nPnrFyXkeXVpqT+MJS+vgbgOf1EbtvgQmba8HwUFgDl1oeZR6XqEJnkJmQdKmg==",
"dev": true,
"dependencies": {
"steno": "^2.1.0"
},
"engines": {
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
},
"funding": {
"url": "https://github.com/sponsors/typicode"
}
},
"node_modules/merge-deep": {
"version": "3.0.3",
"dev": true,
@ -2759,6 +2775,18 @@
"node": ">=8"
}
},
"node_modules/steno": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/steno/-/steno-2.1.0.tgz",
"integrity": "sha512-mauOsiaqTNGFkWqIfwcm3y/fq+qKKaIWf1vf3ocOuTdco9XoHCO2AGF1gFYXuZFSWuP38Q8LBHBGJv2KnJSXyA==",
"dev": true,
"engines": {
"node": "^14.13.1 || >=16.0.0"
},
"funding": {
"url": "https://github.com/sponsors/typicode"
}
},
"node_modules/string_decoder": {
"version": "1.3.0",
"dev": true,
@ -4323,6 +4351,15 @@
"p-locate": "^4.1.0"
}
},
"lowdb": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/lowdb/-/lowdb-3.0.0.tgz",
"integrity": "sha512-9KZRulmIcU8fZuWiaM0d5e2/nPnrFyXkeXVpqT+MJS+vgbgOf1EbtvgQmba8HwUFgDl1oeZR6XqEJnkJmQdKmg==",
"dev": true,
"requires": {
"steno": "^2.1.0"
}
},
"merge-deep": {
"version": "3.0.3",
"dev": true,
@ -4813,6 +4850,12 @@
}
}
},
"steno": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/steno/-/steno-2.1.0.tgz",
"integrity": "sha512-mauOsiaqTNGFkWqIfwcm3y/fq+qKKaIWf1vf3ocOuTdco9XoHCO2AGF1gFYXuZFSWuP38Q8LBHBGJv2KnJSXyA==",
"dev": true
},
"string_decoder": {
"version": "1.3.0",
"dev": true,

View file

@ -8,6 +8,7 @@
"devDependencies": {
"@playwright/test": "^1.20.1",
"cross-env": "^7.0.3",
"lowdb": "^3.0.0",
"playwright": "^1.20.1",
"puppeteer-extra-plugin-stealth": "^2.9.0"
},

View file

@ -1,6 +1,6 @@
import { chromium } from 'playwright'; // stealth plugin needs no outdated playwright-extra
import path from 'path';
import { dirs, stealth } from './util.js';
import { dirs, jsonDb, datetime, stealth } from './util.js';
const debug = process.env.PWDEBUG == '1'; // runs headful and opens https://playwright.dev/docs/inspector
const show = process.argv.includes('show', 2);
@ -10,6 +10,17 @@ const headless = !debug && !show;
const URL_CLAIM = 'https://gaming.amazon.com/home';
const TIMEOUT = 20 * 1000; // 20s, default is 30s
const db = await jsonDb('prime-gaming.json');
db.data ||= { claimed: [], runs: [] };
const run = {
startTime: datetime(),
endTime: null,
n_internal: null, // unclaimed games at beginning
c_internal: 0, // claimed games at end
n_external: null,
c_external: 0,
};
// https://playwright.dev/docs/auth#multi-factor-authentication
const context = await chromium.launchPersistentContext(dirs.browser, {
// channel: 'chrome', // https://playwright.dev/docs/browsers#google-chrome--microsoft-edge, chrome will not work on arm64 linux, only chromium which is the default
@ -25,13 +36,14 @@ if (!debug) context.setDefaultTimeout(TIMEOUT);
// const page = /* context.pages().length ? context.pages[0] : */ await context.newPage();
const page = context.pages()[0];
console.log('userAgent:', await page.evaluate(() => navigator.userAgent));
console.debug('userAgent:', await page.evaluate(() => navigator.userAgent));
const clickIfExists = async selector => {
if (await page.locator(selector).count() > 0)
await page.click(selector);
};
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:
await Promise.any(['button:has-text("Sign in")', '[data-a-target="user-dropdown-first-name-text"]'].map(s => page.waitForSelector(s)));
@ -54,8 +66,8 @@ const games_sel = 'div[data-a-target="offer-list-FGWP_FULL"]';
await page.waitForSelector(games_sel);
console.log('Number of already claimed games (total):', await page.locator(`${games_sel} p:has-text("Collected")`).count());
const game_sel = `${games_sel} [data-a-target="item-card"]:has-text("Claim game")`;
const n = await page.locator(game_sel).count();
console.log('Number of free unclaimed games (Prime Gaming):', n);
run.n_internal = await page.locator(game_sel).count();
console.log('Number of free unclaimed games (Prime Gaming):', run.n_internal);
const games = await page.$$(game_sel);
// for (let i=1; i<=n; i++) {
for (const card of games) {
@ -64,6 +76,8 @@ for (const card of games) {
const title = await (await card.$('.item-card-details__body__primary')).innerText();
console.log('Current free game:', title);
await (await card.$('button:has-text("Claim game")')).click();
db.data.claimed.push({title, time: datetime(), store: 'internal'});
run.c_internal++;
// const img = await (await card.$('img.tw-image')).getAttribute('src');
// console.log('Image:', img);
const p = path.resolve(dirs.screenshots, 'prime-gaming', 'internal', `${title.replace(/[^a-z0-9]/gi, '_')}.png`);
@ -72,9 +86,11 @@ for (const card of games) {
}
// claim games in linked stores. Origin: key, Epic Games Store: linked
{
let n;
const game_sel = `${games_sel} [data-a-target="item-card"]:has(p:text-is("Claim"))`;
do {
let n = await page.locator(game_sel).count();
n = await page.locator(game_sel).count();
run.n_external ||= n;
console.log('Number of free unclaimed games (external stores):', n);
const card = await page.$(game_sel);
if (!card) break;
@ -82,35 +98,52 @@ for (const card of games) {
console.log('Current free game:', title);
await (await card.$('text=Claim')).click();
// await page.waitForNavigation();
await page.click('button:has-text("Claim now")'); // waits for navigation
await Promise.any([page.click('button:has-text("Claim now")'), page.click('button:has-text("Complete Claim")')]); // waits for navigation
const store_text = await (await page.$('[data-a-target="hero-header-subtitle"]')).innerText();
// FULL GAME FOR PC ON: GOG.COM, ORIGIN, LEGACY GAMES, EPIC GAMES
// 3 Full PC Games on Legacy Games
const store = store_text.toLowerCase().replace(/.* on /, '');
console.log('External store:', store);
// print code if external store is not connected
if(await page.locator('div:has-text("Link game account")').count()) {
console.error('Account linking is required to claim this offer!');
} else {
// print code if there is one
const redeem = {
'origin': 'https://www.origin.com/redeem',
// 'origin': 'https://www.origin.com/redeem', // TODO still needed or now only via account linking?
'gog.com': 'https://www.gog.com/redeem',
'legacy games': 'https://www.legacygames.com/primedeal',
};
if (store in redeem) {
const code = await page.inputValue('input[type="text"]');
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"]');
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.claimed.push({title, time: datetime(), store, code});
// save screenshot of potential code just in case
const p = path.resolve(dirs.screenshots, 'prime-gaming', 'external', `${title.replace(/[^a-z0-9]/gi, '_')}.png`);
await page.screenshot({ path: p, fullPage: true });
console.info('Saved a screenshot of page to', p);
run.c_external++;
}
// await page.pause();
await page.goto(URL_CLAIM, {waitUntil: 'domcontentloaded'});
await page.click('button[data-type="Game"]');
} while (n);
const p = path.resolve(dirs.screenshots, 'prime-gaming', `${new Date().toISOString()}.png`);
const p = path.resolve(dirs.screenshots, 'prime-gaming', `${datetime()}.png`);
await page.screenshot({ path: p, fullPage: true });
}
await context.close();
} catch(error) {
console.error(error);
run.error = error.toString();
} finally {
// write out json db
run.endTime = datetime();
db.data.runs.push(run);
await db.write(); // TODO try-finally to always write out any updates
await context.close();
}

10
util.js
View file

@ -7,10 +7,20 @@ 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
const dataDir = s => path.resolve(__dirname, 'data', s);
export const dirs = {
data: dataDir('.'),
browser: dataDir('browser'),
screenshots: dataDir('screenshots'),
};
import { Low, JSONFile } from 'lowdb';
export const jsonDb = async file => {
const db = new Low(new JSONFile(dataDir(file)));
await db.read();
return db;
}
export const datetime = (d = new Date()) => d.toISOString();
// stealth with playwright: https://github.com/berstend/puppeteer-extra/issues/454#issuecomment-917437212
const newStealthContext = async (browser, contextOptions = {}, debug = false) => {
if (!debug) { // only need to fix userAgent in headless mode