/
getPlayerRanking.ts
90 lines (82 loc) · 2.77 KB
/
getPlayerRanking.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import { stringify } from 'querystring'
import { HLTVConfig } from '../config'
import { HLTVScraper } from '../scraper'
import { BestOfFilter } from '../shared/BestOfFilter'
import { GameMap, toMapFilter } from '../shared/GameMap'
import { MatchType } from '../shared/MatchType'
import { Player } from '../shared/Player'
import { RankingFilter } from '../shared/RankingFilter'
import { Team } from '../shared/Team'
import { fetchPage, getIdAt } from '../utils'
export interface PlayerRanking {
player: Player
teams: Team[]
maps: number
kdDiff: number
rounds: number
kd: number
rating1?: number
rating2?: number
}
export interface GetPlayerRankingOptions {
startDate?: string
endDate?: string
matchType?: MatchType
rankingFilter?: RankingFilter
maps?: GameMap[]
minMapCount?: number
countries?: string[]
bestOfX?: BestOfFilter
}
export const getPlayerRanking =
(config: HLTVConfig) =>
async (options: GetPlayerRankingOptions = {}): Promise<PlayerRanking[]> => {
const query = stringify({
...(options.startDate ? { startDate: options.startDate } : {}),
...(options.endDate ? { endDate: options.endDate } : {}),
...(options.matchType ? { matchType: options.matchType } : {}),
...(options.rankingFilter
? { rankingFilter: options.rankingFilter }
: {}),
...(options.maps ? { maps: options.maps.map(toMapFilter) } : {}),
...(options.minMapCount ? { minMapCount: options.minMapCount } : {}),
...(options.countries ? { country: options.countries } : {}),
...(options.bestOfX ? { bestOfX: options.bestOfX } : {})
})
const $ = HLTVScraper(
await fetchPage(
`https://www.hltv.org/stats/players?${query}`,
config.loadPage
)
)
return $('.player-ratings-table tbody tr')
.toArray()
.map((el) => {
const id = el.find('.playerCol a').attrThen('href', getIdAt(3))
const name = el.find('.playerCol a').text()
const player = { name, id }
const teams = el
.find('.teamCol a')
.toArray()
.map((teamEl) => ({
id: teamEl.attrThen('href', getIdAt(3)),
name: teamEl.find('img').attr('title')
}))
const maps = el.find('td.statsDetail').eq(0).numFromText()!
const rounds = el.find('td.statsDetail').eq(1).numFromText()!
const kdDiff = el.find('td.kdDiffCol').numFromText()!
const kd = el.find('td.statsDetail').eq(2).numFromText()!
const rating = el.find('td.ratingCol').numFromText()!
return {
player,
teams,
maps,
kdDiff,
rounds,
kd,
...($('.ratingCol .ratingDesc').text() === '2.0'
? { rating2: rating }
: { rating1: rating })
}
})
}