From bab4c105f1e7a6c887df28d8cb93aeb3d1e97336 Mon Sep 17 00:00:00 2001 From: Jordy Mendoza Date: Tue, 30 Apr 2024 17:26:24 -0600 Subject: [PATCH] -Fix notifications -Add support for epic games bundles --- epic-games.js | 27 ++++++++++++++++++--------- prime-gaming.js | 9 +++++++-- xbox.js | 3 +-- 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/epic-games.js b/epic-games.js index 68a2dde..ecb71da 100644 --- a/epic-games.js +++ b/epic-games.js @@ -158,14 +158,18 @@ try { await page.click('button:has-text("Continue")', { delay: 111 }); await page.waitForTimeout(2000); } - - const title = await page.locator('h1').first().innerText(); + let title; + 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(); 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.'); notify_game.status = 'existed'; @@ -182,7 +186,6 @@ try { } else { // 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 - // 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? @@ -243,10 +246,16 @@ try { // 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? }).catch(_ => { }); // may time out if not shown - await page.locator('text=Thanks for your order!').waitFor({ state: 'attached' }); - db.data[user][game_id].status = 'claimed'; - db.data[user][game_id].time = datetime(); // claimed time overwrites failed/dryrun time - console.log(' Claimed successfully!'); + try { + await page.locator('text=Thanks for your order!').waitFor({ state: 'attached' }); + db.data[user][game_id].status = 'claimed'; + db.data[user][game_id].time = datetime(); // claimed time overwrites failed/dryrun time + 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); } catch (e) { console.log(e); diff --git a/prime-gaming.js b/prime-gaming.js index 572eb04..8a07a86 100644 --- a/prime-gaming.js +++ b/prime-gaming.js @@ -144,9 +144,8 @@ try { if (cfg.dryrun) 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 - 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 }; - 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. || await page.locator('div:has-text("Link account")').count()) { console.error(' Account linking is required to claim this offer!'); @@ -167,6 +166,8 @@ try { '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() + 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 console.log(' Code to redeem game:', chalk.blue(code)); if (store == 'legacy games') { // may be different URL like https://legacygames.com/primeday/puzzleoftheyear/ @@ -268,6 +269,10 @@ try { } else { notify_game.status = `claimed on ${store}`; 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 await page.screenshot({ path: screenshot('external', `${filenamify(title)}.png`), fullPage: true }); diff --git a/xbox.js b/xbox.js index b2406cc..fd78c66 100644 --- a/xbox.js +++ b/xbox.js @@ -15,8 +15,7 @@ const URL_CLAIM = 'https://www.xbox.com/en-US/live/gold'; // #gameswithgold"; console.log(datetime(), 'started checking xbox'); -const db = await jsonDb('xbox.json'); -db.data ||= {}; +const db = await jsonDb('xbox.json', {}); handleSIGINT();