Clean Sonar issues in store scripts
This commit is contained in:
parent
397871b012
commit
5f919039ab
6 changed files with 172 additions and 187 deletions
|
|
@ -6,7 +6,6 @@ import { cfg } from './src/config.js';
|
|||
|
||||
const screenshot = (...a) => resolve(cfg.dir.screenshots, 'prime-gaming', ...a);
|
||||
|
||||
// const URL_LOGIN = 'https://www.amazon.de/ap/signin'; // wrong. needs some session args to be valid?
|
||||
const URL_CLAIM = 'https://luna.amazon.com/claims/home';
|
||||
|
||||
console.log(datetime(), 'started checking prime-gaming');
|
||||
|
|
@ -25,14 +24,12 @@ const context = await firefox.launchPersistentContext(cfg.dir.browser, {
|
|||
|
||||
handleSIGINT(context);
|
||||
|
||||
// TODO test if needed
|
||||
await stealth(context);
|
||||
|
||||
if (!cfg.debug) context.setDefaultTimeout(cfg.timeout);
|
||||
|
||||
const page = context.pages().length ? context.pages()[0] : await context.newPage(); // should always exist
|
||||
await page.setViewportSize({ width: cfg.width, height: cfg.height }); // TODO workaround for https://github.com/vogler/free-games-claimer/issues/277 until Playwright fixes it
|
||||
// console.debug('userAgent:', await page.evaluate(() => navigator.userAgent));
|
||||
await page.setViewportSize({ width: cfg.width, height: cfg.height }); // workaround for https://github.com/vogler/free-games-claimer/issues/277 until Playwright fixes it
|
||||
|
||||
const notify_games = [];
|
||||
let user;
|
||||
|
|
@ -62,14 +59,18 @@ try {
|
|||
await page.fill('[name=password]', password);
|
||||
await page.click('input[type="submit"]');
|
||||
await handleMFA(page).catch(() => {});
|
||||
page.waitForURL('**/ap/signin**').then(async () => { // check for wrong credentials
|
||||
try {
|
||||
await page.waitForURL('**/ap/signin**');
|
||||
const error = await page.locator('.a-alert-content').first().innerText();
|
||||
if (!error.trim.length) return;
|
||||
console.error('Login error:', error);
|
||||
await notify(`prime-gaming: login: ${error}`);
|
||||
await context.close(); // finishes potential recording
|
||||
process.exit(1);
|
||||
});
|
||||
if (error.trim().length) {
|
||||
console.error('Login error:', error);
|
||||
await notify(`prime-gaming: login: ${error}`);
|
||||
await context.close(); // finishes potential recording
|
||||
process.exit(1);
|
||||
}
|
||||
} catch {
|
||||
// if navigation succeeded, continue
|
||||
}
|
||||
await page.waitForURL(/luna\.amazon\.com\/claims\/.*signedIn=true/);
|
||||
if (!cfg.debug) context.setDefaultTimeout(cfg.timeout);
|
||||
return true;
|
||||
|
|
@ -91,7 +92,7 @@ try {
|
|||
'[data-a-target="user-dropdown-first-name-text"]',
|
||||
'[data-testid="user-dropdown-first-name-text"]',
|
||||
].map(s => page.waitForSelector(s)));
|
||||
page.click('[aria-label="Cookies usage disclaimer banner"] button:has-text("Accept Cookies")').catch(() => { }); // to not waste screen space when non-headless, TODO does not work reliably, need to wait for something else first?
|
||||
page.click('[aria-label="Cookies usage disclaimer banner"] button:has-text("Accept Cookies")').catch(() => { }); // to not waste screen space when non-headless; could be flaky
|
||||
while (await page.locator('button:has-text("Sign in"), button:has-text("Anmelden")').count() > 0) {
|
||||
console.error('Not signed in anymore.');
|
||||
await page.click('button:has-text("Sign in")');
|
||||
|
|
@ -105,16 +106,19 @@ try {
|
|||
await page.fill('[name=email]', email);
|
||||
await page.click('input[type="submit"]');
|
||||
await page.fill('[name=password]', password);
|
||||
// await page.check('[name=rememberMe]'); // no longer exists
|
||||
await page.click('input[type="submit"]');
|
||||
page.waitForURL('**/ap/signin**').then(async () => { // check for wrong credentials
|
||||
try {
|
||||
await page.waitForURL('**/ap/signin**');
|
||||
const error = await page.locator('.a-alert-content').first().innerText();
|
||||
if (!error.trim.length) return;
|
||||
console.error('Login error:', error);
|
||||
await notify(`prime-gaming: login: ${error}`);
|
||||
await context.close(); // finishes potential recording
|
||||
process.exit(1);
|
||||
});
|
||||
if (error.trim().length) {
|
||||
console.error('Login error:', error);
|
||||
await notify(`prime-gaming: login: ${error}`);
|
||||
await context.close(); // finishes potential recording
|
||||
process.exit(1);
|
||||
}
|
||||
} catch {
|
||||
// navigation ok
|
||||
}
|
||||
handleMFA(page).catch(() => {});
|
||||
} else {
|
||||
console.log('Waiting for you to login in the browser.');
|
||||
|
|
@ -130,9 +134,6 @@ try {
|
|||
}
|
||||
user = await page.locator('[data-a-target="user-dropdown-first-name-text"], [data-testid="user-dropdown-first-name-text"]').first().innerText();
|
||||
console.log(`Signed in as ${user}`);
|
||||
// await page.click('button[aria-label="User dropdown and more options"]');
|
||||
// const twitch = await page.locator('[data-a-target="TwitchDisplayName"]').first().innerText();
|
||||
// console.log(`Twitch user name is ${twitch}`);
|
||||
db.data[user] ||= {};
|
||||
|
||||
if (await page.getByRole('button', { name: 'Try Prime' }).count()) {
|
||||
|
|
@ -156,7 +157,7 @@ try {
|
|||
// loading all games became flaky; see https://github.com/vogler/free-games-claimer/issues/357
|
||||
await page.keyboard.press('PageDown'); // scrolling to straight to the bottom started to skip loading some games
|
||||
await page.waitForLoadState('networkidle'); // wait for all games to be loaded
|
||||
await page.waitForTimeout(3000); // TODO networkidle wasn't enough to load all already collected games
|
||||
await page.waitForTimeout(3000); // extra wait to load all already collected games
|
||||
// do it again since once wasn't enough...
|
||||
await page.keyboard.press('PageDown');
|
||||
await page.waitForTimeout(3000);
|
||||
|
|
@ -372,9 +373,9 @@ try {
|
|||
|
||||
for (const { title, url } of external_info) {
|
||||
console.log('Current free game:', chalk.blue(title)); // , url);
|
||||
const existing = db.data[user]?.[title];
|
||||
if (existing && existing.status && !existing.status.startsWith('failed')) {
|
||||
console.log(` Already recorded as ${existing.status}, skipping.`);
|
||||
const existingStatus = db.data[user]?.[title]?.status;
|
||||
if (existingStatus && !existingStatus.startsWith('failed')) {
|
||||
console.log(` Already recorded as ${existingStatus}, skipping.`);
|
||||
notify_games.push({ title, url, status: 'existed' });
|
||||
continue;
|
||||
}
|
||||
|
|
@ -448,21 +449,21 @@ try {
|
|||
page.waitForSelector('div:has-text("Link game account")', { timeout: cfg.timeout }).catch(() => {}),
|
||||
]).catch(() => {});
|
||||
db.data[user][title] ||= { title, time: datetime(), url, store };
|
||||
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() // epic games store also shows "Link account"
|
||||
|| await page.locator('div:has-text("Link account")').count()) {
|
||||
console.error(' Account linking is required to claim this offer!');
|
||||
notify_game.status = `failed: need account linking for ${store}`;
|
||||
db.data[user][title].status = 'failed: need account linking';
|
||||
// await page.pause();
|
||||
// await page.click('[data-a-target="LinkAccountModal"] [data-a-target="LinkAccountButton"]');
|
||||
// TODO login for epic games also needed if already logged in
|
||||
// login for epic games also needed if already logged in
|
||||
// wait for https://www.epicgames.com/id/authorize?redirect_uri=https%3A%2F%2Fservice.link.amazon.gg...
|
||||
// await page.click('button[aria-label="Allow"]');
|
||||
} else {
|
||||
db.data[user][title].status = 'claimed';
|
||||
// print code if there is one
|
||||
const redeem = {
|
||||
// 'origin': 'https://www.origin.com/redeem', // TODO still needed or now only via account linking?
|
||||
// 'origin': 'https://www.origin.com/redeem', // kept for legacy flows; current path uses account linking
|
||||
'gog.com': 'https://www.gog.com/redeem',
|
||||
'microsoft store': 'https://account.microsoft.com/billing/redeem',
|
||||
xbox: 'https://account.microsoft.com/billing/redeem',
|
||||
|
|
@ -519,7 +520,7 @@ try {
|
|||
} else if (reason == 'code_not_found') {
|
||||
redeem_action = 'redeem (not found)';
|
||||
console.error(' Code was not found!');
|
||||
} else { // TODO not logged in? need valid unused code to test.
|
||||
} else { // unknown state; keep info log for later analysis
|
||||
redeem_action = 'redeemed?';
|
||||
// console.log(' Redeemed successfully? Please report your Responses (if new) in https://github.com/vogler/free-games-claimer/issues/5');
|
||||
console.debug(` Response 1: ${r1t}`);
|
||||
|
|
@ -569,12 +570,12 @@ try {
|
|||
if (j?.events?.cart.length && j.events.cart[0]?.data?.reason == 'UserAlreadyOwnsContent') {
|
||||
redeem_action = 'already redeemed';
|
||||
console.error(' error: UserAlreadyOwnsContent');
|
||||
} else { // TODO what's returned on success?
|
||||
} else { // success path not seen yet; log below if needed
|
||||
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
|
||||
} else { // other responses; keep info log for analysis
|
||||
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');
|
||||
|
|
@ -672,7 +673,7 @@ try {
|
|||
const match = unlinked_store.match(/Link (.*) account/);
|
||||
if (match && match.length == 2) unlinked_store = match[1];
|
||||
} else if (await page.locator('text=Link game account').count()) { // epic-games only?
|
||||
console.error(' Missing account linking (epic-games specific button?):', await page.locator('button[data-a-target="gms-cta"]').innerText()); // TODO needed?
|
||||
console.error(' Missing account linking (epic-games specific button?):', await page.locator('button[data-a-target="gms-cta"]').innerText()); // track account-linking UI drift
|
||||
unlinked_store = 'epic-games';
|
||||
}
|
||||
if (unlinked_store) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue