-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
238 lines (201 loc) · 6.96 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
const puppeteer = require("puppeteer-extra");
const AdblockerPlugin = require("puppeteer-extra-plugin-adblocker");
const { JSDOM } = require("jsdom");
puppeteer.use(AdblockerPlugin());
exports.getStats = async (user, region, refresh) => {
try {
const browser = await puppeteer.launch({ headless: true });
const context = browser.defaultBrowserContext();
context.overridePermissions(`https://${region}.op.gg`, [
"geolocation",
"notifications",
]);
const page = await browser.newPage();
await page.goto(`https://${region}.op.gg/summoner/userName=${user}`);
if (refresh === true) {
await page.click("#SummonerRefreshButton");
page.on("dialog", async (dialog) => {
await dialog.accept();
});
await page.waitForSelector("div.GameItemList");
await page.waitForTimeout(5000);
} else {
}
await page.waitForTimeout(1000);
await page.click("#right_gametype_soloranked").catch(error=>console.error("this player hasn't played ranked yet"));
await page.waitForTimeout(1000);
const wins = await page
.$eval("span.win", (e) => e.innerText)
.catch(() => {
return "unraked";
});
const loses = await page
.$eval("span.lose", (e) => e.innerText)
.catch(() => {
return "unraked";
});
const level = await page.$eval(".Level", (e) => e.innerText);
const kdaRatio = await page.$eval(
".KDARatio",
(e) => e.firstElementChild.textContent
);
const rank = await page.$eval(".TierRank", (e) => e.innerText);
const pdl = await page
.$eval(".LeaguePoints", (e) => e.innerText)
.catch(() => {
return "Unranked";
});
const main = await page
.$$eval(".PositionStatContent .Name", (e) => e[0].innerHTML)
.catch(() => {
return "unraked";
});
const mainChampion = await page
.$$eval(".MostChampionContent .ChampionName", (e) => e[0].innerText)
.catch(() => {
return "unraked";
});
const winrate = await page
.$eval("#GameAverageStatsBox-matches div.Text", (e) => e.innerText)
.catch(() => {
return "none";
});
const lastTime = await page
.$$eval(".TimeStamp ._timeago", (e) => e[0].innerText)
.catch(() => {
return "inactive player";
});
const stats = {
Name: user,
Level: level,
RecentWins: wins,
RecentLoses: loses,
Rank: rank,
KDARatio: kdaRatio,
RankedLP: pdl,
WinRate: winrate,
MainLane: main,
MainChampion: mainChampion,
LastTimeOnline: lastTime,
};
await browser.close();
return stats;
} catch (e) {
console.error(e);
}
};
exports.getLiveMatch = async (user, region) => {
try {
const browser = await puppeteer.launch({ headless: true });
const context = browser.defaultBrowserContext();
context.overridePermissions(`https://${region}.op.gg`, [
"geolocation",
"notifications",
]);
const page = await browser.newPage();
await page.goto(`https://${region}.op.gg/summoner/userName=${user}`);
await page.waitForTimeout(1000);
await page.click("dd.inGame");
await page.waitForTimeout(2000);
const isNotActive = await page
.$eval(".SpectatorError", (e) => e.outerHTML)
.catch(() => {
return undefined;
});
if (isNotActive != undefined) {
await browser.close();
return { error: 1 };
}
const team1 = await page
.$eval("table.Team-100", (e) => e.outerHTML)
.catch(() => {
return "unraked";
});
const team2 = await page
.$eval("table.Team-200", (e) => e.outerHTML)
.catch(() => {
return "unraked";
});
data = {
teamA: [],
teamB: [],
error: 0,
};
let document1 = new JSDOM(team1).window.document;
for (let i = 1; i < 6; i++) {
let tempRow = {};
let currentRow = document1.querySelector(
`tbody>tr:nth-of-type(${JSON.stringify(i)})`
);
let championName = currentRow.children[0].children[0].href;
championName = championName.substring(10, championName.length - 11);
tempRow["champion"] = championName;
tempRow["spell1"] = currentRow.children[1].children[0].title;
tempRow["spell2"] = currentRow.children[1].children[1].title;
tempRow[
"tierRank"
] = currentRow.children[5].children[0].innerHTML.replace(/\s/g, "");
tempRow["currentSeasonPlayed"] =
currentRow.children[6].children[1] === undefined
? "-"
: currentRow.children[6].children[1].innerHTML.replace(/\s/g, "");
tempRow["winRatio"] =
currentRow.children[6].children[0] === undefined
? "-"
: currentRow.children[6].children[0].innerHTML;
tempRow["currentChampionPlayed"] =
currentRow.children[7].children[0] === undefined
? "-"
: currentRow.children[7].children[0].children[1].innerHTML;
tempRow["currentChampionWinRatio"] =
currentRow.children[7].children[0] === undefined
? "-"
: currentRow.children[7].children[0].children[0].innerHTML;
tempRow["currentChampionKDA"] =
currentRow.children[8].children[0] === undefined
? "-"
: currentRow.children[8].children[0].children[0].innerHTML;
data.teamA.push(tempRow);
}
let document2 = new JSDOM(team2).window.document;
for (let i = 1; i < 6; i++) {
let tempRow = {};
let currentRow = document2.querySelector(
`tbody>tr:nth-of-type(${JSON.stringify(i)})`
);
let championName = currentRow.children[0].children[0].href;
championName = championName.substring(10, championName.length - 11);
tempRow["champion"] = championName;
tempRow["spell1"] = currentRow.children[1].children[0].title;
tempRow["spell2"] = currentRow.children[1].children[1].title;
tempRow[
"tierRank"
] = currentRow.children[5].children[0].innerHTML.replace(/\s/g, "");
tempRow["currentSeasonPlayed"] =
currentRow.children[6].children[1] === undefined
? "-"
: currentRow.children[6].children[1].innerHTML.replace(/\s/g, "");
tempRow["winRatio"] =
currentRow.children[6].children[0] === undefined
? "-"
: currentRow.children[6].children[0].innerHTML;
tempRow["currentChampionPlayed"] =
currentRow.children[7].children[0] === undefined
? "-"
: currentRow.children[7].children[0].children[1].innerHTML;
tempRow["currentChampionWinRatio"] =
currentRow.children[7].children[0] === undefined
? "-"
: currentRow.children[7].children[0].children[0].innerHTML;
tempRow["currentChampionKDA"] =
currentRow.children[8].children[0] === undefined
? "-"
: currentRow.children[8].children[0].children[0].innerHTML;
data.teamB.push(tempRow);
}
await browser.close();
return data;
} catch (e) {
console.error(e);
}
};