-Fix notifications

-Add support for epic games bundles
This commit is contained in:
Jordy Mendoza 2024-04-30 17:26:24 -06:00
parent 0a3910171a
commit bab4c105f1
3 changed files with 26 additions and 13 deletions

View file

@ -158,14 +158,18 @@ try {
await page.click('button:has-text("Continue")', { delay: 111 }); await page.click('button:has-text("Continue")', { delay: 111 });
await page.waitForTimeout(2000); await page.waitForTimeout(2000);
} }
let title;
const title = await page.locator('h1').first().innerText(); if (url.includes("/bundles/")) {
title = (await page.locator('//span[@class][contains(text(),"Buy")]').first().innerText()).replace("Buy ","")
console.log('Current free bundle:', title);
} else {
title = await page.locator('h1').first().innerText();
console.log('Current free game:', title);
}
const game_id = page.url().split('/').pop(); 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! 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' }; const notify_game = { title, url, status: 'failed' };
notify_games.push(notify_game); // status is updated below notify_games.push(notify_game); // status is updated below
if (btnText.toLowerCase() == 'in library') { if (btnText.toLowerCase() == 'in library') {
console.log(' Already in library! Nothing to claim.'); console.log(' Already in library! Nothing to claim.');
notify_game.status = 'existed'; notify_game.status = 'existed';
@ -182,7 +186,6 @@ try {
} else { // GET } else { // GET
console.log(' Not in library yet! Click GET.'); console.log(' Not in library yet! Click GET.');
await page.click('[data-testid="purchase-cta-button"]', { delay: 11 }); // got stuck here without delay (or mouse move), see #75, 1ms was also enough await page.click('[data-testid="purchase-cta-button"]', { delay: 11 }); // got stuck here without delay (or mouse move), see #75, 1ms was also enough
// click Continue if 'Device not supported. This product is not compatible with your current device.' - avoided by Windows userAgent? // click Continue if 'Device not supported. This product is not compatible with your current device.' - avoided by Windows userAgent?
page.click('button:has-text("Continue")').catch(_ => { }); // needed since change from Chromium to Firefox? page.click('button:has-text("Continue")').catch(_ => { }); // needed since change from Chromium to Firefox?
@ -243,10 +246,16 @@ try {
// console.info(' Saved a screenshot of hcaptcha challenge to', p); // console.info(' Saved a screenshot of hcaptcha challenge to', p);
// console.error(' Got hcaptcha challenge. To avoid it, get a link from https://www.hcaptcha.com/accessibility'); // TODO save this link in config and visit it daily to set accessibility cookie to avoid captcha challenge? // console.error(' Got hcaptcha challenge. To avoid it, get a link from https://www.hcaptcha.com/accessibility'); // TODO save this link in config and visit it daily to set accessibility cookie to avoid captcha challenge?
}).catch(_ => { }); // may time out if not shown }).catch(_ => { }); // may time out if not shown
try {
await page.locator('text=Thanks for your order!').waitFor({ state: 'attached' }); await page.locator('text=Thanks for your order!').waitFor({ state: 'attached' });
db.data[user][game_id].status = 'claimed'; db.data[user][game_id].status = 'claimed';
db.data[user][game_id].time = datetime(); // claimed time overwrites failed/dryrun time db.data[user][game_id].time = datetime(); // claimed time overwrites failed/dryrun time
console.log(' Claimed successfully!'); console.log(' Claimed successfully!');
} catch (e) {
db.data[user][game_id].status = 'claimed?';
db.data[user][game_id].time = datetime(); // claimed time overwrites failed/dryrun time
console.log(' Claimed successfully?');
}
// context.setDefaultTimeout(cfg.timeout); // context.setDefaultTimeout(cfg.timeout);
} catch (e) { } catch (e) {
console.log(e); console.log(e);

View file

@ -144,9 +144,8 @@ try {
if (cfg.dryrun) continue; if (cfg.dryrun) continue;
if (cfg.interactive && !await confirm()) continue; if (cfg.interactive && !await confirm()) continue;
await Promise.any([page.click('[data-a-target="buy-box"] .tw-button:has-text("Get game")'), page.click('[data-a-target="buy-box"] .tw-button:has-text("Claim")'), page.click('.tw-button:has-text("Complete Claim")'), page.waitForSelector('div:has-text("Link game account")'), page.waitForSelector('.thank-you-title:has-text("Success")')]); // waits for navigation await Promise.any([page.click('[data-a-target="buy-box"] .tw-button:has-text("Get game")'), page.click('[data-a-target="buy-box"] .tw-button:has-text("Claim")'), page.click('.tw-button:has-text("Complete Claim")'), page.waitForSelector('div:has-text("Link game account")'), page.waitForSelector('.thank-you-title:has-text("Success")')]); // waits for navigation
db.data[user][title] ||= { title, time: datetime(), url, store }; db.data[user][title] ||= { title, time: datetime(), url, store, notified: false };
const notify_game = { title, url }; const notify_game = { title, url };
notify_games.push(notify_game); // status is updated below
if (await page.locator('div:has-text("Link game account")').count() // TODO still needed? epic games store just has 'Link account' as the button text now. if (await page.locator('div:has-text("Link game account")').count() // TODO still needed? epic games store just has 'Link account' as the button text now.
|| await page.locator('div:has-text("Link account")').count()) { || await page.locator('div:has-text("Link account")').count()) {
console.error(' Account linking is required to claim this offer!'); console.error(' Account linking is required to claim this offer!');
@ -167,6 +166,8 @@ try {
'legacy games': 'https://www.legacygames.com/primedeal', 'legacy games': 'https://www.legacygames.com/primedeal',
}; };
if (store in redeem) { // did not work for linked origin: && !await page.locator('div:has-text("Successfully Claimed")').count() if (store in redeem) { // did not work for linked origin: && !await page.locator('div:has-text("Successfully Claimed")').count()
notify_games.push(notify_game); // status is updated below
db.data[user][title].notified = true
const code = await Promise.any([page.inputValue('input[type="text"]'), page.textContent('[data-a-target="ClaimStateClaimCodeContent"]').then(s => s.replace('Your code: ', ''))]); // input: Legacy Games; text: gog.com const code = await Promise.any([page.inputValue('input[type="text"]'), page.textContent('[data-a-target="ClaimStateClaimCodeContent"]').then(s => s.replace('Your code: ', ''))]); // input: Legacy Games; text: gog.com
console.log(' Code to redeem game:', chalk.blue(code)); console.log(' Code to redeem game:', chalk.blue(code));
if (store == 'legacy games') { // may be different URL like https://legacygames.com/primeday/puzzleoftheyear/ if (store == 'legacy games') { // may be different URL like https://legacygames.com/primeday/puzzleoftheyear/
@ -268,6 +269,10 @@ try {
} else { } else {
notify_game.status = `claimed on ${store}`; notify_game.status = `claimed on ${store}`;
db.data[user][title].status = 'claimed'; db.data[user][title].status = 'claimed';
if (!db.data[user][title].notified) {
db.data[user][title].notified = true
notify_games.push(notify_game);
}
} }
// save screenshot of potential code just in case // save screenshot of potential code just in case
await page.screenshot({ path: screenshot('external', `${filenamify(title)}.png`), fullPage: true }); await page.screenshot({ path: screenshot('external', `${filenamify(title)}.png`), fullPage: true });

View file

@ -15,8 +15,7 @@ const URL_CLAIM = 'https://www.xbox.com/en-US/live/gold'; // #gameswithgold";
console.log(datetime(), 'started checking xbox'); console.log(datetime(), 'started checking xbox');
const db = await jsonDb('xbox.json'); const db = await jsonDb('xbox.json', {});
db.data ||= {};
handleSIGINT(); handleSIGINT();