-
-
Notifications
You must be signed in to change notification settings - Fork 110
/
getEvents.ts
175 lines (150 loc) · 4.65 KB
/
getEvents.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 { stringify } from 'querystring'
import { HLTVConfig } from '../config'
import { HLTVScraper } from '../scraper'
import { Country } from '../shared/Country'
import { EventType } from '../shared/EventType'
import { fetchPage, getIdAt, parseNumber } from '../utils'
export interface EventPreview {
id: number
name: string
dateStart: number
dateEnd: number
numberOfTeams?: number
prizePool?: string
location?: Country
featured: boolean
}
export interface GetEventsArguments {
eventType?: EventType
prizePoolMin?: number
prizePoolMax?: number
attendingTeamIds?: number[]
attendingPlayerIds?: number[]
}
export const getEvents =
(config: HLTVConfig) =>
async (options: GetEventsArguments = {}): Promise<EventPreview[]> => {
const query = stringify({
...(options.eventType ? { eventType: options.eventType } : {}),
...(options.prizePoolMin ? { prizeMin: options.prizePoolMin } : {}),
...(options.prizePoolMax ? { prizeMax: options.prizePoolMax } : {}),
...(options.attendingTeamIds ? { team: options.attendingTeamIds } : {}),
...(options.attendingPlayerIds
? { player: options.attendingPlayerIds }
: {})
})
const $ = HLTVScraper(
await fetchPage(`https://www.hltv.org/events?${query}`, config.loadPage)
)
const featuredOngoingEvents = $(
'.tab-content[id="FEATURED"] a.ongoing-event'
)
.toArray()
.map((el) => el.attrThen('href', getIdAt(2)))
const ongoingEvents = $('.tab-content[id="ALL"] a.ongoing-event')
.toArray()
.map((el) => {
const id = el.attrThen('href', getIdAt(2))!
const name = el.find('.event-name-small .text-ellipsis').text()
const dateStart = el
.find('tr.eventDetails span[data-unix]')
.first()
.numFromAttr('data-unix')!
const dateEnd = el
.find('tr.eventDetails span[data-unix]')
.last()
.numFromAttr('data-unix')!
const featured = featuredOngoingEvents.includes(id)
return { id, name, dateStart, dateEnd, featured }
})
const bigUpcomingEvents = $('a.big-event')
.toArray()
.map((el) => {
const id = el.attrThen('href', getIdAt(2))!
const name = el.find('.big-event-name').text()
const dateStart = el
.find('.additional-info .col-date span[data-unix]')
.first()
.numFromAttr('data-unix')!
const dateEnd = el
.find('.additional-info .col-date span[data-unix]')
.last()
.numFromAttr('data-unix')!
const locationName = el.find('.big-event-location').text()
const location =
locationName !== 'TBA'
? {
name: locationName,
code: el
.find('.location-top-teams img.flag')
.attr('src')
.split('/')
.pop()!
.split('.')[0]
}
: undefined
const prizePool = el
.find('.additional-info tr')
.first()
.find('td')
.eq(1)
.text()
const numberOfTeams = parseNumber(
el.find('.additional-info tr').first().find('td').eq(2).text()
)
return {
id,
name,
dateStart,
dateEnd,
location,
prizePool,
numberOfTeams,
featured: true
}
})
const smallUpcomingEvents = $('a.small-event')
.toArray()
.map((el) => {
const id = el.attrThen('href', getIdAt(2))!
const name = el
.find('.table tr')
.first()
.find('td')
.first()
.find('.text-ellipsis')
.text()
const dateStart = el
.find('td span[data-unix]')
.first()
.numFromAttr('data-unix')!
const dateEnd = el
.find('td span[data-unix]')
.last()
.numFromAttr('data-unix')!
const location = {
name: el.find('.smallCountry .col-desc').text().replace(' | ', ''),
code: el
.find('.smallCountry img.flag')
.attr('src')
.split('/')
.pop()!
.split('.')[0]
}
const prizePool = el.find('.prizePoolEllipsis').text()
const numberOfTeams = parseNumber(
el.find('.prizePoolEllipsis').prev().text()
)
return {
id,
name,
dateStart,
dateEnd,
location,
prizePool,
numberOfTeams,
featured: false
}
})
return ongoingEvents.concat(bigUpcomingEvents).concat(smallUpcomingEvents)
}