-
-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathgetTeamRanking.ts
More file actions
84 lines (74 loc) · 1.96 KB
/
getTeamRanking.ts
File metadata and controls
84 lines (74 loc) · 1.96 KB
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
import { HLTVConfig } from '../config'
import { HLTVScraper } from '../scraper'
import { Team } from '../shared/Team'
import { fetchPage, getIdAt } from '../utils'
export interface TeamRanking {
team: Team
points: number
place: number
change: number
isNew: boolean
}
export interface GetTeamArguments {
year?: 2015 | 2016 | 2017 | 2018 | 2019 | 2020 | 2021 | 2022
month?:
| 'january'
| 'february'
| 'march'
| 'april'
| 'may'
| 'june'
| 'july'
| 'august'
| 'september'
| 'october'
| 'november'
| 'december'
day?: number
country?: string
}
export const getTeamRanking =
(config: HLTVConfig) =>
async ({ year, month, day, country }: GetTeamArguments = {}): Promise<
TeamRanking[]
> => {
let $ = HLTVScraper(
await fetchPage(
`https://www.hltv.org/ranking/teams/${year ?? ''}/${month ?? ''}/${
day ?? ''
}`,
config.loadPage
)
)
if (country) {
const redirectedLink = $('.ranking-country > a').first().attr('href')
const countryRankingLink = redirectedLink
.split('/')
.slice(0, -1)
.concat(country)
.join('/')
$ = HLTVScraper(
await fetchPage(
`https://www.hltv.org/${countryRankingLink}`,
config.loadPage
)
)
}
const teams = $('.ranked-team')
.toArray()
.map((el) => {
const points = Number(
el.find('.points').text().replace(/\(|\)/g, '').split(' ')[0]
)
const place = Number(el.find('.position').text().substring(1))
const team = {
name: el.find('.name').text(),
id: el.find('.moreLink').attrThen('href', getIdAt(2))
}
const changeText = el.find('.change').text()
const isNew = changeText === 'NEW TEAM'
const change = changeText === '-' || isNew ? 0 : Number(changeText)
return { points, place, team, change, isNew }
})
return teams
}