-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
94 lines (74 loc) · 2.29 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
import puppeteer from "puppeteer-extra";
import * as fs from "fs";
import StealthPlugin from "puppeteer-extra-plugin-stealth";
puppeteer.use(StealthPlugin());
async function sleep(milliseconds) {
return new Promise((resolve) => setTimeout(resolve, milliseconds));
}
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/meta", {
waitUntil: "networkidle2",
});
console.log(`get page data..`);
await page.waitForSelector("#meta-table .hero-row");
await page.waitForSelector(".nav a");
const navLinks = await page.$$(".nav a");
const allHeroData = {};
for (const link of navLinks) {
const linkText = await page.evaluate((el) => el.innerText.trim(), link);
console.log(`get meta data '${linkText}' heroes...`);
await link.click();
await sleep(2000);
const heroes = await page.evaluate(() => {
const heroElements = document.querySelectorAll("#meta-table .hero-row");
const heroData = [];
const dataIndexToProperty = [
"role",
"name",
"matches",
"winRate",
"winRate9500",
"contestRate",
"rating",
"radiantWinRate",
"direWinRate",
"expertWinRate",
"phase1WinRate",
"phase2WinRate",
"phase3WinRate",
"networth",
];
heroElements.forEach((hero, index) => {
if (index >= 5) return;
const dataElements = hero.querySelectorAll("div[data-sort-value]");
const heroInfo = {};
dataElements.forEach((element, dataIndex) => {
const property = dataIndexToProperty[dataIndex];
if (property) {
heroInfo[property] = element.dataset.sortValue;
}
});
heroData.push(heroInfo);
});
return heroData;
});
allHeroData[linkText] = heroes;
}
console.log("Heróis do Meta:", allHeroData);
// save data
const jsonOutputPath = "heroes_meta_data.json";
let data = JSON.stringify(allHeroData, null, 2);
fs.writeFile(jsonOutputPath, data, (err) => {
if (err) throw err;
console.log("Data written to file");
});
console.log(`All done, database atualized. ✨`);
await browser.close();
})
.catch((error) => {
console.error("Error during puppeteer execution:", error);
});