-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhero.js
75 lines (63 loc) · 2.08 KB
/
hero.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
import puppeteer from "puppeteer-extra";
import * as fs from "fs";
import StealthPlugin from "puppeteer-extra-plugin-stealth";
puppeteer.use(StealthPlugin());
async function getHeroData(name) {
puppeteer
.launch({ headless: true, defaultViewport: null })
.then(async (browser) => {
const page = await browser.newPage();
console.log(`Loading page dota2protracker..`);
await page.goto(`https://dota2protracker.com/hero/${name}`, {
waitUntil: "networkidle2",
});
console.log(`Getting page data..`);
await page.waitForSelector(
".flex.flex-col.lg\\:flex-row.gap-2.sticky.top-1.bg-black\\/20"
);
const heroData = await page.$$eval(
".flex.flex-col.lg\\:flex-row.gap-2.sticky.top-1.bg-black\\/20 > div",
(elements) => {
return elements
.map((element) => {
const roleElement = element.querySelector(
".flex.font-bold.gap-2.items-center.text-sm.lg\\:text-lg"
);
const winRateElement = element.querySelector(
".text-white\\/\\[0\\.4\\].text-xs.text-center > span"
);
const matchesElement = element.querySelector(
".text-white\\/\\[0\\.4\\].text-xs.text-center"
);
if (roleElement && winRateElement && matchesElement) {
const role = roleElement.innerText.trim();
const winRate = winRateElement.innerText.trim();
const matchesText = matchesElement.innerText;
const matches = matchesText.match(/(\d+) matches/)[1];
const players = matchesText.match(/by (\d+) Players/)[1];
return {
role,
winRate,
matches,
players,
};
}
return null;
})
.filter(Boolean);
}
);
const jsonOutputPath = "hero_data.json";
let data = JSON.stringify(heroData, null, 2);
fs.writeFile(jsonOutputPath, data, (err) => {
if (err) throw err;
console.log("Data written to file");
});
console.log(`Hero data, database atualized. ✨`);
await browser.close();
})
.catch((error) => {
console.error("Error during puppeteer execution:", error);
});
}
getHeroData("Anti-Mage");