-
-
Notifications
You must be signed in to change notification settings - Fork 110
/
getEvent.ts
214 lines (188 loc) · 5.42 KB
/
getEvent.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
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,
notNull,
parseNumber
} from '../utils'
import { fromMapName, GameMap } from '../shared/GameMap'
import { Article } from '../shared/Article'
export interface FullEventTeam extends Team {
reasonForParticipation?: string
rankDuringEvent?: number
}
export interface FullEventPrizeDistribution {
place: string
prize?: string
otherPrize?: string
qualifiesFor?: Event
team?: Team
}
export interface FullEventFormat {
type: string
description: string
}
export interface FullEventHighlight {
link: string
name: string
views: number
thumbnail: string
team1: Team
team2: Team
}
export interface FullEvent {
id: number
name: string
logo: string
dateStart?: number
dateEnd?: number
prizePool: string
location: Country
numberOfTeams?: number
teams: FullEventTeam[]
prizeDistribution: FullEventPrizeDistribution[]
relatedEvents: Event[]
formats: FullEventFormat[]
mapPool: GameMap[]
highlights: FullEventHighlight[]
news: Article[]
}
export const getEvent =
(config: HLTVConfig) =>
async ({ id }: { id: number }): Promise<FullEvent> => {
const $ = HLTVScraper(
await fetchPage(
`https://www.hltv.org/events/${id}/${generateRandomSuffix()}`,
config.loadPage
)
)
const name = $('.event-hub-title').text()
const logo = $('.sidebar-first-level').find('.event-logo').attr('src')
const prizePool = $('td.prizepool').text()
const dateStart = $('td.eventdate span[data-unix]')
.first()
.numFromAttr('data-unix')
const dateEnd = $('td.eventdate span[data-unix]')
.last()
.numFromAttr('data-unix')
const location = {
name: $('.location span.text-ellipsis').text(),
code: $('img.flag').attr('src').split('/').pop()!.split('.')[0]
}
const relatedEvents = $('.related-event')
.toArray()
.map((el) => ({
name: el.find('.event-name').text(),
id: el.find('a').attrThen('href', getIdAt(2))
}))
const prizeDistribution = $('.placements .placement')
.toArray()
.map((el) => {
const otherPrize =
el.find('.spot-prize').text() ||
el.find('.prize').first().next().text() ||
undefined
const qualifiesFor = !!otherPrize
? relatedEvents.find((event) => event.name === otherPrize)
: undefined
return {
place: el.children().eq(1).text(),
prize: el.find('.prize').first().text() || undefined,
qualifiesFor,
otherPrize: !qualifiesFor ? otherPrize : undefined,
team: el.find('.team').children().exists()
? {
name: el.find('.team a').text(),
id: el.find('.team a').attrThen('href', getIdAt(2))
}
: undefined
}
})
const numberOfTeams = $('td.teamsNumber').numFromText()!
const teams = $('.team-box')
.toArray()
.map((el) => {
if (!el.find('.team-name a').exists()) {
return null
}
return {
name: el.find('.logo').attr('title'),
id: el.find('.team-name a').attrThen('href', getIdAt(2)),
reasonForParticipation: el.find('.sub-text').trimText(),
rankDuringEvent: parseNumber(
el.find('.event-world-rank').text().replace('#', '')
)
}
})
.filter(notNull)
const formats = $('.formats tr')
.toArray()
.map((el) => ({
type: el.find('.format-header').text(),
description: el.find('.format-data').text().split('\n').join(' ').trim()
}))
const mapPool = $('.map-pool-map-holder')
.toArray()
.map((el) => fromMapName(el.find('.map-pool-map-name').text()))
const highlights = $('.highlight-video')
.toArray()
.map((el) => {
const name = el.find('.video-discription-text').text()
const link = el.data('mp4-url')
const [thumbnailBase] = el.data('thumbnail').split('-preview-')
const thumbnail = `${thumbnailBase}-preview.jpg`
const team1Name = el
.find('.video-team')
.first()
.find('.video-team-img')
.first()
.attr('title')
const team1 = teams.find((x) => x.name === team1Name)
const team2Name = el
.find('.video-team')
.last()
.find('.video-team-img')
.first()
.attr('title')
const team2 = teams.find((x) => x.name === team2Name)
const views = Number(
el.find('.thumbnail-view-count').text().split(' ')[0]
)
return {
name,
link,
thumbnail,
team1: { id: team1?.id, name: team1?.name ?? team1Name },
team2: { id: team2?.id, name: team2?.name ?? team2Name },
views
}
})
const news = $('.news .item')
.toArray()
.map((el) => ({
name: el.find('.flag-align .text-ellipsis').text(),
link: el.find('a').attr('href')
}))
return {
id,
name,
logo,
dateStart,
dateEnd,
prizePool,
location,
numberOfTeams,
teams,
prizeDistribution,
relatedEvents,
formats,
mapPool,
highlights,
news
}
}