-
-
Notifications
You must be signed in to change notification settings - Fork 110
/
getTeam.ts
148 lines (136 loc) · 3.84 KB
/
getTeam.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import { HLTVConfig } from '../config'
import { HLTVScraper } from '../scraper'
import { Article } from '../shared/Article'
import { Country } from '../shared/Country'
import { Player } from '../shared/Player'
import { fetchPage, generateRandomSuffix, getIdAt, parseNumber } from '../utils'
export enum TeamPlayerType {
Coach = 'Coach',
Starter = 'Starter',
Substitute = 'Substitute',
Benched = 'Benched'
}
export interface FullTeamPlayer extends Player {
type: TeamPlayerType
timeOnTeam: string
mapsPlayed: number
}
export interface FullTeam {
id: number
name: string
logo?: string
facebook?: string
twitter?: string
instagram?: string
country: Country
rank?: number
players: FullTeamPlayer[]
rankingDevelopment: number[]
news: Article[]
}
export const getTeam =
(config: HLTVConfig) =>
async ({ id }: { id: number }): Promise<FullTeam> => {
const $ = HLTVScraper(
await fetchPage(
`https://www.hltv.org/team/${id}/${generateRandomSuffix()}`,
config.loadPage
)
)
const name = $('.profile-team-name').text()
const logoSrc = $('.teamlogo').attr('src')
const logo = logoSrc.includes('placeholder.svg') ? undefined : logoSrc
const facebook = $('.facebook').parent().attr('href')
const twitter = $('.twitter').parent().attr('href')
const instagram = $('.instagram').parent().attr('href')
const rank = parseNumber(
$('.profile-team-stat .right').first().text().replace('#', '')
)
const players = $('.players-table tbody tr')
.toArray()
.map((el) => ({
name: el
.find(
'.playersBox-playernick-image .playersBox-playernick .text-ellipsis'
)
.text(),
id: el
.find('.playersBox-playernick-image')
.attrThen('href', getIdAt(2)),
timeOnTeam: el.find('td').eq(2).trimText()!,
mapsPlayed: el.find('td').eq(3).numFromText()!,
type: getPlayerType(el.find('.player-status').text())!
}))
.concat(
...($('.coach-table').exists()
? [
{
id: $('.coach-table .playersBox-playernick-image').attrThen(
'href',
getIdAt(2)
),
name: $(
'.coach-table .playersBox-playernick-image .playersBox-playernick .text-ellipsis'
).text(),
timeOnTeam: $('.coach-table tbody tr')
.first()
.find('td')
.eq(1)
.trimText()!,
mapsPlayed: $('.coach-table tbody tr')
.first()
.find('td')
.eq(2)
.numFromText()!,
type: TeamPlayerType.Coach
}
]
: [])
)
let rankingDevelopment
try {
const rankings = JSON.parse($('.graph').attr('data-fusionchart-config')!)
rankingDevelopment = rankings.dataSource.dataset[0].data.map((x: any) =>
parseNumber(x.value)
)
} catch {
rankingDevelopment = []
}
const country = {
name: $('.team-country .flag').attr('alt'),
code: $('.team-country .flag').attrThen(
'src',
(x) => x.split('/').pop()?.split('.')[0]!
)
}
const news = $('#newsBox a')
.toArray()
.map((el) => ({
name: el.contents().eq(1).text(),
link: el.attr('href')
}))
return {
id,
name,
logo,
facebook,
twitter,
instagram,
country,
rank,
players,
rankingDevelopment,
news
}
}
function getPlayerType(text: string): TeamPlayerType | undefined {
if (text === 'STARTER') {
return TeamPlayerType.Starter
}
if (text === 'BENCHED') {
return TeamPlayerType.Benched
}
if (text === 'SUBSTITUTE') {
return TeamPlayerType.Substitute
}
}