Skip to content

Commit

Permalink
Merge pull request #110 from Revadike/feature-check-before-login
Browse files Browse the repository at this point in the history
Feature check before login
  • Loading branch information
Revadike authored Jun 23, 2021
2 parents cb5429b + 072bf1c commit 90cec19
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 18 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ node_modules
package-lock.json
DeviceAuthGenerator.exe
device_auths.json
deviceAuths.json
history.json
.egstore/*
.vscode/*
!.vscode/settings.json
Expand All @@ -13,4 +15,4 @@ device_auths.json
!.vscode/extensions.json
*.code-workspace
.idea
.github
.github
36 changes: 27 additions & 9 deletions claimer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand All @@ -31,38 +33,51 @@ function sleep(delay) {
}

(async() => {
if (!await isUpToDate()) {
Logger.warn(`There is a new version available: ${Package.url}`);
}

let { options, delay, loop } = Config;
do {
if (!await isUpToDate()) {
Logger.warn(`There is a new version available: ${Package.url}`);
}

for (let email in Auths) {
let { country } = Auths[email];
let useDeviceAuth = true;
let clientOptions = { email, ...options };
let client = new EpicGames(clientOptions);
if (!await client.init()) {
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
Expand All @@ -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`);
}
Expand Down
6 changes: 3 additions & 3 deletions config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"options": {},
"delay": 1440,
"loop": true
"options": {},
"delay": 1440,
"loop": true
}
10 changes: 5 additions & 5 deletions logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ module.exports = {
"debug": colors.blue,
"info": colors.green,
"warn": colors.magenta,
"error": [colors.red, colors.bold]
"error": [colors.red, colors.bold],
},
"preprocess": data => {
"preprocess": (data) => {
data.title = data.title.toUpperCase();
while (data.title.length < 5) { data.title += " "; }
data.args = [...data.args];
Expand All @@ -23,17 +23,17 @@ module.exports = {
data.args.shift();
}
},
"transport": data => {
"transport": (data) => {
// eslint-disable-next-line no-console
console.log(data.output);

const streamoptions = {
"flags": "a",
"encoding": "utf8"
"encoding": "utf8",
};
fs.createWriteStream(path.join(__dirname, "claimer.log"), streamoptions).write(`\r\n${data.rawoutput}`);
if (data.logpath) {
fs.createWriteStream(data.logpath, streamoptions).write(`\r\n${data.rawoutput}`);
}
}
},
};

0 comments on commit 90cec19

Please sign in to comment.