-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
62 lines (53 loc) · 2.58 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
const axios = require('axios');
const jsdom = require('jsdom');
async function xboxGamertag(gamertag) {
try {
const response = await axios({
url: `https://xboxgamertag.com/search/${gamertag}`,
method: 'get'
});
const { window } = new jsdom.JSDOM(response.data);
const userAvatar = window.document.querySelector('img[class="rounded img-thumbnail"]');
const userDetails = Array.from(window.document.querySelectorAll('div[class="col-auto profile-detail-item"]'));
const gameName = Array.from(window.document.querySelectorAll('div[class="game-card-desc"] > h3'));
const lastPlayed = Array.from(window.document.querySelectorAll('div[class="game-card-desc"] > p[class="text-sm"]'));
const gamePlatform = Array.from(window.document.querySelectorAll('div[class="game-card-desc"] > p[class="text-xs"]'));
const gameAchievements = Array.from(window.document.querySelectorAll('div[class="game-card-desc"] div[class="col-9 font-weight-bold"]'));
const gameProgress = Array.from(window.document.querySelectorAll('div[class="progress"] div[class="progress-bar"]'));
const result = {
gamertag: gamertag,
avatar: `https:${userAvatar.src}`,
detail: {},
history: []
};
userDetails.forEach(detail => {
const content = detail.textContent.trim().split('\n').map(line => line.trim());
for (let i = 0; i < content.length; i++) {
if (content[i] === 'Gamerscore') {
result.detail.gamerscore = content[i + 1];
} else if (content[i] === 'Games Played') {
result.detail.gamesplayed = content[i + 1];
}
}
});
for (let i = 0; i < gameName.length; i++) {
result.history.push({
gamename: gameName[i].textContent.trim(),
lastplayed: lastPlayed[i].textContent.trim(),
platform: gamePlatform[i].textContent.trim(),
gamescore: gameAchievements[i * 2] == undefined ? undefined : gameAchievements[i * 2].textContent.trim(),
achievement: gameAchievements[i * 2 + 1] == undefined ? undefined : gameAchievements[i * 2 + 1].textContent.trim(),
progress: gameProgress[i].textContent.trim(),
});
}
return {
message: 'Successfully found Gamertag!',
profile: result
}
} catch (err) {
return {
message: 'Gamertag not found!'
}
}
}
module.exports = xboxGamertag;