forked from SoMuchForSubtlety/f1viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
290 lines (252 loc) · 7.6 KB
/
api.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
279
280
281
282
283
284
285
286
287
288
289
290
package main
import (
"path"
"sort"
"strings"
"sync"
"time"
"github.com/SoMuchForSubtlety/golark"
)
var endpoint = "https://f1tv.formula1.com/api/"
const (
liveSlug = "grand-prix-weekend-live"
)
type episode struct {
Title string `json:"title"`
Subtitle string `json:"subtitle"`
UID string `json:"uid"`
DataSourceID string `json:"data_source_id"`
Items []string `json:"items"`
}
type vodTypes struct {
Objects []struct {
Name string `json:"name"`
ContentUrls []string `json:"content_urls"`
UID string `json:"uid"`
} `json:"objects"`
}
type seasonStruct struct {
Name string `json:"name"`
HasContent bool `json:"has_content"`
Year int `json:"year"`
EventoccurrenceUrls []string `json:"eventoccurrence_urls"`
UID string `json:"uid"`
}
type seasons struct {
Seasons []seasonStruct `json:"objects"`
}
type eventStruct struct {
UID string `json:"uid"`
Name string `json:"name"`
OfficialName string `json:"official_name"`
SessionoccurrenceUrls []string `json:"sessionoccurrence_urls"`
StartDate string `json:"start_date"`
EndDate string `json:"end_date"`
}
type sessionStruct struct {
UID string `json:"uid"`
SessionName string `json:"session_name"`
Name string `json:"name"`
Status string `json:"status"`
ContentUrls []string `json:"content_urls"`
StartTime time.Time `json:"start_time"`
EndTime time.Time `json:"end_time"`
}
type channel struct {
UID string `json:"uid"`
Self string `json:"self"`
Name string `json:"name"`
}
func (c channel) PrettyName() string {
switch c.Name {
case "WIF":
return "Main Feed"
case "pit lane":
return "Pit Lane"
case "driver":
return "Driver Tracker"
case "data":
return "Data Channel"
default:
return c.Name
}
}
type collectionItem struct {
Archived bool `json:"archived"`
UID string `json:"uid"`
Language string `json:"language"`
ContentType string `json:"content_type"`
ContentURL string `json:"content_url"`
DisplayType string `json:"display_type,omitempty"`
SetURL string `json:"set_url"`
}
type collection struct {
UID string `json:"uid"`
Title string `json:"title"`
UniqueItems bool `json:"unique_items"`
Items []collectionItem `json:"items"`
Summary string `json:"summary"`
}
type collectionList struct {
Objects []collection `json:"objects"`
}
func getLiveWeekendEvent() (eventStruct, bool, error) {
type container struct {
Objects []collection `json:"objects"`
}
var liveSet container
err := golark.NewRequest(endpoint, "sets", "").
AddField(golark.NewField("items")).
WithFilter("slug", golark.NewFilter(golark.Equals, liveSlug)).
Execute(&liveSet)
if err != nil {
return eventStruct{}, false, err
}
if len(liveSet.Objects) == 0 || len(liveSet.Objects[0].Items) == 0 {
return eventStruct{}, false, nil
}
event, err := getEvent(liveSet.Objects[0].Items[0].ContentURL)
return event, true, err
}
func getCollectionList() (collList collectionList, err error) {
err = golark.NewRequest(endpoint, "sets", "").
AddField(golark.NewField("title")).
AddField(golark.NewField("uid")).
WithFilter("set_type_slug", golark.NewFilter(golark.Equals, "video")).
Execute(&collList)
return
}
func getCollection(collID string) (coll collection, err error) {
err = golark.NewRequest(endpoint, "sets", collID).
AddField(golark.NewField("items")).
Execute(&coll)
return
}
func getVodTypes() (types vodTypes, err error) {
err = golark.NewRequest(endpoint, "vod-type-tag", "").
AddField(golark.NewField("name")).
AddField(golark.NewField("content_urls")).
Execute(&types)
return
}
func getSeasons() (s seasons, err error) {
year := golark.NewField("year").WithFilter(golark.NewFilter(golark.GreaterThan, "2017"))
err = golark.NewRequest(endpoint, "race-season", "").
AddField(year).
AddField(golark.NewField("name")).
AddField(golark.NewField("has_content")).
AddField(golark.NewField("eventoccurrence_urls")).
OrderBy(year, golark.Ascending).
Execute(&s)
return
}
func getEvent(eventID string) (event eventStruct, err error) {
// TODO: use proper ID
err = golark.NewRequest(endpoint, "event-occurrence", pathToUID(eventID)).
AddField(golark.NewField("name")).
AddField(golark.NewField("sessionoccurrence_urls")).
Execute(&event)
return
}
func getSession(sessionID string) (session sessionStruct, err error) {
err = golark.NewRequest(endpoint, "session-occurrence", pathToUID(sessionID)).
AddField(golark.NewField("name")).
AddField(golark.NewField("status")).
AddField(golark.NewField("uid")).
AddField(golark.NewField("session_name")).
Execute(&session)
return
}
func getSessions(sessionIDs []string) ([]sessionStruct, error) {
type container struct {
Objects []sessionStruct `json:"objects"`
}
var response container
for i, id := range sessionIDs {
sessionIDs[i] = pathToUID(id)
}
err := golark.NewRequest(endpoint, "session-occurrence", "").
AddField(golark.NewField("name")).
AddField(golark.NewField("status")).
AddField(golark.NewField("content_urls")).
AddField(golark.NewField("uid").
WithFilter(golark.NewFilter(golark.Equals, strings.Join(sessionIDs, ",")))).
Execute(&response)
return response.Objects, err
}
func getSessionStreams(sessionID string) ([]channel, error) {
type container struct {
Channels []channel `json:"channel_urls"`
}
var channels container
err := golark.NewRequest(endpoint, "session-occurrence", sessionID).
AddField(golark.NewField("channel_urls").
WithSubField(golark.NewField("self")).
WithSubField(golark.NewField("name")).
WithSubField(golark.NewField("uid"))).
Execute(&channels)
return channels.Channels, err
}
func (s *viewerSession) loadEpisodes(episodeIDs []string) ([]episode, error) {
type container struct {
Objects []episode `json:"objects"`
}
const batchSize = 5
for i, id := range episodeIDs {
episodeIDs[i] = pathToUID(id)
}
episodes := make([]episode, len(episodeIDs))
var wg sync.WaitGroup
wg.Add(len(episodes) / batchSize)
if len(episodeIDs)%batchSize > 0 {
wg.Add(1)
}
for i := 0; i < len(episodeIDs); i += batchSize {
go func(rangeStart int) {
rangeEnd := rangeStart + batchSize
if rangeEnd > len(episodeIDs) {
rangeEnd = len(episodeIDs)
}
query := strings.Join(episodeIDs[rangeStart:rangeEnd], ",")
var response container
// TODO: properly handle error
err := golark.NewRequest(endpoint, "episodes", "").
AddField(golark.NewField("title")).
AddField(golark.NewField("subtitle")).
AddField(golark.NewField("uid").
WithFilter(golark.NewFilter(golark.Equals, query))).
AddField(golark.NewField("data_source_id")).
AddField(golark.NewField("items")).
Execute(&response)
if err != nil {
s.logError(err)
return
}
copy(episodes[rangeStart:], response.Objects)
wg.Done()
}(i)
}
wg.Wait()
return episodes, nil
}
func sortEpisodes(episodes []episode) []episode {
sort.Slice(episodes, func(i, j int) bool {
if len(episodes[i].DataSourceID) >= 4 && len(episodes[j].DataSourceID) >= 4 {
year1, race1, err := getYearAndRace(episodes[i].DataSourceID)
year2, race2, err2 := getYearAndRace(episodes[j].DataSourceID)
if err == nil && err2 == nil {
// sort chronologically by year and race number
if year1 != year2 {
return year1 < year2
} else if race1 != race2 {
return race1 < race2
}
}
}
return episodes[i].Title < episodes[j].Title
})
return episodes
}
func pathToUID(p string) (uid string) {
return path.Base(p)
}