epic-games: migrateDb: rm .runs, .claimed[] -> .[user][game_id], closes #27
If you'd like to keep the .runs data: `cp -a data/epic-games.{json, v1.json}`
Objects also have insertion order for non-number strings, so there's not need for a list:
https://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order
This commit is contained in:
parent
61af4e35f6
commit
ac758d39e4
2 changed files with 23 additions and 19 deletions
|
|
@ -12,13 +12,16 @@ const SCREEN_WIDTH = Number(process.env.SCREEN_WIDTH) - 80 || 1280;
|
||||||
const SCREEN_HEIGHT = Number(process.env.SCREEN_HEIGHT) || 1280;
|
const SCREEN_HEIGHT = Number(process.env.SCREEN_HEIGHT) || 1280;
|
||||||
|
|
||||||
const db = await jsonDb('epic-games.json');
|
const db = await jsonDb('epic-games.json');
|
||||||
db.data ||= { claimed: [], runs: [] };
|
const migrateDb = (user) => {
|
||||||
const run = {
|
if (user in db.data || !('claimed' in db.data)) return;
|
||||||
startTime: datetime(),
|
db.data[user] = {};
|
||||||
endTime: null,
|
for (const e of db.data.claimed) {
|
||||||
n: null, // unclaimed games at beginning
|
const k = e.url.split('/').pop();
|
||||||
c: 0, // claimed games at end
|
db.data[user][k] = e;
|
||||||
};
|
}
|
||||||
|
delete db.data.claimed;
|
||||||
|
delete db.data.runs;
|
||||||
|
}
|
||||||
|
|
||||||
// https://playwright.dev/docs/auth#multi-factor-authentication
|
// https://playwright.dev/docs/auth#multi-factor-authentication
|
||||||
const context = await chromium.launchPersistentContext(dirs.browser, {
|
const context = await chromium.launchPersistentContext(dirs.browser, {
|
||||||
|
|
@ -62,6 +65,8 @@ try {
|
||||||
}
|
}
|
||||||
const user = await page.locator('#user span').first().innerHTML();
|
const user = await page.locator('#user span').first().innerHTML();
|
||||||
console.log(`Signed in as ${user}`);
|
console.log(`Signed in as ${user}`);
|
||||||
|
migrateDb(user); // TODO remove this after some time since it will run fine without and people can still use this commit to adjust their data epic-games.json
|
||||||
|
db.data[user] ||= {};
|
||||||
|
|
||||||
// Detect free games
|
// Detect free games
|
||||||
const game_loc = await page.locator('a:has(span:text-is("Free Now"))');
|
const game_loc = await page.locator('a:has(span:text-is("Free Now"))');
|
||||||
|
|
@ -72,8 +77,6 @@ try {
|
||||||
// filter data.Catalog.searchStore.elements for .promotions.promotionalOffers being set and build URL with .catalogNs.mappings[0].pageSlug or .urlSlug if not set to some wrong id like it was the case for spirit-of-the-north-f58a66 - this is also what's done here: https://github.com/claabs/epicgames-freegames-node/blob/938a9653ffd08b8284ea32cf01ac8727d25c5d4c/src/puppet/free-games.ts#L138-L213
|
// filter data.Catalog.searchStore.elements for .promotions.promotionalOffers being set and build URL with .catalogNs.mappings[0].pageSlug or .urlSlug if not set to some wrong id like it was the case for spirit-of-the-north-f58a66 - this is also what's done here: https://github.com/claabs/epicgames-freegames-node/blob/938a9653ffd08b8284ea32cf01ac8727d25c5d4c/src/puppet/free-games.ts#L138-L213
|
||||||
const urlSlugs = await Promise.all((await game_loc.elementHandles()).map(a => a.getAttribute('href')));
|
const urlSlugs = await Promise.all((await game_loc.elementHandles()).map(a => a.getAttribute('href')));
|
||||||
const urls = urlSlugs.map(s => 'https://store.epicgames.com' + s);
|
const urls = urlSlugs.map(s => 'https://store.epicgames.com' + s);
|
||||||
const n = run.n = await game_loc.count();
|
|
||||||
// console.log('Number of free games:', n);
|
|
||||||
console.log('Free games:', urls);
|
console.log('Free games:', urls);
|
||||||
|
|
||||||
for (const url of urls) {
|
for (const url of urls) {
|
||||||
|
|
@ -87,11 +90,14 @@ try {
|
||||||
}
|
}
|
||||||
|
|
||||||
const title = await page.locator('h1 div').first().innerText();
|
const title = await page.locator('h1 div').first().innerText();
|
||||||
const title_url = 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!
|
||||||
console.log('Current free game:', title);
|
console.log('Current free game:', title);
|
||||||
|
|
||||||
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.');
|
||||||
|
db.data[user][game_id].status ||= 'existed'; // does not overwrite claimed or failed
|
||||||
|
if (db.data[user][game_id].status == 'failed') db.data[user][game_id].status = 'manual'; // was failed but now it's claimed
|
||||||
} 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"]');
|
await page.click('[data-testid="purchase-cta-button"]');
|
||||||
|
|
@ -121,28 +127,25 @@ try {
|
||||||
// }
|
// }
|
||||||
|
|
||||||
await page.waitForSelector('text=Thank you for buying'); // EU: wait, non-EU: wait again = no-op
|
await page.waitForSelector('text=Thank you for buying'); // EU: wait, non-EU: wait again = no-op
|
||||||
db.data.claimed.push({ title, time: datetime(), url: page.url() });
|
db.data[user][game_id].status = 'claimed';
|
||||||
run.c++;
|
db.data[user][game_id].time = datetime(); // claimed time overwrites failed time
|
||||||
console.log(' Claimed successfully!');
|
console.log(' Claimed successfully!');
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log(e);
|
console.log(e);
|
||||||
const p = path.resolve(dirs.screenshots, 'epic-games', 'captcha', `${filenamify(datetime())}.png`);
|
const p = path.resolve(dirs.screenshots, 'epic-games', 'captcha', `${filenamify(datetime())}.png`);
|
||||||
await page.screenshot({ path: p, fullPage: true });
|
await page.screenshot({ path: p, fullPage: true });
|
||||||
|
db.data[user][game_id].status = 'failed';
|
||||||
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?
|
||||||
}
|
}
|
||||||
|
|
||||||
const p = path.resolve(dirs.screenshots, 'epic-games', `${title_url}.png`);
|
const p = path.resolve(dirs.screenshots, 'epic-games', `${game_id}.png`);
|
||||||
if (!existsSync(p)) await page.screenshot({ path: p, fullPage: false }); // fullPage is quite long...
|
if (!existsSync(p)) await page.screenshot({ path: p, fullPage: false }); // fullPage is quite long...
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error); // .toString()?
|
||||||
run.error = error.toString();
|
|
||||||
} finally {
|
} finally {
|
||||||
// write out json db
|
await db.write(); // write out json db
|
||||||
run.endTime = datetime();
|
|
||||||
db.data.runs.push(run);
|
|
||||||
await db.write();
|
|
||||||
}
|
}
|
||||||
await context.close();
|
await context.close();
|
||||||
|
|
|
||||||
1
util.js
1
util.js
|
|
@ -16,6 +16,7 @@ import { Low, JSONFile } from 'lowdb';
|
||||||
export const jsonDb = async file => {
|
export const jsonDb = async file => {
|
||||||
const db = new Low(new JSONFile(dataDir(file)));
|
const db = new Low(new JSONFile(dataDir(file)));
|
||||||
await db.read();
|
await db.read();
|
||||||
|
db.data ||= {};
|
||||||
return db;
|
return db;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue