-
-
Notifications
You must be signed in to change notification settings - Fork 110
/
getPlayer.ts
175 lines (154 loc) · 4.3 KB
/
getPlayer.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import { HLTVConfig } from '../config'
import { HLTVScraper } from '../scraper'
import { Country } from '../shared/Country'
import { Team } from '../shared/Team'
import { Event } from '../shared/Event'
import { fetchPage, generateRandomSuffix, getIdAt, parseNumber } from '../utils'
import { Article } from '../shared/Article'
export interface FullPlayerTeam extends Team {
startDate: number
leaveDate?: number
trophies: Event[]
}
export interface PlayerAchievement {
event: Event
place: string
}
export interface FullPlayer {
id: number
name?: string
ign: string
image?: string
age?: number
country: Country
team?: Team
twitter?: string
twitch?: string
facebook?: string
instagram?: string
statistics?: {
rating: number
killsPerRound: number
headshots: number
mapsPlayed: number
deathsPerRound: number
roundsContributed: number
}
teams: FullPlayerTeam[]
achievements: PlayerAchievement[]
news: Article[]
}
export const getPlayer =
(config: HLTVConfig) =>
async ({ id }: { id: number }): Promise<FullPlayer> => {
const $ = HLTVScraper(
await fetchPage(
`https://www.hltv.org/player/${id}/${generateRandomSuffix()}`,
config.loadPage
)
)
const nameText = $('.playerRealname').trimText()
const name = nameText === '-' ? undefined : nameText
const ign = $('.playerNickname').text()
const imageUrl =
$('.profile-img').attr('src') || $('.bodyshot-img').attr('src')
const image =
imageUrl.includes('bodyshot/unknown.png') ||
imageUrl.includes('static/player/player_silhouette.png')
? undefined
: imageUrl
const age = $('.playerAge .listRight').textThen((x) =>
parseNumber(x.split(' ')[0])
)
const twitter = $('.twitter').parent().attr('href')
const twitch = $('.twitch').parent().attr('href')
const facebook = $('.facebook').parent().attr('href')
const instagram = $('.instagram').parent().attr('href')
const country = {
name: $('.playerRealname .flag').attr('alt')!,
code: $('.playerRealname .flag').attrThen(
'src',
(x) => x.split('/').pop()?.split('.')[0]!
)
}
const hasTeam = $('.playerTeam .listRight').trimText() !== 'No team'
let team
if (hasTeam) {
team = {
name: $('.playerTeam a').trimText()!,
id: $('.playerTeam a').attrThen('href', getIdAt(2))
}
}
const getMapStat = (i: number) =>
Number(
$('.playerpage-container')
.find('.player-stat')
.eq(i)
.find('.statsVal')
.text()
.replace('%', '')
)
const statistics = $('.playerpage-container.empty-state').exists()
? undefined
: {
rating: getMapStat(0),
killsPerRound: getMapStat(1),
headshots: getMapStat(2),
mapsPlayed: getMapStat(3),
deathsPerRound: getMapStat(4),
roundsContributed: getMapStat(5)
}
const achievements = $('.achievement-table .team')
.toArray()
.map((el) => ({
place: el.find('.achievement').text(),
event: {
name: el.find('.tournament-name-cell a').text(),
id: el.find('.tournament-name-cell a').attrThen('href', getIdAt(2))
}
}))
const teams = $('.team-breakdown .team')
.toArray()
.map((el) => ({
id: el.find('.team-name-cell a').attrThen('href', getIdAt(2)),
name: el.find('.team-name').text(),
startDate: el
.find('.time-period-cell [data-unix]')
.first()
.numFromAttr('data-unix')!,
leaveDate: el
.find('.time-period-cell [data-unix]')
.eq(1)
.numFromAttr('data-unix'),
trophies: el
.find('.trophy-row-trophy a')
.toArray()
.map((trophyEl) => ({
id: trophyEl.attrThen('href', getIdAt(2)),
name: trophyEl.find('img').attr('title')
}))
}))
const news = $('#newsBox a')
.toArray()
.map((el) => ({
name: el.contents().eq(1).text(),
link: el.attr('href')
}))
return {
id,
name,
ign,
image,
age,
twitter,
twitch,
facebook,
instagram,
country,
team,
statistics,
achievements,
teams,
news
}
}