-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
153 lines (125 loc) · 3.24 KB
/
main.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 main
import (
"fmt"
"net/http"
"strconv"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
"github.com/rs/zerolog/log"
)
var (
URL = "http://stats.sportsadmin.dk/schedule.aspx?tournamentID=1783"
timeformat = "02-01-2006 15:04"
)
type Week struct {
Round int
Matches []MatchRow
}
type MatchRow struct {
Date time.Time
HomeTeam Team
AwayTeam Team
Result string
}
type Team struct {
Name string
Logo string
Winner bool
}
func main() {
schedule := getFullSchedule()
rounds := weekSplitter(schedule)
for _, v := range rounds {
v.checkWinner()
}
fmt.Println(rounds[1].Matches)
}
func getFullSchedule() []MatchRow {
var headings, row []string
var rows [][]string
var matches []MatchRow
res, err := http.Get(URL)
if err != nil {
log.Error().Err(err).Msg("could not contact sportsadmin")
return nil
}
defer res.Body.Close()
doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
log.Error().Err(err).Msg("could not get document from response")
return nil
}
doc.Find("table").Each(func(index int, tablehtml *goquery.Selection) {
tablehtml.Find("tr").Each(func(indextr int, rowhtml *goquery.Selection) {
rowhtml.Find("th").Each(func(indexth int, tableheading *goquery.Selection) {
headings = append(headings, tableheading.Text())
})
rowhtml.Find("td").Each(func(indexth int, tablecell *goquery.Selection) {
row = append(row, tablecell.Text())
})
rows = append(rows, row)
row = nil
})
})
for i := 1; i < len(rows); i++ {
date, err := time.Parse(timeformat, rows[i][0]+" "+rows[i][1])
if err != nil {
log.Error().Err(err).Msg("could not parse time")
continue
}
hometeam := Team{rows[i][3], "", false}
awayteam := Team{rows[i][4], "", false}
result := rows[i][5]
match := MatchRow{
Date: date,
HomeTeam: hometeam,
AwayTeam: awayteam,
Result: result,
}
matches = append(matches, match)
}
return matches
}
func (w *Week) checkWinner() {
for i, r := range w.Matches {
if r.Result == "" {
return
}
resultvalues := strings.Split(r.Result, " - ")
homescore, err := strconv.Atoi(resultvalues[0])
if err != nil {
log.Error().Err(err).Msg("could not convert hometeam score to integer")
}
awayscore, err := strconv.Atoi(resultvalues[1])
if err != nil {
log.Error().Err(err).Msg("could not convert awayteam score to integers")
}
if homescore > awayscore {
w.Matches[i].HomeTeam.Winner = true
} else if homescore < awayscore {
w.Matches[i].AwayTeam.Winner = true
}
}
}
func weekSplitter(matches []MatchRow) []Week {
var roundmatches []MatchRow
var weeks []Week
starttime := matches[0].Date.Add(-time.Duration(matches[0].Date.Hour()) * time.Hour)
endtime := starttime.Add(8 * 24 * time.Hour)
var round = 0
for _, v := range matches {
if v.Date.After(starttime) && v.Date.Before(endtime) || v.Date.Day() == endtime.Day() || v.Date.Day() == starttime.Day() {
roundmatches = append(roundmatches, v)
} else {
weeks = append(weeks, Week{round, roundmatches})
roundmatches = nil
roundmatches = append(roundmatches, v)
round += 1
starttime = starttime.Add(7 * 24 * time.Hour)
endtime = endtime.Add(7 * 24 * time.Hour)
}
}
weeks = append(weeks, Week{round, roundmatches})
return weeks
}