diff --git a/.eslintrc.json b/.eslintrc.json index e4af0bc..ee30741 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -51,7 +51,7 @@ "linebreak-style": "error", "lines-between-class-members": ["error", "always"], "max-classes-per-file": "error", - "max-depth": "error", + "max-depth": ["error", 5], "max-len": ["error", 130], "max-nested-callbacks": ["error", 4], "max-statements-per-line": ["error", { "max": 2 }], @@ -107,7 +107,6 @@ "no-trailing-spaces": "error", "no-undef-init": "error", "no-undefined": "error", - "no-unmodified-loop-condition": "error", "no-unneeded-ternary": ["error", { "defaultAssignment": false }], "no-unused-expressions": "error", "no-use-before-define": "error", diff --git a/README.md b/README.md index f3476f8..bd56ffd 100644 --- a/README.md +++ b/README.md @@ -7,11 +7,16 @@ Claim [available free game promotions](https://www.epicgames.com/store/free-game ## Requirements * [Node.js](https://nodejs.org/download/) -## Instructions +## Instructions (config) +1. Download/clone repo +2. Run `npm install` +3. Edit `config.json` to include your EpicGames credentials and options +4. Run `node gimme_free_epic_shit`* + +## Instructions (arguments) 1. Download/clone repo -2. (Optional) Edit `config.json` to include your EpicGames credentials 3. Run `npm install` -4. Run `node gimme_free_epic_shit` or `node gimme_free_epic_shit USERNAME PASSWORD 0/1`* +4. Run `node gimme_free_epic_shit USERNAME PASSWORD 0/1`* *Only this step is required after the initial use. @@ -23,7 +28,16 @@ Also, this is a good alternative, in case you don't like using Epic's client or ### Why should I even bother claiming these free games? To which I will say, why not? Most of these games are actually outstanding games! Even if you don't like Epic and their shenanigans, you will be pleased to know that Epic actually funds all the free copies that are given away: ["But we actually found it was more economical to pay developers [a lump sum] to distribute their game free for two weeks..."](https://arstechnica.com/gaming/2019/03/epic-ceo-youre-going-to-see-lower-prices-on-epic-games-store/) +### Can I use the looping or multi-account feature when using launch arguments? +No, these are only usable by using the config. + ## Changelog +### V1.2.2 + * Added looping feature a.k.a. run forever* (Closes #2) + * Added multi-account support* + +*Please update your config accordingly + ### V1.2.1 * Makes `rememberLastSession` optional in config or launch parameter (Closes #8) * Added ESLint linter diff --git a/config.json b/config.json index bfe3e29..e200c19 100644 --- a/config.json +++ b/config.json @@ -11,6 +11,6 @@ "rememberLastSession": true } ], - "delay": 3600, + "delay": 1440, "loop": true } \ No newline at end of file diff --git a/gimme_free_epic_shit.js b/gimme_free_epic_shit.js index c5220db..fadb24d 100644 --- a/gimme_free_epic_shit.js +++ b/gimme_free_epic_shit.js @@ -1,86 +1,82 @@ "use strict"; -const { "Launcher": EpicGames } = require("epicgames-client"); const ClientLoginAdapter = require("epicgames-client-login-adapter"); -const sleep = delay => new Promise(res => setTimeout(res, delay * 1000)); +const { "Launcher": EpicGames } = require("epicgames-client"); +let { accounts, delay, loop } = require(`${__dirname}/config.json`); (async() => { - let config = require(`${__dirname}/config.json`); + let sleep = delay => new Promise(res => setTimeout(res, delay * 60000)); do { - try { - for (let accountId in config.accounts) { - let account = config.accounts[accountId]; - const credentials = { - "email": process.argv[2] || account.email, - "password": process.argv[3] || account.password - }; - if (typeof process.argv[4] === "undefined") { - credentials.rememberLastSession = account.rememberLastSession; - } else { - credentials.rememberLastSession = Boolean(Number(process.argv[4])); - } - let client = new EpicGames(credentials); + if (process.argv.length > 2) { + loop = false; + accounts = [{ + "email": process.argv[2], + "password": process.argv[3], + "rememberLastSession": Boolean(Number(process.argv[4])) + }]; + } - if (!await client.init()) { - throw new Error("Error while initialize process."); - } + for (let account of accounts) { + let client = new EpicGames(account); - if (!await client.login().catch(() => false)) { - console.log(`Failed to login as ${client.config.email}, please attempt manually.`); - let auth = await ClientLoginAdapter.init(credentials); - let exchangeCode = await auth.getExchangeCode(); - await auth.close(); + if (!await client.init()) { + throw new Error("Error while initialize process."); + } - if (!await client.login(null, exchangeCode)) { - throw new Error("Error while logging in."); - } + if (!await client.login().catch(() => false)) { + console.log(`Failed to login as ${client.config.email}, please attempt manually.`); + let auth = await ClientLoginAdapter.init(account); + let exchangeCode = await auth.getExchangeCode(); + await auth.close(); + + if (!await client.login(null, exchangeCode)) { + throw new Error("Error while logging in."); } + } - console.log(`Logged in as ${client.account.name} (${client.account.id})`); - let getAllOffers = async(namespace, pagesize = 100) => { - let i = 0; - let results = []; - while ((i * pagesize) - results.length === 0) { - let { elements } = await client.getOffersForNamespace(namespace, pagesize, pagesize * i++); - results = results.concat(elements); - } - return results; - }; + console.log(`Logged in as ${client.account.name} (${client.account.id})`); + let getAllOffers = async(namespace, pagesize = 100) => { + let i = 0; + let results = []; + while ((i * pagesize) - results.length === 0) { + let { elements } = await client.getOffersForNamespace(namespace, pagesize, pagesize * i++); + results = results.concat(elements); + } + return results; + }; - let all = await getAllOffers("epic"); - let freegames = all - .filter(game => game.categories.find(cat => cat.path === "freegames") + let all = await getAllOffers("epic"); + let freegames = all + .filter(game => game.categories.find(cat => cat.path === "freegames") && game.customAttributes["com.epicgames.app.offerNs"].value) - .map(game => game.customAttributes["com.epicgames.app.offerNs"].value); + .map(game => game.customAttributes["com.epicgames.app.offerNs"].value); - for (let namespace of freegames) { - let offers = await getAllOffers(namespace); - let freeoffers = offers.filter(game => game.currentPrice === 0 && game.discountPercentage === 0); + for (let namespace of freegames) { + let offers = await getAllOffers(namespace); + let freeoffers = offers.filter(game => game.currentPrice === 0 && game.discountPercentage === 0); - for (let offer of freeoffers) { - let purchased = await client.purchase(offer, 1); + for (let offer of freeoffers) { + let purchased = await client.purchase(offer, 1); - if (purchased) { - console.log(`Successfully claimed ${offer.title} (${purchased})`); - } else { - console.log(`${offer.title} was already claimed for this account`); - } + if (purchased) { + console.log(`Successfully claimed ${offer.title} (${purchased})`); + } else { + console.log(`${offer.title} was already claimed for this account`); } } + } + + await client.logout(); + console.log(`Logged ${client.account.name} out of Epic Games`); - await client.logout(); - console.log(`Logged out of Epic Games (account ${client.account.name})`); - console.log(`Wait ${config.delay} seconds`); - await sleep(config.delay); + if (loop) { + console.log(`Waiting ${delay} minutes`); + await sleep(delay); + } else { + process.exit(0); } - } catch (e) { - console.error(e); - console.log(`Wait ${config.delay} seconds`); - await sleep(config.delay); } - process.exit(0); - } - while (config.loop); + } while (loop); })().catch(err => { console.error(err); - process.exit(-1); + process.exit(1); }); diff --git a/package.json b/package.json index 2ca9708..5df5f6b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "epicgames-freebies-claimer", - "version": "1.2.1", + "version": "1.2.2", "description": "Claim free game promotions from the Epic Game Store", "author": "Revadike ", "main": "gimme_free_epic_shit.js",