-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathindex.js
501 lines (433 loc) · 19.1 KB
/
index.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
#!/usr/bin/env node
// Load neccessary modules
var async = require('async');
var cheerio = require('cheerio');
var fs = require('fs');
var request = require('request');
var FileCookieStore = require('tough-cookie-filestore');
// Array with default headers
var DEFAULT_HEADERS = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'en-US,en;q=0.8,nl;q=0.6',
'Connection': 'keep-alive',
'Host': 'socialclub.rockstargames.com',
'Origin': 'https://socialclub.rockstargames.com',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'
};
// Check if config file exists
if (fs.existsSync('config.json')) {
var config = require('./config.json');
} else {
console.error('[Error] Couldn\'t find configuration file "config.json"');
process.exit(1);
}
/**
* Write debug message if debug mode is enabled
* in configuration file
* @param {String} msg
* @return {String}
*/
function debug(msg){
if (config.debug) {
console.log(msg);
}
}
/**
* Start the script.
*/
function start(forceNewLogin) {
// Force a new login to SocialClub, for example when the cookies are not valid anymore
if (forceNewLogin) {
renewAuthentication(function (cookieJar, verificationToken) {
printActualInformation(cookieJar);
});
return;
}
debug('Checking for existing cookies and verification token');
async.parallel({
getCookiesFile: function (done) {
readFile('cookies.json', function (err, cookieFileData) {
done(err, cookieFileData);
});
},
getVerificationTokenFile: function (done) {
readFile('verificationToken.txt', function (err, verificationFileData) {
done(err, verificationFileData);
});
}
}, function (err, results) {
if (err) {
console.error(err);
return;
}
var cookieFileData = results['getCookiesFile'];
var verificationToken = results['getVerificationTokenFile'];
// If one or both files doesn't exist, renew the authentication
if (!cookieFileData || !verificationToken) {
debug('No existing cookies and/or verification token found');
start(true);
return;
}
debug('Existing cookies and verification token found');
try {
// Use the existing cookies and verification token
var cookieJar = request.jar(new FileCookieStore('cookies.json'));
printActualInformation(cookieJar);
} catch (err) {
// Sometimes an exception is thrown because the cookies.json file contains invalid JSON for some reason
if (err instanceof SyntaxError) {
debug('Existing cookies are unreadable');
// So renew the authentication in that case
start(true);
} else {
console.error(err);
}
}
});
}
/**
* Get the contents of a file. If it doesn't exist, an empty string is given to the callback.
* @param {string} filePath - The file path of the file to be read.
* @param {function} callback - Function that gets called when the contents of the verification token file is retrieved.
*/
function readFile(filePath, callback) {
fs.readFile(filePath, 'utf8', function (err, data) {
if (err && err.code === 'ENOENT') {
callback('');
return;
}
callback(err, data);
});
}
/**
* Get the current timestamp.
* @returns {number} - The current timestamp.
*/
function getTimestamp() {
return Math.floor(new Date().getTime());
}
/**
* Parse a string to an integer, including stripping the 'per mille' sign (,) and the dollar sign ($).
* @param {string} input - The input to be parsed to an integer.
* @returns {number} - The parsed integer.
*/
function parseInteger(input) {
return parseInt(input.replace(/[\$,]/g, '').trim());
}
/* -------------------------------------- */
/**
* Function to sign into SocialClub to get a "AuthenticateCookie" cookie.
* @param {function} callback - Function that gets called when the authentication is renewed.
*/
function renewAuthentication(callback) {
debug('Renewing the authentication');
createCookiesJar(function (cookieJar) {
getVerificationToken(cookieJar, function (cookieJar, verificationToken) {
signIn(cookieJar, verificationToken, function (cookieJar) {
callback(cookieJar, verificationToken);
});
});
});
}
/**
* Creates a new cookie jar.
* @param callback - Function that gets called when the cookie jar is created.
*/
function createCookiesJar(callback) {
// Create the cookies jar file
fs.writeFile('cookies.json', '', function (err) {
if (err) {
console.error(err);
return;
}
// Create an empty cookie jar, which will be filled with cookies
var cookieJar = request.jar(new FileCookieStore('cookies.json'));
callback(cookieJar);
});
}
/**
* Remember a SocialClub verification token. It is saved to a .txt file.
* @param {string} verificationToken - The SocialClub verification token to be remembered.
* @param callback - Function that gets called when the verification token is saved.
*/
function rememberVerificationToken(verificationToken, callback) {
fs.writeFile('verificationToken.txt', verificationToken, function (err) {
if (err) {
console.error(err);
return;
}
callback();
});
}
/**
* Function to retrieve the login page of SocialClub to get a verification token.
* @param {RequestJar} cookieJar - An (empty) cookie jar.
* @param {function} callback - Function that gets called when the verification token is retrieved.
*/
function getVerificationToken(cookieJar, callback) {
var options = {
gzip: true,
headers: DEFAULT_HEADERS,
jar: cookieJar,
url: 'https://socialclub.rockstargames.com/profile/signin'
};
debug('Send GET-request to: ' + options.url);
request.get(options, function (err, response, body) {
if (err) {
console.error(err);
return;
}
// Caution: There are multiple "__RequestVerificationToken" in the body, take the right one
var regExpVerificationToken = new RegExp('</li>[^]*<input name="__RequestVerificationToken" type="hidden" value="(.*)" \/><li class="twitter">');
var match = body.match(regExpVerificationToken);
var verificationToken = match ? match[1] : null;
// When the verification token is not found, the regular expression could be outdated
if (!verificationToken) {
console.error('Retrieving the verification token from the homepage of SocialClub failed. ' +
'It could be that R* has moved the position of the verification token field, ' +
'or that they have changed everything and this script doesn\'t work anymore.');
return;
}
debug('Retrieved verification token: ' + verificationToken);
// Remember the verification token
rememberVerificationToken(verificationToken, function () {
// Return the cookie jar filled with some cookies and the verification token
callback(cookieJar, verificationToken);
});
});
}
/**
* Function to sign into SocialClub to get the "AuthenticateCookie" cookie in the cookie jar.
* @param {RequestJar} cookieJar - The cookie jar filled with some cookies from the login page of SocialClub.
* @param {string} verificationToken - The verification token that was retrieved from the homepage of SocialClub.
* @param {function} callback - Function that gets called when the sign in was successful.
*/
function signIn(cookieJar, verificationToken, callback) {
var headers = DEFAULT_HEADERS;
headers['Accept'] = 'application/json, text/javascript, */*; q=0.01';
headers['Content-Type'] = 'application/json; charset=UTF-8';
headers['RequestVerificationToken'] = verificationToken;
var options = {
body: {
'login': config.username,
'password': config.password,
'rememberme': true
},
gzip: true,
headers: headers,
jar: cookieJar,
json: true,
url: 'https://socialclub.rockstargames.com/profile/signincompact'
};
debug('Send POST-request to: ' + options.url);
request.post(options, function (err, response, body) {
if (err) {
console.error(err);
return;
}
// console.log('Request HTTP headers:', response.request.headers);
// console.log('Response HTTP headers:', response.headers);
// When a CAPTCHA is requested, a status code of 403 is returned
if (response.statusCode === 403) {
console.error('Signing into SocialClub failed, probably because a CAPTCHA is requested. ' +
'Using this machine, first sign into SocialClub manually using a browser ' +
'and then try to run this script again.');
return;
}
// We should get a status code of 200
if (response.statusCode !== 200) {
console.error('Signing into SocialClub failed. The problem could be anything. ' +
'Please make sure the correct headers are in the HTTP request.');
return;
}
var cookies = response.headers['set-cookie'];
var authenticationCookieReceived = false;
for (var i = 0; i < cookies.length; i++) {
if (cookies[i].indexOf('AuthenticateCookie') !== -1) {
authenticationCookieReceived = true;
break;
}
}
if (!authenticationCookieReceived) {
console.error('No authentication cookie has been sent. Did you provide the correct credentials?');
return;
}
callback(cookieJar);
});
}
/* -------------------------------------- */
/**
* Calculate the actual percentage of a progress bar statistic.
* @param {Cheerio} $ - A Cheerio object loaded with the HTML of the page containing the personal information.
* @param {string} statsType - The statistic type of which the percentage must be calculated.
* @returns {number} - The calculated percentage of the given statistic type.
*/
function calculateProgressbarStats($, statsType) {
var percentage = 0;
var statsPercentages = $('h5:contains("' + statsType + '")').next().find($('.progress-bar'));
for (var i = 0; i < 5; i++) {
var subPercentage = parseInteger(statsPercentages.eq(i).text());
if (!subPercentage) {
subPercentage = 0;
}
percentage += subPercentage;
}
return percentage / 5;
}
/**
* Constructs an array for all recent activities.
* @param {Cheerio} $ - A Cheerio object loaded with the HTML of the page containing the personal information.
* @returns {object[]} - The array with all recent activities.
*/
function getRecentActivity($) {
var activities = $('#recentActivity ul').find('li[data-type=award]');
var arr = [];
activities.each(function (i, elem) {
arr.push({
'name': $(this).attr('data-name').trim(),
'type': $(this).attr('data-award').trim(),
'image': $(this).find('img').eq(0).attr('src').trim()
});
});
return arr;
}
/**
* Request, parse and print the actual information.
* @param {RequestJar} cookieJar - The cookie jar including the "AuthenticateCookie" cookie.
*/
function printActualInformation(cookieJar) {
var target = process.argv[2] ? process.argv[2] : config.username;
var headers = DEFAULT_HEADERS;
var options = {
gzip: true,
headers: headers,
jar: cookieJar,
json: true,
url: 'http://socialclub.rockstargames.com/games/gtav/career/overviewAjax?character=Freemode&nickname=' + target + '&slot=Freemode&gamerHandle=&gamerTag=&_=' + getTimestamp()
};
debug('Send GET-request to: ' + options.url);
request.get(options, function (err, response, body) {
if (err) {
console.error(err);
return;
}
// Cookies and/or verification code are probably invalid, so renew the authentication and try again
if (response.statusCode !== 200) {
start(true);
return;
}
var $ = cheerio.load(body.replace(/\\r\\n|\\n|\\r/g, ''));
var crewObj = {};
var favouriteWeapon = {};
// When the user is in a crew
if (body.indexOf('Not In A Crew') === -1) {
crewObj = {
'name': $('.crewCard .clearfix .left h3 a').first().text().trim(),
'tag': $('.crewCard .clearfix .left .crewTag span').first().text().trim(),
'emblem': $('.crewCard .clearfix .avatar').first().attr('src').trim()
};
}
// When the user has a favorite weapon
if (!$('#faveWeaponWrapper').has($('.noData'))) {
favouriteWeapon = {
'name': $('#faveWeaponWrapper .imageHolder h4').first().text().trim(),
'image': $('#faveWeaponWrapper .imageHolder img').first().attr('src').trim(),
'stats': {
'damage': $('.weaponStats tr td:contains("Damage") span').text().trim(),
'fire-rate': $('.weaponStats tr td:contains("Fire Rate") span').text().trim(),
'accuracy': $('.weaponStats tr td:contains("Accuracy") span').text().trim(),
'range': $('.weaponStats tr td:contains("Range") span').text().trim(),
'clip-size': $('.weaponStats tr td:contains("Clip Size") span').text().trim()
},
'kills': parseInteger($('h5:contains("Kills")').next().text()),
'headshots': parseInteger($('h5:contains("Headshots")').next().text()),
'accuracy': $('h5:contains("Accuracy")').next().text().trim(),
'time-held': $('h5:contains("Time held")').next().text().trim()
};
}
// The actual information object
var obj = {
'general': {
'rank': parseInt($('.rankHex h3').first().text().trim()),
'xp': parseInt($('.rankXP .clearfix .left').first().text().trim()),
'play-time': $('.rankBar h4').first().text().replace('Play Time: ', '').trim(),
'money': {
'cash': $('#cash-value').first().text().trim(),
'bank': $('#bank-value').first().text().trim()
}
},
'crew': crewObj,
'freemode': {
'races': {
'wins': parseInteger($('p[data-name="Races"]').first().attr('data-win')),
'losses': parseInteger($('p[data-name="Races"]').first().attr('data-loss')),
'time': $('p[data-name="Races"]').first().attr('data-extra').trim()
},
'deathmatches': {
'wins': parseInteger($('p[data-name="Deathmatches"]').first().attr('data-win')),
'losses': parseInteger($('p[data-name="Deathmatches"]').first().attr('data-loss')),
'time': $('p[data-name="Deathmatches"]').first().attr('data-extra').trim()
},
'parachuting': {
'wins': parseInteger($('p[data-name="Parachuting"]').first().attr('data-win')),
'losses': parseInteger($('p[data-name="Parachuting"]').first().attr('data-loss')),
'perfect-landing': parseInteger($('p[data-name="Parachuting"]').first().attr('data-extra'))
},
'darts': {
'wins': parseInteger($('p[data-name="Darts"]').first().attr('data-win')),
'losses': parseInteger($('p[data-name="Darts"]').first().attr('data-loss')),
'six-darter': parseInteger($('p[data-name="Darts"]').first().attr('data-extra'))
},
'tennis': {
'wins': parseInteger($('p[data-name="Tennis"]').first().attr('data-win')),
'losses': parseInteger($('p[data-name="Tennis"]').first().attr('data-loss')),
'aces': parseInteger($('p[data-name="Tennis"]').first().attr('data-extra'))
},
'golf': {
'wins': parseInteger($('p[data-name="Golf"]').first().attr('data-win')),
'losses': parseInteger($('p[data-name="Golf"]').first().attr('data-loss')),
'hole-in-one': parseInteger($('p[data-name="Golf"]').first().attr('data-extra'))
}
},
'money': {
'total': {
'spent': $('#cashSpent p').first().text().trim(),
'earned': $('#cashEarned p').first().text().trim()
},
'earnedby': {
'jobs': $('.cash-val[data-name="Jobs"]').first().attr('data-cash').trim(),
'shared': $('.cash-val[data-name="Shared"]').first().attr('data-cash').trim(),
'betting': $('.cash-val[data-name="Betting"]').first().attr('data-cash').trim(),
'car-sales': $('.cash-val[data-name="Car Sales"]').first().attr('data-cash').trim(),
'picked-up': $('.cash-val[data-name="Picked Up"]').first().attr('data-cash').trim(),
'other': $('.cash-val[data-name="Other"]').first().attr('data-cash').trim()
}
},
'stats': {
'stamina': calculateProgressbarStats($, 'Stamina') + '%',
'stealth': calculateProgressbarStats($, 'Stealth') + '%',
'lung-capacity': calculateProgressbarStats($, 'Lung Capacity') + '%',
'flying': calculateProgressbarStats($, 'Flying') + '%',
'shooting': calculateProgressbarStats($, 'Shooting') + '%',
'strength': calculateProgressbarStats($, 'Strength') + '%',
'driving': calculateProgressbarStats($, 'Driving') + '%',
'mental-state': calculateProgressbarStats($, 'Mental State') + '%'
},
'criminalrecord': {
'cops-killed': parseInteger($('h5:contains("Cops killed")').next().text()),
'wanted-stars': parseInteger($('h5:contains("Wanted stars attained")').next().text()),
'time-wanted': $('h5:contains("Time Wanted")').next().text().trim(),
'stolen-vehicles': parseInteger($('h5:contains("Vehicles Stolen")').next().text()),
'cars-exported': parseInteger($('h5:contains("Cars Exported")').next().text()),
'store-holdups': parseInteger($('h5:contains("Store Hold Ups")').next().text())
},
'favourite-weapon': favouriteWeapon,
'recent-activity': getRecentActivity($)
};
// Print the actual information object
console.log(JSON.stringify(obj, null, 4));
});
}
/* -------------------------------------- */
start(false);