-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch.js
128 lines (104 loc) · 3.25 KB
/
fetch.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
const puppeteer = require('puppeteer');
//const fs = require('fs');
const { formatValue } = require('./helper');
module.exports.fetch = async (profile) => {
await sleep(1000);
const name = profile._name;
const system = profile._system;
console.log(`Running fetch for ${name}...`);
console.log('Launching browser...');
const browser = await puppeteer.launch({
args: ['--no-sandbox', '--disable-setuid-sandbox'],
});
try {
console.log('Navigating to page...');
const page = await browser.newPage();
await page.goto(
`https://playoverwatch.com/en-us/career/${system}/${name}/`,
{
waitUntil: 'networkidle0',
}
);
const NO_PROFILE = await page.evaluate(() => {
const profileNotFound = document.querySelector('section > div > h1');
if (profileNotFound) {
return true;
}
return false;
});
if (await NO_PROFILE) {
throw new Error(`No profile found for ${name} on system ${system}.`);
}
console.log('Getting heroes...');
const HEROES = await page.evaluate(() =>
Array.from(
document.querySelectorAll('select[data-group-id="stats"] > option'),
(option) => option.innerText
)
);
console.log('Getting stats...');
const HERO_STATS = await page.evaluate(() => {
let characterStats = [];
const STAT_DIVS = Array.from(
document.querySelectorAll('div[data-group-id="stats"]'),
(div) => div.childNodes
);
for (let i = 0; i < STAT_DIVS.length; i++) {
let fields = {};
STAT_DIVS[i].forEach((stat) => {
stat
.querySelectorAll('div > div > table > tbody > tr')
.forEach((row) => {
const key = row.cells[0].innerText.replace(/ /g, '\\ ');
const value = row.cells[1].innerText;
fields[key] = value;
});
});
characterStats.push({
measurement: '',
tags: { character: '', playType: '', system: '' },
fields,
});
}
return characterStats;
});
let playType = 'QUICK\\ PLAY';
console.log('Closing browser...');
await browser.close();
console.log('Formatting data...');
for (let i = 0; i < HERO_STATS.length; i++) {
let hero = HEROES[i];
if (i > 0 && hero === 'ALL HEROES') {
playType = 'COMPETITIVE\\ PLAY';
}
HERO_STATS[i].measurement = name;
HERO_STATS[i].tags.character = hero.replace(/ /g, '\\ ');
HERO_STATS[i].tags.playType = playType;
HERO_STATS[i].tags.system = system;
Object.keys(HERO_STATS[i].fields).map(
(key) =>
(HERO_STATS[i].fields[key] = formatValue(HERO_STATS[i].fields[key]))
);
}
/* fs.writeFile('./data.json', JSON.stringify(HERO_STATS), (err) =>
console.log(err)
); */
profile.stats = HERO_STATS;
return;
} catch (error) {
await browser.close();
if (error.message.includes('No profile')) {
profile.noProfile = true;
profile.stats = null;
console.log(error.message);
return;
}
return;
}
};
function sleep(ms) {
return new Promise((resolve) => {
console.log(`Waiting ${ms / 1000} second(s) for project to load...`);
setTimeout(resolve, ms);
});
}