Skip to content

Commit 8139c0a

Browse files
committed
gog: save metadata, screenshot, closes #18
1 parent 351670f commit 8139c0a

File tree

1 file changed

+45
-9
lines changed

1 file changed

+45
-9
lines changed

gog.js

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { firefox } from 'playwright'; // stealth plugin needs no outdated playwright-extra
22
import path from 'path';
3-
import { dirs, jsonDb, datetime, stealth, filenamify } from './util.js';
3+
import { dirs, jsonDb, datetime, filenamify } from './util.js';
44
import { cfg } from './config.js';
55

66
import prompts from 'prompts'; // alternatives: enquirer, inquirer
@@ -20,6 +20,8 @@ const context = await firefox.launchPersistentContext(dirs.browser, {
2020
headless: cfg.headless,
2121
viewport: { width: cfg.width, height: cfg.height },
2222
locale: "en-US", // ignore OS locale to be sure to have english text for locators -> done via /en in URL
23+
// recordHar: { path: './data/gog.har' }, // https://toolbox.googleapps.com/apps/har_analyzer/
24+
// recordVideo: { dir: './data/videos' }, // console.log(await page.video().path());
2325
});
2426

2527
if (!cfg.debug) context.setDefaultTimeout(cfg.timeout);
@@ -69,17 +71,51 @@ try {
6971
// await page.waitForNavigation(); // TODO was blocking
7072
if (!cfg.debug) context.setDefaultTimeout(cfg.timeout);
7173
}
72-
const user = await page.locator('#menuUsername').first().innerHTML();
73-
console.log(`Signed in as ${user}`);
74+
const user = await page.locator('#menuUsername').first().textContent(); // innerText is uppercase due to styling!
75+
console.log(`Signed in as '${user}'`);
7476
db.data[user] ||= {};
7577

76-
console.log('TODO get title of current game (waiting for next offer)');
77-
await page.goto('https://www.gog.com/giveaway/claim');
78-
console.log(await page.innerText('body'));
78+
const banner = page.locator('#giveaway');
79+
if (!await banner.count()) {
80+
console.log('Currently no free giveaway!');
81+
} else {
82+
const text = await page.locator('.giveaway-banner__title').innerText();
83+
const title = text.match(/Claim (.*) and don't miss/)[1];
84+
const slug = await banner.getAttribute('href');
85+
const url = `https://gog.com${slug}`;
86+
console.log(`Current free game: ${title} - ${url}`);
87+
db.data[user][title] ||= { title, time: datetime(), url };
88+
const p = path.resolve(dirs.screenshots, 'gog', `${filenamify(title)}.png`);
89+
await banner.screenshot({ path: p }); // overwrites every time - only keep first?
90+
// await banner.getByRole('button', { name: 'Add to library' }).click();
91+
// instead of clicking the button, we visit the auto-claim URL which gives as a JSON response which is easier than checking the state of a button
92+
await page.goto('https://www.gog.com/giveaway/claim');
93+
const response = await page.innerText('body');
94+
// console.log(response);
95+
// {} // when successfully claimed
96+
// {"message":"Already claimed"}
97+
// {"message":"Unauthorized"}
98+
// {"message":"Giveaway has ended"}
99+
let status;
100+
if (response == '{}') {
101+
status = 'claimed';
102+
console.log(' Claimed successfully!');
103+
} else {
104+
const message = JSON.parse(response).message;
105+
if (message == 'Already claimed') {
106+
status = 'existed'; // same status text as for epic-games
107+
console.log(' Already in library! Nothing to claim.');
108+
} else {
109+
console.log(response);
110+
status = message;
111+
}
112+
}
113+
db.data[user][title].status ||= status;
79114

80-
console.log("Unsubscribe from 'Promotions and hot deals' newsletter");
81-
await page.goto('https://www.gog.com/en/account/settings/subscriptions');
82-
await page.locator('li:has-text("Promotions and hot deals") input').uncheck();
115+
console.log("Unsubscribe from 'Promotions and hot deals' newsletter");
116+
await page.goto('https://www.gog.com/en/account/settings/subscriptions');
117+
await page.locator('li:has-text("Promotions and hot deals") label').uncheck();
118+
}
83119
} catch (error) {
84120
console.error(error); // .toString()?
85121
} finally {

0 commit comments

Comments
 (0)