-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_matches.go
153 lines (130 loc) · 3.78 KB
/
get_matches.go
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
package operations
import (
"net/url"
"strconv"
"strings"
"time"
"github.com/go-rod/rod"
"github.com/richecr/hltvgo/lib/api"
"github.com/richecr/hltvgo/lib/entity"
)
const (
matchesURL = "https://www.hltv.org/matches"
)
type Filters struct {
Event string
EventType string
LanOnly bool
TopTier bool
Teams []string
}
func GetMatches(filters Filters) ([]entity.Match, error) {
query := AddFilters(filters)
page := api.GetPage(matchesURL + query)
defer page.MustClose()
events := GetEvents(page)
element := page.MustElement("div.upcomingMatchesAll")
divMatches := append(
element.MustElements("div.upcomingMatch"),
page.MustElements("div.liveMatch-container")...,
)
matches := make(chan []entity.Match, len(divMatches))
defer close(matches)
go GetMatch(divMatches[:len(divMatches)/2], matches, events)
go GetMatch(divMatches[len(divMatches)/2:], matches, events)
return append(<-matches, <-matches...), nil
}
func AddFilters(filters Filters) string {
values := url.Values{}
if filters.Event != "" {
values.Add("event", filters.Event)
}
if filters.EventType != "" {
values.Add("eventType", filters.EventType)
}
if filters.LanOnly {
values.Add("predefinedFilter", "lan_only")
} else if filters.TopTier {
values.Add("predefinedFilter", "top_tier")
}
for _, id := range filters.Teams {
values.Add("team", id)
}
query := values.Encode()
if query != "" {
return "?" + query
}
return ""
}
func GetMatch(divMatches rod.Elements, matches chan []entity.Match, events []entity.Event) {
var partial []entity.Match
for _, row := range divMatches {
main := row.MustElement("a.match.a-reset")
matchInfoEmpty, _ := main.Element(".matchInfoEmpty")
if matchInfoEmpty == nil {
tags := strings.Split(row.MustElement("a").MustText(), "\n")
var team1_name, team2_name, event_name string = tags[2], tags[3], tags[4]
live := false
var date time.Time
if tags[0] == "LIVE" {
team2_name = tags[4]
event_name = tags[6]
live = true
} else {
date = ConvertStringUnixToDate(
*row.MustElement(".matchTime").MustAttribute("data-unix"),
)
}
star := 5 - len(row.MustElement(".matchRating").MustElements(".faded"))
format := row.MustElement(".matchMeta").MustText()
team1 := GetTeam(row, team1_name, 1)
team2 := GetTeam(row, team2_name, 2)
event := FindEventByName(event_name, events)
id := strings.Split(*main.MustAttribute("href"), "/")[2]
match := entity.NewMatch(id, format, star, event, date, live, *team1, *team2)
partial = append(partial, *match)
}
}
matches <- partial
}
func GetEvents(page *rod.Page) []entity.Event {
var events []entity.Event
div := page.MustElement("div.filter-custom-content")
tagsEvents := append(
div.MustElements("a.filter-button-link.event-row"),
div.MustElements("a.filter-button-link")...,
)
for _, tag := range tagsEvents {
href_text := strings.Split(*tag.MustAttribute("href"), "=")
id := href_text[len(href_text)-1]
var el_name *rod.Element
el_name, err := tag.Element("div.event-name")
if err != nil {
el_name = tag.MustElement("div.featured-event-tooltip-content")
}
events = append(events, *entity.NewEvent(id, el_name.MustText()))
}
return events
}
func GetTeam(row *rod.Element, name string, numberTeam int8) *entity.Team {
attribute := "team" + strconv.Itoa(int(numberTeam))
if id := row.MustAttribute(attribute); id != nil {
return entity.NewTeam(*id, name)
}
return entity.NewTeam("", name)
}
func FindEventByName(name string, events []entity.Event) entity.Event {
for _, event := range events {
if event.Name == name {
return event
}
}
return *entity.NewEvent("0", "not found")
}
func ConvertStringUnixToDate(dateUnix string) time.Time {
i, err := strconv.ParseInt(dateUnix[:10], 10, 64)
if err != nil {
panic(err)
}
return time.Unix(i, 0)
}