Merge branch 'main' into main

This commit is contained in:
Ralf Vogler 2024-06-25 13:30:42 +02:00 committed by GitHub
commit afceb0aad9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 451 additions and 311 deletions

View file

@ -107,6 +107,26 @@ try {
// can't use .all() since the list of elements via locator will change after click while we iterate over it
const internal = await games.locator('.item-card__action:has([data-a-target="FGWPOffer"])').elementHandles();
const external = await games.locator('.item-card__action:has([data-a-target="ExternalOfferClaim"])').all();
// bottom to top: oldest to newest games
internal.reverse();
external.reverse();
const checkTimeLeft = async url => {
// console.log(' Checking time left for game:', url);
const check = async p => {
console.log(' ', await p.locator('.availability-date').innerText());
const dueDateOrg = await p.locator('.availability-date .tw-bold').innerText();
const dueDate = datetime(new Date(Date.parse(dueDateOrg + ' 17:00')));
console.log(' Due date:', dueDate);
};
if (page.url() == url) {
await check(page);
} else {
const p = await context.newPage();
await p.goto(url, { waitUntil: 'domcontentloaded' });
await check(p);
p.close();
}
};
console.log('Number of free unclaimed games (Prime Gaming):', internal.length);
// claim games in internal store
for (const card of internal) {
@ -115,6 +135,7 @@ try {
const slug = await (await card.$('a')).getAttribute('href');
const url = 'https://gaming.amazon.com' + slug.split('?')[0];
console.log('Current free game:', title);
if (cfg.pg_timeLeft) await checkTimeLeft(url);
if (cfg.dryrun) continue;
if (cfg.interactive && !await confirm()) continue;
await (await card.$('.tw-button:has-text("Claim")')).click();
@ -134,6 +155,7 @@ try {
// await (await card.$('text=Claim')).click(); // goes to URL of game, no need to wait
external_info.push({ title, url });
}
// external_info = [ { title: 'Fallout 76 (XBOX)', url: 'https://gaming.amazon.com/fallout-76-xbox-fgwp/dp/amzn1.pg.item.9fe17d7b-b6c2-4f58-b494-cc4e79528d0b?ingress=amzn&ref_=SM_Fallout76XBOX_S01_FGWP_CRWN' } ];
for (const { title, url } of external_info) {
console.log('Current free game:', title); // , url);
await page.goto(url, { waitUntil: 'domcontentloaded' });
@ -141,6 +163,7 @@ try {
const item_text = await page.innerText('[data-a-target="DescriptionItemDetails"]');
const store = item_text.toLowerCase().replace(/.* on /, '').slice(0, -1);
console.log(' External store:', store);
if (cfg.pg_timeLeft) await checkTimeLeft(url);
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
@ -163,7 +186,8 @@ try {
const redeem = {
// 'origin': 'https://www.origin.com/redeem', // TODO still needed or now only via account linking?
'gog.com': 'https://www.gog.com/redeem',
'microsoft games': 'https://redeem.microsoft.com',
'microsoft store': 'https://account.microsoft.com/billing/redeem',
xbox: 'https://account.microsoft.com/billing/redeem',
'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()
@ -218,27 +242,44 @@ try {
console.log(' Unknown Response 2 - please report in https://github.com/vogler/free-games-claimer/issues/5');
}
}
} else if (store == 'microsoft games') {
console.error(` Redeem on ${store} not yet implemented!`);
} else if (store == 'microsoft store' || store == 'xbox') {
console.error(` Redeem on ${store} is experimental!`);
// await page2.pause();
if (page2.url().startsWith('https://login.')) {
console.error(' Not logged in! Use the browser to login manually.');
console.error(' Not logged in! Use the browser to login manually. Waiting for 60s.');
await page2.waitForTimeout(60 * 1000);
redeem_action = 'redeem (login)';
} else {
const r = page2.waitForResponse(r => r.url().startsWith('https://purchase.mp.microsoft.com/'));
await page2.fill('[name=tokenString]', code);
const iframe = page2.frameLocator('#redeem-iframe');
const input = iframe.locator('[name=tokenString]');
await input.waitFor();
await input.fill(code);
const r = page2.waitForResponse(r => r.url().startsWith('https://cart.production.store-web.dynamics.com/v1.0/Redeem/PrepareRedeem'));
// console.log(await page2.locator('.redeem_code_error').innerText());
const rt = await (await r).text();
console.debug(` Response: ${rt}`);
// {"code":"NotFound","data":[],"details":[],"innererror":{"code":"TokenNotFound",...
const reason = JSON.parse(rt).code;
if (reason == 'NotFound') {
const j = JSON.parse(rt);
const reason = j?.events?.cart.length && j.events.cart[0]?.data?.reason;
if (reason == 'TokenNotFound') {
redeem_action = 'redeem (not found)';
console.error(' Code was not found!');
} else if (j?.productInfos?.length && j.productInfos[0]?.redeemable) {
await iframe.locator('button:has-text("Next")').click();
await iframe.locator('button:has-text("Confirm")').click();
const r = page2.waitForResponse(r => r.url().startsWith('https://cart.production.store-web.dynamics.com/v1.0/Redeem/RedeemToken'));
const j = JSON.parse(await (await r).text());
if (j?.events?.cart.length && j.events.cart[0]?.data?.reason == 'UserAlreadyOwnsContent') {
redeem_action = 'already redeemed';
console.error(' error: UserAlreadyOwnsContent');
} else if (true) { // TODO what's returned on success?
redeem_action = 'redeemed';
db.data[user][title].status = 'claimed and redeemed?';
console.log(' Redeemed successfully? Please report if not in https://github.com/vogler/free-games-claimer/issues/5');
}
} else { // TODO find out other responses
await page2.click('#nextButton');
redeem_action = 'redeemed?';
redeem_action = 'unknown';
console.debug(` Response: ${rt}`);
console.log(' Redeemed successfully? Please report your Response from above (if it is new) in https://github.com/vogler/free-games-claimer/issues/5');
db.data[user][title].status = 'claimed and redeemed?';
}
}
} else if (store == 'legacy games') {