-
Notifications
You must be signed in to change notification settings - Fork 17
/
sports.go
139 lines (119 loc) · 4.41 KB
/
sports.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
package api
import (
"encoding/xml"
"time"
"github.com/minus5/go-uof-sdk"
)
const (
pathMarkets = "/v1/descriptions/{{.Lang}}/markets.xml?include_mappings={{.IncludeMappings}}"
pathMarketVariant = "/v1/descriptions/{{.Lang}}/markets/{{.MarketID}}/variants/{{.Variant}}?include_mappings={{.IncludeMappings}}"
pathFixture = "/v1/sports/{{.Lang}}/sport_events/{{.EventURN}}/fixture.xml"
pathSummary = "/v1/sports/{{.Lang}}/sport_events/{{.EventURN}}/summary.xml"
pathPlayer = "/v1/sports/{{.Lang}}/players/sr:player:{{.PlayerID}}/profile.xml"
pathCompetitor = "/v1/sports/{{.Lang}}/competitors/sr:competitor:{{.PlayerID}}/profile.xml"
events = "/v1/sports/{{.Lang}}/schedules/pre/schedule.xml?start={{.Start}}&limit={{.Limit}}"
liveEvents = "/v1/sports/{{.Lang}}/schedules/live/schedule.xml"
)
// Markets all currently available markets for a language
func (a *API) Markets(lang uof.Lang) (uof.MarketDescriptions, error) {
var mr marketsRsp
return mr.Markets, a.getAs(&mr, pathMarkets, ¶ms{Lang: lang})
}
func (a *API) MarketVariant(lang uof.Lang, marketID int, variant string) (uof.MarketDescriptions, error) {
var mr marketsRsp
return mr.Markets, a.getAs(&mr, pathMarketVariant, ¶ms{Lang: lang, MarketID: marketID, Variant: variant})
}
// Fixture lists the fixture for a specified sport event
func (a *API) Fixture(lang uof.Lang, eventURN uof.URN) (*uof.Fixture, error) {
var fr fixtureRsp
return &fr.Fixture, a.getAs(&fr, pathFixture, ¶ms{Lang: lang, EventURN: eventURN})
}
// Fixture with extra information
func (a *API) Summary(lang uof.Lang, eventURN uof.URN) (*uof.Summary, error) {
var s uof.Summary
return &s, a.getAs(&s, pathSummary, ¶ms{Lang: lang, EventURN: eventURN})
}
func (a *API) Tournament(lang uof.Lang, eventURN uof.URN) (*uof.FixtureTournament, error) {
var ft uof.FixtureTournament
return &ft, a.getAs(&ft, pathFixture, ¶ms{Lang: lang, EventURN: eventURN})
}
func (a *API) Player(lang uof.Lang, playerID int) (*uof.Player, error) {
var pr playerRsp
return &pr.Player, a.getAs(&pr, pathPlayer, ¶ms{Lang: lang, PlayerID: playerID})
}
func (a *API) Competitor(lang uof.Lang, playerID int) (*uof.CompetitorPlayer, error) {
var cr competitorRsp
return &cr.Competitor, a.getAs(&cr, pathCompetitor, ¶ms{Lang: lang, PlayerID: playerID})
}
type marketsRsp struct {
Markets uof.MarketDescriptions `xml:"market,omitempty" json:"markets,omitempty"`
// unused
// ResponseCode string `xml:"response_code,attr,omitempty" json:"responseCode,omitempty"`
// Location string `xml:"location,attr,omitempty" json:"location,omitempty"`
}
type playerRsp struct {
Player uof.Player `xml:"player" json:"player"`
GeneratedAt time.Time `xml:"generated_at,attr,omitempty" json:"generatedAt,omitempty"`
}
type competitorRsp struct {
Competitor uof.CompetitorPlayer `xml:"competitor" json:"competitor"`
GeneratedAt time.Time `xml:"generated_at,attr,omitempty" json:"generatedAt,omitempty"`
}
type fixtureRsp struct {
Fixture uof.Fixture `xml:"fixture" json:"fixture"`
GeneratedAt time.Time `xml:"generated_at,attr,omitempty" json:"generatedAt,omitempty"`
}
type scheduleRsp struct {
Fixtures []uof.Fixture `xml:"sport_event,omitempty" json:"sportEvent,omitempty"`
GeneratedAt time.Time `xml:"generated_at,attr,omitempty" json:"generatedAt,omitempty"`
}
// Fixtures gets all the fixtures with schedule before to
func (a *API) Fixtures(lang uof.Lang, to time.Time) (<-chan uof.Fixture, <-chan error) {
errc := make(chan error, 1)
out := make(chan uof.Fixture)
go func() {
defer close(out)
defer close(errc)
done := false
parse := func(buf []byte) error {
var sr scheduleRsp
if err := xml.Unmarshal(buf, &sr); err != nil {
return uof.Notice("unmarshal", err)
}
for _, f := range sr.Fixtures {
out <- f
if f.Scheduled.After(to) {
done = true
}
}
return nil
}
// first live events
buf, err := a.get(liveEvents, ¶ms{Lang: lang})
if err != nil {
errc <- err
return
}
if err := parse(buf); err != nil {
errc <- err
return
}
// than all events which has scheduled before to
limit := 1000
for start := 0; true; start += limit {
buf, err := a.get(events, ¶ms{Lang: lang, Start: start, Limit: limit})
if err != nil {
errc <- err
return
}
if err := parse(buf); err != nil {
errc <- err
return
}
if done {
return
}
}
}()
return out, errc
}