-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrapper.js
67 lines (57 loc) · 2.04 KB
/
scrapper.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
const axios = require('axios')
const jsdom = require('jsdom')
const { JSDOM } = jsdom
const championPage = (championName, championRole) => {
if (championName == null) return
let url = ''
switch(championRole) {
case 'bot' || 'bottom' || 'bas' || 'adc' :
url = `https://euw.op.gg/champion/${championName}/statistics/bot`
break
case 'supp' || 'support' :
url = `https://euw.op.gg/champion/${championName}/statistics/support`
break
case 'mid' || 'middle' || 'midlane' :
url = `https://euw.op.gg/champion/${championName}/statistics/mid`
break
case 'jungle' || 'jung' || 'jgl' :
url = `https://euw.op.gg/champion/${championName}/statistics/jungle`
break
case 'top' || 'toplane' :
url = `https://euw.op.gg/champion/${championName}/statistics/top`
break
default :
url = `https://euw.op.gg/champion/${championName}/statistics/`
}
return url
}
const getImages = (dom, selector) => {
const nodeList = dom.window.document.querySelectorAll(selector)
let array = []
Array.prototype.slice.call(nodeList).forEach(img => {
array.push(`https:${img.src.split('.png')[0]}.png`)
})
return array
}
const scrape = async (name, role) => {
const championName = name.toLowerCase()
const championRole = role.toLowerCase()
const url = championPage(championName, championRole)
const html = await axios.get(url)
const dom = new JSDOM(html.data)
let data = {
runes:{
masteries:[],
fragments:[],
},
url:url
}
// img[src*="//opgg-static.akamaized.net/images/lol/item/"]
let masteriesSelector = '.perk-page__item--active > div > img'
let fragmentsSelector = '.fragment > div > img.active'
data.runes.masteries = getImages(dom, masteriesSelector).slice(0,6)
data.runes.fragments = getImages(dom, fragmentsSelector).slice(0,3)
// console.log(data)
return data
}
module.exports = scrape