-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrapper.js
89 lines (84 loc) · 2.87 KB
/
scrapper.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
const fetch = require("node-fetch");
const fs = require('fs');
async function getVersion() {
return fetch(
"https://ddragon.leagueoflegends.com/api/versions.json"
).then(res => res.json())
.then(data => {
return data[0];
})
.catch(error => console.log(error));
}
async function getEncryptedSummonerID(region, summonerName) {
return fetch(
`https://${region}.api.riotgames.com/lol/summoner/v4/summoners/by-name/${summonerName}`,
{
headers: {
"X-Riot-Token": process.env.RIOT_TOKEN,
},
}
)
.then((response) => response.json())
.then((data) => {
return data;
})
.catch((error) => console.log(error));
}
async function getStats(region, encryptedSummonerID, queue) {
return fetch(
`https://${region}.api.riotgames.com/lol/league/v4/entries/by-summoner/${encryptedSummonerID}`,
{
headers: {
"X-Riot-Token": process.env.RIOT_TOKEN,
},
}
)
.then((response) => response.json())
.then((data) => {
for (var key in data) {
//console.log(data);
if (queue === "solo/duo") {
if (data[key].queueType === "RANKED_SOLO_5x5") {
return data[key];
}
} else if (queue === "flex") {
if (data[key].queueType === "RANKED_FLEX_SR") {
return data[key];
}
}
}
})
.catch((error) => console.log(error));
}
async function scrapper(region, summonerName, queue) {
const patch = await getVersion();
var summoner = await getEncryptedSummonerID(region, summonerName);
if (summoner.status) {
let stat = {};
stat.exists = false;
return stat;
}
stat = await getStats(region, summoner.id, queue);
if (!stat) {
stat = [];
stat.summonerName = summoner.name;
stat.profileIcon = `http://ddragon.leagueoflegends.com/cdn/${patch}/img/profileicon/${summoner.profileIconId}.png`;
stat.tier = "Unranked";
stat.rank = "0";
stat.leaguePoints = "0";
stat.winrate = "0";
stat.wins = "0";
stat.losses = "0";
stat.opgg = `https://euw.op.gg/summoners/euw/${summoner.name.replace(/ /g, "%20")}`;
return stat;
}
stat.summonerName = summoner.name;
stat.profileIcon = `http://ddragon.leagueoflegends.com/cdn/${patch}/img/profileicon/${summoner.profileIconId}.png`;
stat.winrate = (stat.wins / (stat.losses + stat.wins) * 100).toFixed(2);
stat.opgg = `https://euw.op.gg/summoners/euw/${summoner.name.replace(/ /g, "%20")}`;
for (var key in stat) {
stat[key] = stat[key].toString();
}
return stat;
}
module.exports = { scrapper };