From 072bf1c027490d4df637d46e5eb8654e655874ef Mon Sep 17 00:00:00 2001 From: Revadike Date: Wed, 23 Jun 2021 14:31:14 +0200 Subject: [PATCH] fixes #109 --- .gitignore | 1 + claimer.js | 28 +++++++++++++++++++++++----- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 1b0fa7f..0b02f9e 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ package-lock.json DeviceAuthGenerator.exe device_auths.json deviceAuths.json +history.json .egstore/* .vscode/* !.vscode/settings.json diff --git a/claimer.js b/claimer.js index d283fd3..dc80742 100644 --- a/claimer.js +++ b/claimer.js @@ -2,10 +2,12 @@ const { "Launcher": EpicGames } = require("epicgames-client"); const { freeGamesPromotions } = require("./src/gamePromotions"); +const { writeFile } = require("fs"); const Auths = require(`${__dirname}/device_auths.json`); const CheckUpdate = require("check-update-github"); const Config = require(`${__dirname}/config.json`); +const History = require(`${__dirname}/history.json`); const Logger = require("tracer").console(`${__dirname}/logger.js`); const Package = require("./package.json"); @@ -38,6 +40,7 @@ function sleep(delay) { } for (let email in Auths) { + let { country } = Auths[email]; let useDeviceAuth = true; let clientOptions = { email, ...options }; let client = new EpicGames(clientOptions); @@ -45,24 +48,36 @@ function sleep(delay) { throw new Error("Error while initialize process."); } + // Check before logging in + let freePromos = await freeGamesPromotions(client, country, country); + let claimedPromos = Object.values(History); + let unclaimedPromos = freePromos.filter((offer) => !claimedPromos.find( + (_offer) => _offer.id === offer.id && _offer.namespace === offer.namespace, + )); + + Logger.info(`Found ${unclaimedPromos.length} unclaimed freebie(s)`); + if (unclaimedPromos.length === 0) { + return; + } + let success = await client.login({ useDeviceAuth }); if (!success) { throw new Error(`Failed to login as ${client.config.email}`); } Logger.info(`Logged in as ${client.account.name} (${client.account.id})`); + Auths[email].country = client.account.country; + writeFile(`${__dirname}/device_auths.json`, JSON.stringify(Auths, null, 4), () => false); // ignore fails - let { country } = client.account; - let freePromos = await freeGamesPromotions(client, country, country); - - for (let offer of freePromos) { + for (let offer of unclaimedPromos) { try { let purchased = await client.purchase(offer, 1); if (purchased) { Logger.info(`Successfully claimed ${offer.title} (${purchased})`); } else { - Logger.info(`${offer.title} was already claimed for this account`); + Logger.warn(`${offer.title} was already claimed for this account`); } + History[Date.now()] = offer; // Also remember already claimed offers } catch (err) { Logger.warn(`Failed to claim ${offer.title} (${err})`); if (err.response @@ -75,6 +90,9 @@ function sleep(delay) { } } + writeFile(`${__dirname}/history.json`, JSON.stringify(History, null, 4), (err) => { + if (err) { throw err; } + }); await client.logout(); Logger.info(`Logged ${client.account.name} out of Epic Games`); }