-
-
Notifications
You must be signed in to change notification settings - Fork 356
/
Copy pathclaimer.js
169 lines (147 loc) · 6.68 KB
/
claimer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
"use strict";
const { writeFile, writeFileSync, existsSync, readFileSync } = require("fs");
if (!existsSync(`${__dirname}/data/config.json`)) {
writeFileSync(`${__dirname}/data/config.json`, readFileSync(`${__dirname}/data/config.example.json`));
}
if (!existsSync(`${__dirname}/data/history.json`)) {
writeFileSync(`${__dirname}/data/history.json`, "{}");
}
const { "Launcher": EpicGames } = require("epicgames-client");
const { freeGamesPromotions } = require(`${__dirname}/src/gamePromotions`);
const { latestVersion } = require(`${__dirname}/src/latestVersion.js`);
const Auths = require(`${__dirname}/data/device_auths.json`);
const Config = require(`${__dirname}/data/config.json`);
const Fork = require("child_process");
const History = require(`${__dirname}/data/history.json`);
const Logger = require("tracer").console(`${__dirname}/logger.js`);
const Package = require(`${__dirname}/package.json`);
function appriseNotify(appriseUrl, notificationMessages) {
if (!appriseUrl || notificationMessages.length === 0) {
return;
}
let notification = notificationMessages.join("\n");
try {
let s = Fork.spawnSync("apprise", [
"-vv",
"-t",
`Epicgames Freebies Claimer ${Package.version}`,
"-b",
notification,
appriseUrl,
]);
let output = s.stdout ? s.stdout.toString() : "ERROR: Maybe apprise not found?";
if (output && output.includes("ERROR")) {
Logger.error(`Failed to send push notification (${output})`);
} else if (output) {
Logger.info("Push notification sent");
} else {
Logger.warn("No output from apprise");
}
} catch (err) {
Logger.error(`Failed to send push notification (${err})`);
}
}
function write(path, data) {
// eslint-disable-next-line no-extra-parens
return new Promise((res, rej) => writeFile(path, data, (err) => (err ? rej(err) : res(true))));
}
function sleep(delay) {
return new Promise((res) => setTimeout(res, delay * 60000));
}
(async() => {
let { options, delay, loop, appriseUrl, notifyIfNoUnclaimedFreebies } = Config;
do {
Logger.info(`Epicgames Freebies Claimer (${Package.version}) by ${Package.author.name || Package.author}`);
let latest = await latestVersion().catch((err) => {
Logger.error(`Failed to check for updates (${err})`);
});
if (latest && latest !== Package.version) {
Logger.warn(`There is a new release available (${latest}): ${Package.url}`);
}
let notificationMessages = [];
for (let email in Auths) {
let { country } = Auths[email];
let claimedPromos = History[email] || [];
let newlyClaimedPromos = [];
let useDeviceAuth = true;
let rememberDevicesPath = `${__dirname}/data/device_auths.json`;
let clientOptions = { email, ...options, rememberDevicesPath };
let client = new EpicGames(clientOptions);
if (!await client.init()) {
let errMess = "Error while initialize process.";
notificationMessages.push(errMess);
Logger.error(errMess);
break;
}
// Check before logging in
let freePromos = await freeGamesPromotions(client, country, country);
let unclaimedPromos = freePromos.filter((offer) => !claimedPromos.find(
(_offer) => _offer.id === offer.id && _offer.namespace === offer.namespace,
));
Logger.info(`Found ${unclaimedPromos.length} unclaimed freebie(s) for ${email}`);
if (unclaimedPromos.length === 0) {
if (notifyIfNoUnclaimedFreebies) {
notificationMessages.push(`${email} has no unclaimed freebies`);
}
continue;
}
let success = await client.login({ useDeviceAuth }).catch(() => false);
if (!success) {
let errMess = `Failed to login as ${email}`;
notificationMessages.push(errMess);
Logger.error(errMess);
continue;
}
Logger.info(`Logged in as ${client.account.name} (${client.account.id})`);
Auths[email].country = client.account.country;
write(rememberDevicesPath, JSON.stringify(Auths, null, 4)).catch(() => false); // ignore fails
for (let offer of unclaimedPromos) {
try {
let purchased = await client.purchase(offer, 1);
if (purchased) {
Logger.info(`Successfully claimed ${offer.title} (${purchased})`);
newlyClaimedPromos.push(offer.title);
} else {
Logger.warn(`${offer.title} was already claimed for this account`);
}
// Also remember already claimed offers
offer.date = Date.now();
claimedPromos.push(offer);
} catch (err) {
notificationMessages.push(`${email} failed to claim ${offer.title}`);
Logger.error(`Failed to claim ${offer.title} (${err})`);
if (err.response
&& err.response.body
&& err.response.body.errorCode === "errors.com.epicgames.purchase.purchase.captcha.challenge") {
// It's pointless to try next one as we'll be asked for captcha again.
let errMess = "Aborting! Captcha detected.";
notificationMessages.push(errMess);
Logger.error(errMess);
break;
}
}
}
History[email] = claimedPromos;
// Setting up notification message for current account
if (newlyClaimedPromos.length > 0) {
notificationMessages.push(`${email} claimed ${newlyClaimedPromos.length} freebies: ${
newlyClaimedPromos.join(", ")}`);
} else {
notificationMessages.push(`${email} has claimed 0 freebies`);
}
await client.logout();
Logger.info(`Logged ${client.account.name} out of Epic Games`);
}
appriseNotify(appriseUrl, notificationMessages);
await write(`${__dirname}/data/history.json`, JSON.stringify(History, null, 4));
if (loop) {
Logger.info(`Waiting ${delay} minutes`);
await sleep(delay);
} else {
process.exit(0);
}
} while (loop);
})().catch((err) => {
Logger.error(err);
process.exit(1);
});