-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
69 lines (51 loc) · 2.04 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
const axios = require('axios').default
const cheerio = require('cheerio')
const datefns = require('date-fns')
const fs = require('fs')
const path = require('path')
const champs = require('./champs.json')
const baseUrl = 'https://www.leagueofgraphs.com/es/rankings/summoners/'
const resultsFolderPath = path.resolve(__dirname, 'results')
const requests = champs.map(champ => axios.get(baseUrl + champ).then(res => handleChampionPageResponse(res, champ)))
console.log('Scraping... \n')
Promise.all(requests)
.then(res => {
createResultsFolder()
storeResults(res)
})
.then(() => console.log('\nScraping complete!'))
.catch(console.error)
function handleChampionPageResponse(res, champ) {
console.log('Scraping ' + champ)
const percentageContainers = getPercentageContainers(res.data)
const percentages = getPercentages(percentageContainers)
const total = percentages.reduce((acc, next) => acc + next, 0)
const mean = (total / percentages.length).toFixed(2)
return [champ, mean]
}
function getPercentageContainers(html) {
const $ = cheerio.load(html)
return $('.data_table > tbody')
.children()
.map((_, element) => $(element).children('td:nth-child(4)').text())
.toArray()
}
function getPercentages(percentageContainers) {
return percentageContainers
.map(element => element.toString())
.map(elementAsString => elementAsString.match(/\d.?\.\d/))
.filter(percentageMatch => percentageMatch != null)
.map(percentageMatch => percentageMatch[0])
.map(percentageAsString => parseFloat(percentageAsString))
}
function createResultsFolder() {
if (!fs.existsSync(resultsFolderPath)) {
fs.mkdirSync(resultsFolderPath)
}
}
function storeResults(res) {
const resultFileName = path.resolve(resultsFolderPath, datefns.format(new Date(), 'YYYY-MM-DD') + '.csv')
const resultAsCsv = res.map(pair => pair.join(',')).join('\n')
fs.writeFileSync(resultFileName, resultAsCsv)
console.log('\nResults stored in ' + resultFileName)
}