Skip to content

Commit aba4300

Browse files
GamerPower: restrict claimed check to current user
1 parent 0a9117c commit aba4300

File tree

3 files changed

+87
-20
lines changed

3 files changed

+87
-20
lines changed

epic-games.js

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ import { resolve, jsonDb, datetime, filenamify, prompt, confirm, notify, html_ga
88
import { cfg } from './src/config.js';
99
import { getMobileGames } from './src/epic-games-mobile.js';
1010
import { gpUrlToStoreUrls } from './src/gp.js';
11+
import { getUsername, setUsername, writeAccountsDb } from './src/accounts.js';
1112

1213
const screenshot = (...a) => resolve(cfg.dir.screenshots, 'epic-games', ...a);
1314

1415
const URL_CLAIM = 'https://store.epicgames.com/en-US/free-games';
1516
const URL_LOGIN = 'https://www.epicgames.com/id/login?lang=en-US&noHostRedirect=true&redirectUrl=' + URL_CLAIM;
1617
const GAMERPOWER_API_URL = 'https://www.gamerpower.com/api/giveaways?platform=epic-games-store&type=game';
18+
const PLATFORM = 'epic-games';
1719

1820
console.log(datetime(), 'started checking epic-games');
1921

@@ -26,16 +28,17 @@ function extractGameIdFromUrl(url) {
2628
}
2729

2830
function isGpGameAlreadyClaimed(storeUrl) {
31+
const username = getUsername(PLATFORM, cfg.eg_email);
32+
if (!username) return false; // Username not known yet
33+
2934
const game_id = extractGameIdFromUrl(storeUrl);
30-
31-
// Check if any user has claimed this game
32-
for (const [username, games] of Object.entries(db.data)) {
33-
if (games[game_id]?.status === 'claimed' || games[game_id]?.status === 'existed') {
34-
console.log(`[GamerPower] Already claimed by ${username}: ${storeUrl} -> ${game_id}`);
35-
return true;
36-
}
35+
const games = db.data[username];
36+
37+
if (games?.[game_id]?.status === 'claimed' || games?.[game_id]?.status === 'existed') {
38+
console.log(`[GamerPower] Already claimed by ${username}: ${storeUrl} -> ${game_id}`);
39+
return true;
3740
}
38-
41+
3942
return false;
4043
}
4144

@@ -56,7 +59,7 @@ async function getUnclaimedGpUrls() {
5659
// Filter out already claimed games
5760
const unclaimed = epicGames.filter(g => !isGpGameAlreadyClaimed(g.storeUrl));
5861
console.log(`[GamerPower] ${unclaimed.length} unclaimed games`);
59-
62+
6063
return unclaimed.map(g => g.storeUrl);
6164
}
6265

@@ -66,10 +69,15 @@ async function getUnclaimedGpUrls() {
6669

6770
if (cfg.time) console.time('startup');
6871

72+
// EG_CHECK_GP requires EG_EMAIL to be set for claimed status tracking
73+
if (cfg.eg_check_gp && !cfg.eg_email) {
74+
throw new Error('EG_CHECK_GP requires EG_EMAIL to be set');
75+
}
76+
6977
// Check GamerPower first (before starting main browser)
7078
const gpUrls = await getUnclaimedGpUrls();
7179

72-
// If EG_CHECK_GP is enabled and no unclaimed games, exit early without opening browser
80+
// If EG_CHECK_GP is enabled and no unclaimed giveaways, exit early without opening browser
7381
if (cfg.eg_check_gp && gpUrls.length === 0) {
7482
console.log('No unclaimed GamerPower giveaways. Exiting.');
7583
process.exit(0);
@@ -186,7 +194,14 @@ try {
186194
}
187195
user = await page.locator('egs-navigation').getAttribute('displayname'); // 'null' if !isloggedin
188196
console.log(`Signed in as ${user}`);
197+
198+
// Save email -> username mapping if email is configured
199+
if (cfg.eg_email) {
200+
setUsername(PLATFORM, cfg.eg_email, user);
201+
await writeAccountsDb();
202+
}
189203
db.data[user] ||= {};
204+
190205
if (cfg.time) console.timeEnd('login');
191206
if (cfg.time) console.time('claim all games');
192207

gog.js

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,37 @@ import chalk from 'chalk';
44
import { resolve, jsonDb, datetime, filenamify, prompt, confirm, notify, html_game_list, handleSIGINT } from './src/util.js';
55
import { cfg } from './src/config.js';
66
import { gpUrlToStoreUrls } from './src/gp.js';
7+
import { getUsername, setUsername, writeAccountsDb } from './src/accounts.js';
78

89
const screenshot = (...a) => resolve(cfg.dir.screenshots, 'gog', ...a);
910

1011
const URL_CLAIM = 'https://www.gog.com/en';
1112
const GAMERPOWER_API_URL = 'https://www.gamerpower.com/api/giveaways?platform=gog&type=game';
13+
const PLATFORM = 'gog';
1214

1315
console.log(datetime(), 'started checking gog');
1416

1517
const db = await jsonDb('gog.json', {});
1618

1719
function isGpGameAlreadyClaimed(storeUrl) {
20+
const username = getUsername(PLATFORM, cfg.gog_email);
21+
if (!username) return false; // Username not known yet
22+
1823
// GOG URLs look like: https://www.gog.com/en/game/game-name
1924
const match = storeUrl.match(/\/game\/([^/?]+)/);
2025
const game_id = match ? match[1] : storeUrl.split('/').pop();
21-
22-
// Check if any user has claimed this game (GOG db uses title as key)
23-
for (const [username, games] of Object.entries(db.data)) {
24-
for (const [title, info] of Object.entries(games)) {
25-
if (info.url?.includes(game_id) && (info.status === 'claimed' || info.status === 'existed')) {
26-
console.log(`[GamerPower] Already claimed by ${username}: ${storeUrl}`);
27-
return true;
28-
}
26+
27+
// GOG db uses title as key, check if any game URL contains the game_id
28+
const games = db.data[username];
29+
if (!games) return false;
30+
31+
for (const [title, info] of Object.entries(games)) {
32+
if (info.url?.includes(game_id) && (info.status === 'claimed' || info.status === 'existed')) {
33+
console.log(`[GamerPower] Already claimed by ${username}: ${storeUrl}`);
34+
return true;
2935
}
3036
}
31-
37+
3238
return false;
3339
}
3440

@@ -49,10 +55,15 @@ async function getUnclaimedGpUrls() {
4955
return unclaimed.map(g => g.storeUrl);
5056
}
5157

58+
// GOG_CHECK_GP requires GOG_EMAIL to be set for claimed status tracking
59+
if (cfg.gog_check_gp && !cfg.gog_email) {
60+
throw new Error('GOG_CHECK_GP requires GOG_EMAIL to be set');
61+
}
62+
5263
// Check GamerPower first (before starting browser)
5364
const gpUrls = await getUnclaimedGpUrls();
5465

55-
// If GOG_CHECK_GP is enabled and no unclaimed games, exit early without opening browser
66+
// If GOG_CHECK_GP is enabled and no unclaimed giveaways, exit early without opening browser
5667
if (cfg.gog_check_gp && gpUrls.length === 0) {
5768
console.log('No unclaimed GamerPower giveaways. Exiting.');
5869
process.exit(0);
@@ -151,6 +162,12 @@ try {
151162
}
152163
user = await page.locator('#menuUsername').first().textContent(); // innerText is uppercase due to styling!
153164
console.log(`Signed in as ${user}`);
165+
166+
// Save email -> username mapping if email is configured
167+
if (cfg.gog_email) {
168+
setUsername(PLATFORM, cfg.gog_email, user);
169+
await writeAccountsDb();
170+
}
154171
db.data[user] ||= {};
155172

156173
const banner = page.locator('#giveaway');

src/accounts.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Shared accounts database: maps platform + email -> username
2+
import { jsonDb } from './util.js';
3+
4+
const accountsDb = await jsonDb('accounts.json', {});
5+
6+
/**
7+
* Get username for email from accounts db (if known)
8+
* @param {string} platform - Platform name (e.g., 'epic-games', 'gog', 'steam')
9+
* @param {string} email - User's email
10+
* @returns {string|undefined} - Username if known, undefined otherwise
11+
*/
12+
export function getUsername(platform, email) {
13+
return accountsDb.data[platform]?.[email];
14+
}
15+
16+
/**
17+
* Save username for email to accounts db
18+
* @param {string} platform - Platform name
19+
* @param {string} email - User's email
20+
* @param {string} username - Username to save
21+
*/
22+
export function setUsername(platform, email, username) {
23+
accountsDb.data[platform] ||= {};
24+
if (accountsDb.data[platform][email] !== username) {
25+
accountsDb.data[platform][email] = username;
26+
console.log(`[Accounts] Saved: ${platform}/${email} -> ${username}`);
27+
}
28+
}
29+
30+
/**
31+
* Write accounts db to disk
32+
*/
33+
export async function writeAccountsDb() {
34+
await accountsDb.write();
35+
}

0 commit comments

Comments
 (0)