-
-
Notifications
You must be signed in to change notification settings - Fork 107
/
getMatches.ts
109 lines (94 loc) · 2.83 KB
/
getMatches.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
import { stringify } from 'querystring'
import { HLTVConfig } from '../config'
import { HLTVScraper } from '../scraper'
import { Team } from '../shared/Team'
import { Event } from '../shared/Event'
import { fetchPage, getIdAt } from '../utils'
export enum MatchEventType {
All = 'All',
LAN = 'Lan',
Online = 'Online'
}
export enum MatchFilter {
LanOnly = 'lan_only',
TopTier = 'top_tier'
}
export interface GetMatchesArguments {
eventIds?: number[]
eventType?: MatchEventType
filter?: MatchFilter
teamIds?: number[]
}
export interface MatchPreview {
id: number
team1?: Team
team2?: Team
date?: number
format?: string
event?: Event
title?: string
live: boolean
stars: number
}
export const getMatches =
(config: HLTVConfig) =>
async ({
eventIds,
eventType,
filter,
teamIds
}: GetMatchesArguments = {}): Promise<MatchPreview[]> => {
const query = stringify({
...(eventIds ? { event: eventIds } : {}),
...(eventType ? { eventType } : {}),
...(filter ? { predefinedFilter: filter } : {}),
...(teamIds ? { team: teamIds } : {})
})
const $ = HLTVScraper(
await fetchPage(`https://www.hltv.org/matches?${query}`, config.loadPage)
)
const events = $('.event-filter-popup a')
.toArray()
.map((el) => ({
id: el.attrThen('href', (x) => Number(x.split('=').pop())),
name: el.find('.event-name').text()
}))
.concat(
$('.events-container a')
.toArray()
.map((el) => ({
id: el.attrThen('href', (x) => Number(x.split('=').pop())),
name: el.find('.featured-event-tooltip-content').text()
}))
)
return $('.liveMatch-container')
.toArray()
.concat($('.upcomingMatch').toArray())
.map((el) => {
const id = el.find('.a-reset').attrThen('href', getIdAt(2))!
const stars = 5 - el.find('.matchRating i.faded').length
const live = el.find('.matchTime.matchLive').text() === 'LIVE'
const title = el.find('.matchInfoEmpty').text() || undefined
const date = el.find('.matchTime').numFromAttr('data-unix')
let team1
let team2
if (!title) {
team1 = {
name:
el.find('.matchTeamName').first().text() ||
el.find('.team1 .team').text(),
id: el.numFromAttr('team1')
}
team2 = {
name:
el.find('.matchTeamName').eq(1).text() ||
el.find('.team2 .team').text(),
id: el.numFromAttr('team2')
}
}
const format = el.find('.matchMeta').text()
const eventName = el.find('.matchEventLogo').attr('title')
const event = events.find((x) => x.name === eventName)
return { id, date, stars, title, team1, team2, format, event, live }
})
}