indent prime-gaming

This commit is contained in:
Ralf Vogler 2022-07-13 15:57:04 +02:00
parent 18de5fdfa8
commit 02a49a5a19

View file

@ -44,55 +44,54 @@ const clickIfExists = async selector => {
}; };
try { try {
await page.goto(URL_CLAIM, {waitUntil: 'domcontentloaded'}); // default 'load' takes forever 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: // 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))); await Promise.any(['button:has-text("Sign in")', '[data-a-target="user-dropdown-first-name-text"]'].map(s => page.waitForSelector(s)));
await clickIfExists('[aria-label="Cookies usage disclaimer banner"] button:has-text("Accept Cookies")'); // to not waste screen space in --debug await clickIfExists('[aria-label="Cookies usage disclaimer banner"] button:has-text("Accept Cookies")'); // to not waste screen space in --debug
while (await page.locator('button:has-text("Sign in")').count() > 0) { while (await page.locator('button:has-text("Sign in")').count() > 0) {
console.error('Not signed in anymore.'); console.error('Not signed in anymore.');
if (headless) { if (headless) {
console.log('Please run `node prime-gaming show` to login in the opened browser.'); console.log('Please run `node prime-gaming show` to login in the opened browser.');
await context.close(); // not needed? await context.close(); // not needed?
process.exit(1); process.exit(1);
}
await page.click('button:has-text("Sign in")');
if (!debug) context.setDefaultTimeout(0); // give user time to log in without timeout
await page.waitForNavigation({url: 'https://gaming.amazon.com/home?signedIn=true'});
if (!debug) context.setDefaultTimeout(TIMEOUT);
} }
await page.click('button:has-text("Sign in")'); console.log('Signed in.');
if (!debug) context.setDefaultTimeout(0); // give user time to log in without timeout await page.click('button[data-type="Game"]');
await page.waitForNavigation({url: 'https://gaming.amazon.com/home?signedIn=true'}); const games_sel = 'div[data-a-target="offer-list-FGWP_FULL"]';
if (!debug) context.setDefaultTimeout(TIMEOUT); await page.waitForSelector(games_sel);
} console.log('Number of already claimed games (total):', await page.locator(`${games_sel} p:has-text("Collected")`).count());
console.log('Signed in.'); const game_sel = `${games_sel} [data-a-target="item-card"]:has-text("Claim game")`;
await page.click('button[data-type="Game"]'); run.n_internal = await page.locator(game_sel).count();
const games_sel = 'div[data-a-target="offer-list-FGWP_FULL"]'; console.log('Number of free unclaimed games (Prime Gaming):', run.n_internal);
await page.waitForSelector(games_sel); const games = await page.$$(game_sel);
console.log('Number of already claimed games (total):', await page.locator(`${games_sel} p:has-text("Collected")`).count()); // for (let i=1; i<=n; i++) {
const game_sel = `${games_sel} [data-a-target="item-card"]:has-text("Claim game")`; for (const card of games) {
run.n_internal = await page.locator(game_sel).count(); // const card = page.locator(`:nth-match(${game_sel}, ${i})`); // this will reevaluate after games are claimed and index will be wrong
console.log('Number of free unclaimed games (Prime Gaming):', run.n_internal); // const title = await card.locator('h3').first().innerText();
const games = await page.$$(game_sel); const title = await (await card.$('.item-card-details__body__primary')).innerText();
// for (let i=1; i<=n; i++) { console.log('Current free game:', title);
for (const card of games) { await (await card.$('button:has-text("Claim game")')).click();
// const card = page.locator(`:nth-match(${game_sel}, ${i})`); // this will reevaluate after games are claimed and index will be wrong db.data.claimed.push({title, time: datetime(), store: 'internal'});
// const title = await card.locator('h3').first().innerText(); run.c_internal++;
const title = await (await card.$('.item-card-details__body__primary')).innerText(); // const img = await (await card.$('img.tw-image')).getAttribute('src');
console.log('Current free game:', title); // console.log('Image:', img);
await (await card.$('button:has-text("Claim game")')).click(); const p = path.resolve(dirs.screenshots, 'prime-gaming', 'internal', `${title.replace(/[^a-z0-9]/gi, '_')}.png`);
db.data.claimed.push({title, time: datetime(), store: 'internal'}); await card.screenshot({ path: p });
run.c_internal++; // await page.pause();
// const img = await (await card.$('img.tw-image')).getAttribute('src'); }
// console.log('Image:', img); // claim games in external/linked stores. Linked: origin.com, epicgames.com; Redeem-key: gog.com, legacygames.com
const p = path.resolve(dirs.screenshots, 'prime-gaming', 'internal', `${title.replace(/[^a-z0-9]/gi, '_')}.png`);
await card.screenshot({ path: p });
// await page.pause();
}
// claim games in linked stores. Origin: key, Epic Games Store: linked
{
let n; let n;
const game_sel = `${games_sel} [data-a-target="item-card"]:has(p:text-is("Claim"))`; const game_sel_ext = `${games_sel} [data-a-target="item-card"]:has(p:text-is("Claim"))`;
do { do {
n = await page.locator(game_sel).count(); n = await page.locator(game_sel_ext).count();
run.n_external ||= n; run.n_external ||= n;
console.log('Number of free unclaimed games (external stores):', n); console.log('Number of free unclaimed games (external stores):', n);
const card = await page.$(game_sel); const card = await page.$(game_sel_ext);
if (!card) break; if (!card) break;
const title = await (await card.$('.item-card-details__body__primary')).innerText(); const title = await (await card.$('.item-card-details__body__primary')).innerText();
console.log('Current free game:', title); console.log('Current free game:', title);
@ -135,7 +134,6 @@ for (const card of games) {
} while (n); } while (n);
const p = path.resolve(dirs.screenshots, 'prime-gaming', `${datetime()}.png`); const p = path.resolve(dirs.screenshots, 'prime-gaming', `${datetime()}.png`);
await page.screenshot({ path: p, fullPage: true }); await page.screenshot({ path: p, fullPage: true });
}
} catch(error) { } catch(error) {
console.error(error); console.error(error);
run.error = error.toString(); run.error = error.toString();