-
Notifications
You must be signed in to change notification settings - Fork 0
/
ranking.go
278 lines (235 loc) · 7.56 KB
/
ranking.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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package wizlib
import (
"bytes"
"fmt"
"net/url"
"regexp"
"strings"
"github.com/PuerkitoBio/goquery"
)
// PlayerRanking represents the ranking information of a player.
type PlayerRanking struct {
Position string `json:"position"`
Name string `json:"name"`
Level string `json:"level"`
School string `json:"school"`
Wins string `json:"wins"`
Rating string `json:"rating"`
}
// Tournament represents the information of a tournament.
type Tournament struct {
Name string `json:"name"`
Levels string `json:"levels"`
StartTime string `json:"start_time"`
Duration string `json:"duration"`
}
// DocumentFetcher retrieves HTML documents from a source.
type DocumentFetcher interface {
Fetch(url string) (*goquery.Document, error)
}
// HTTPDocumentFetcher provides methods for fetching HTML documents.
type HTTPDocumentFetcher struct {
Client *APIClient
}
// NewHTTPDocumentFetcher creates a new instance of HTTPDocumentFetcher.
func NewHTTPDocumentFetcher() *HTTPDocumentFetcher {
return &HTTPDocumentFetcher{
Client: NewAPIClient(),
}
}
// Fetch fetches the HTML document from the specified URL.
func (f *HTTPDocumentFetcher) Fetch(url string) (*goquery.Document, error) {
body, err := f.Client.Get(url)
if err != nil {
return nil, err
}
doc, err := goquery.NewDocumentFromReader(bytes.NewReader(body))
if err != nil {
return nil, err
}
return doc, nil
}
// Repository retrieves data from a source.
type Repository struct {
DocumentFetcher DocumentFetcher
URL string
}
// NewRepository creates a new instance of Repository.
func NewRepository(fetcher DocumentFetcher, url string) *Repository {
return &Repository{
DocumentFetcher: fetcher,
URL: url,
}
}
// FetchRankings retrieves the player rankings.
func (r *Repository) FetchRankings() ([]PlayerRanking, error) {
doc, err := r.DocumentFetcher.Fetch(r.URL)
if err != nil {
return nil, err
}
return parseRankings(doc), nil
}
// FetchTournaments retrieves the tournaments.
func (r *Repository) FetchTournaments() ([]Tournament, error) {
doc, err := r.DocumentFetcher.Fetch(r.URL)
if err != nil {
return nil, err
}
return parseTournaments(doc), nil
}
// parseRankings parses the player rankings from a goquery.Document.
func parseRankings(doc *goquery.Document) []PlayerRanking {
rankings := make([]PlayerRanking, 0)
doc.Find(".schedule table tbody tr").Each(func(i int, s *goquery.Selection) {
ranking := PlayerRanking{}
ranking.parseFromSelection(s)
if ranking.Name != "" {
rankings = append(rankings, ranking)
}
})
// Remove empty values
rankings = rankings[:len(rankings)-1]
return rankings
}
// parseTournaments parses the tournaments from a goquery.Document.
func parseTournaments(doc *goquery.Document) []Tournament {
tournaments := make([]Tournament, 0)
doc.Find(".schedule table tbody tr").Each(func(i int, s *goquery.Selection) {
tournament := Tournament{}
tournament.parseFromSelection(s)
tournaments = append(tournaments, tournament)
})
// Remove empty values
tournaments = tournaments[:len(tournaments)-1]
return tournaments
}
// parseFromSelection extracts the ranking information from a goquery.Selection.
func (r *PlayerRanking) parseFromSelection(s *goquery.Selection) {
r.Position = strings.TrimSpace(s.Find("td:nth-child(1)").Text())
r.Name = strings.TrimSpace(s.Find("td:nth-child(2)").Text())
r.Level = strings.TrimSpace(s.Find("td:nth-child(3)").Text())
r.School = strings.TrimSpace(s.Find("td:nth-child(4) img").AttrOr("class", ""))
r.Wins = strings.TrimSpace(s.Find("td:nth-child(5)").Text())
r.Rating = strings.TrimSpace(s.Find("td:nth-child(6)").Text())
}
// parseFromSelection extracts the tournament information from a goquery.Selection.
func (t *Tournament) parseFromSelection(s *goquery.Selection) {
t.Name = parseName(strings.TrimSpace(s.Find("td:nth-child(1)").Text()))
t.Levels = strings.TrimSpace(s.Find("td:nth-child(2)").Text())
t.StartTime = strings.TrimSpace(s.Find("td:nth-child(3)").Text())
t.Duration = strings.TrimSpace(s.Find("td:nth-child(4)").Text())
if timestamp, err := extractTimestamp(t.StartTime); err == nil {
t.StartTime = timestamp
}
}
var nameMap = map[string]string{
"LightningName": "Quick Match Tournament",
"FireAndIceName": "Fire & Ice Perk Tournament",
"OldSchoolName": "Classic Tournament",
"AlternatingTurns_PipsAtOnceName": "Turn-Based Tournament",
"MythAndStormName": "Myth & Storm Perk Tournament",
"LifeAndDeathName": "Life & Death Perk Tournament",
"BalanceName": "Balance Perk Tournament",
}
// parseName parses the name of a tournament.
func parseName(name string) string {
if val, ok := nameMap[name]; ok {
return val
}
return name
}
// extractTimestamp extracts the timestamp from the given line using a regular expression.
func extractTimestamp(line string) (string, error) {
re := regexp.MustCompile(`new Date\((\d+)\)`)
matches := re.FindStringSubmatch(line)
if len(matches) < 2 {
return "", fmt.Errorf("no timestamp found")
}
return matches[1], nil
}
// Presenter is responsible for presenting data.
type Presenter interface {
PresentRankings(rankings []PlayerRanking)
PresentTournaments(tournaments []Tournament)
}
// ConsolePresenter presents player rankings and tournaments on the console.
type ConsolePresenter struct{}
// PresentRankings prints the player rankings.
func (p *ConsolePresenter) PresentRankings(rankings []PlayerRanking) {
for _, r := range rankings {
fmt.Printf("Position: %s\n", r.Position)
fmt.Printf("Name: %s\n", r.Name)
fmt.Printf("Level: %s\n", r.Level)
fmt.Printf("Wins: %s\n", r.Wins)
fmt.Printf("School: %s\n", r.School)
fmt.Printf("Rating: %s\n", r.Rating)
fmt.Println("-----------------------------")
}
}
// PresentTournaments prints the tournament information.
func (p *ConsolePresenter) PresentTournaments(tournaments []Tournament) {
for _, t := range tournaments {
fmt.Printf("Tournament Name: %s\n", t.Name)
fmt.Printf("Levels: %s\n", t.Levels)
fmt.Printf("Start Time: %s\n", t.StartTime)
fmt.Printf("Duration: %s\n", t.Duration)
fmt.Println("-----------------------------")
}
}
// URLParams contains the parameters for a URL.
type URLParams struct {
Age string
Levels string
Filter string
}
// URLParser is responsible for parsing URL parameters.
type URLParser struct {
rawURL string
}
// NewURLParser creates a new instance of URLParser.
func NewURLParser(rawURL string) *URLParser {
return &URLParser{
rawURL: rawURL,
}
}
// ParseURL parses the URL and extracts the relevant parameters.
func (p *URLParser) ParseURL() (*URLParams, error) {
u, err := url.Parse(p.rawURL)
if err != nil {
return nil, err
}
return &URLParams{
Age: u.Query().Get("age"),
Levels: u.Query().Get("levels"),
Filter: u.Query().Get("filter"),
}, nil
}
// URLGenerator is responsible for generating URLs with parameters.
type URLGenerator struct {
baseURL string
params *URLParams
}
// NewURLGenerator creates a new instance of URLGenerator.
func NewURLGenerator(baseURL string) *URLGenerator {
return &URLGenerator{
baseURL: baseURL,
}
}
// WithParams sets the parameters for the URLGenerator.
func (g *URLGenerator) WithParams(params *URLParams) *URLGenerator {
g.params = params
return g
}
// GenerateURL generates a URL with the provided parameters.
func (g *URLGenerator) GenerateURL() (string, error) {
u, err := url.Parse(g.baseURL)
if err != nil {
return "", err
}
q := u.Query()
q.Set("age", g.params.Age)
q.Set("levels", g.params.Levels)
q.Set("filter", g.params.Filter)
u.RawQuery = q.Encode()
return u.String(), nil
}