forked from dajk/hltv-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresults.ts
70 lines (62 loc) · 1.67 KB
/
results.ts
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
import request from 'request'
import cheerio from 'cheerio'
import { CONFIG } from './config'
/**
* Scraping results
*
* @export
* @class Results
*/
export default class Results {
/**
* Creates an instance of Results.
*
* @param {any} callback
*
* @memberOf Results
*/
constructor(callback: any) {
const uri = `${CONFIG.BASE}${CONFIG.RESULTS}`
request({ uri }, (error, response, body) => {
const $ = cheerio.load(body, {
normalizeWhitespace: true,
})
const resultElements = $('.results-all .result-con')
const results: any[] = []
$(resultElements).each((i, element) => {
const el = $(element).find('tr')
const team1 = el.children('.team-cell').first()
const team2 = el.children('.team-cell').last()
const matchId = $(element)
.children('a')
.attr('href')
const maps = el.find('.map-text')
const result1 = el
.find('.result-score')
.children('span')
.first()
const result2 = el
.find('.result-score')
.children('span')
.last()
const objData = {
event: el.find('.event-name').text(),
maps: maps.text(),
team1: {
name: team1.find('.team').text(),
crest: team1.find('img').attr('src'),
result: parseInt(result1.text(), 10),
},
team2: {
name: team2.find('.team').text(),
crest: team2.find('img').attr('src'),
result: parseInt(result2.text(), 10),
},
matchId,
}
results.push(objData)
})
callback(results, error)
})
}
}