forked from nathanjjohnson/GoSchedulesDirect
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlineup.go
289 lines (235 loc) · 8.62 KB
/
lineup.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
package schedulesdirect
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/url"
"time"
)
// A ChangeLineupResponse stores the message after attempting
// to add or delete a lineup.
type ChangeLineupResponse struct {
*BaseResponse
ChangesRemaining jsonInt `json:"changesRemaining,omitempty"`
}
// A LineupResponse stores the message after requesting
// to list subscribed lineups.
type LineupResponse struct {
*BaseResponse
Lineups []Lineup `json:"lineups,omitempty"`
}
// A Headend stores the message information for a headend.
type Headend struct {
Headend string `json:"headend,omitempty"`
Transport string `json:"transport,omitempty"`
Location string `json:"location,omitempty"`
Lineups []Lineup `json:"lineups,omitempty"`
}
// A Lineup stores the message lineup information.
type Lineup struct {
Lineup string `json:"lineup,omitempty"`
Name string `json:"name,omitempty"`
ID string `json:"ID,omitempty"`
Modified *time.Time `json:"modified,omitempty"`
URI string `json:"uri,omitempty"`
IsDeleted bool `json:"isDeleted,omitempty"`
}
// A BroadcasterInfo stores the information about a broadcaster.
type BroadcasterInfo struct {
City string `json:"city,omitempty"`
State string `json:"state,omitempty"`
Postalcode string `json:"postalcode,omitempty"`
Country string `json:"country,omitempty"`
}
// A ChannelResponse stores the channel response for a lineup
type ChannelResponse struct {
*BaseResponse
Map []ChannelMap `json:"map,omitempty"`
Stations []Station `json:"stations,omitempty"`
Metadata *ChannelResponseMeta `json:"metadata,omitempty"`
}
// A ChannelResponseMeta stores the metadata field associated with a channel response
type ChannelResponseMeta struct {
Lineup string `json:"lineup,omitempty"`
Modified *time.Time `json:"modified,omitempty"`
Transport string `json:"transport,omitempty"`
Modulation string `json:"modulation,omitempty"`
}
// A Station stores a station in a lineup or schedule.
type Station struct {
Affiliate string `json:"affiliate,omitempty"`
Broadcaster *BroadcasterInfo `json:"broadcaster,omitempty"`
BroadcastLanguage []string `json:"broadcastLanguage,omitempty"`
CallSign string `json:"callsign,omitempty"`
DescriptionLanguage []string `json:"descriptionLanguage,omitempty"`
IsCommercialFree bool `json:"isCommercialFree,omitempty"`
Logo *StationLogo `json:"logo,omitempty"`
Logos []StationLogo `json:"stationLogo,omitempty"`
Name string `json:"name,omitempty"`
StationID string `json:"stationID,omitempty"`
IsRadioStation bool `json:"isRadioStation,omitempty"`
}
// A StationLogo stores the information to locate a station logo
type StationLogo struct {
URL string `json:"URL,omitempty"`
Height int `json:"height,omitempty"`
Width int `json:"width,omitempty"`
MD5 string `json:"md5,omitempty"`
Source string `json:"source,omitempty"`
}
// StationPreview is a slim version of Station containing the fields only visible during lineup preview.
type StationPreview struct {
Affiliate string `json:"affiliate,omitempty"`
CallSign string `json:"callsign,omitempty"`
Channel string `json:"channel,omitempty"`
Name string `json:"name,omitempty"`
}
// A ChannelMap stores the station id to channel mapping
type ChannelMap struct {
Channel string `json:"channel,omitempty"`
ChannelMajor int `json:"channelMajor,omitempty"`
ChannelMinor int `json:"channelMinor,omitempty"`
DeliverySystem string `json:"deliverySystem,omitempty"`
FED string `json:"fec,omitempty"`
FrequencyHertz int `json:"frequencyHz,omitempty"`
LogicalChannelNumber string `json:"logicalChannelNumber,omitempty"`
MatchType string `json:"matchType,omitempty"`
ModulationSystem string `json:"modulationSystem,omitempty"`
NetworkID int `json:"networkID,omitempty"`
Polarization string `json:"polarization,omitempty"`
ProviderCallSign string `json:"providerCallsign,omitempty"`
ServiceID int `json:"serviceID,omitempty"`
StationID string `json:"stationID,omitempty"`
SymbolRate int `json:"symbolrate,omitempty"`
TransportID int `json:"transportID,omitempty"`
VirtualChannel string `json:"virtualChannel,omitempty"`
}
// AddLineup adds the given lineup uri to the users SchedulesDirect account.
func (c *Client) AddLineup(lineupID string) (*ChangeLineupResponse, error) {
url := fmt.Sprint(c.BaseURL, APIVersion, "/lineups/", lineupID)
req, httpErr := http.NewRequest("PUT", url, nil)
if httpErr != nil {
return nil, httpErr
}
_, data, err := c.SendRequest(req, true)
if err != nil {
return nil, err
}
r := &ChangeLineupResponse{}
err = json.Unmarshal(data, &r)
return r, err
}
// DeleteLineup deletes the given lineup uri from the users SchedulesDirect account.
func (c *Client) DeleteLineup(lineupID string) (*ChangeLineupResponse, error) {
url := fmt.Sprint(c.BaseURL, APIVersion, "/lineups/", lineupID)
req, httpErr := http.NewRequest("DELETE", url, nil)
if httpErr != nil {
return nil, httpErr
}
_, data, err := c.SendRequest(req, true)
if err != nil {
return nil, err
}
r := &ChangeLineupResponse{}
err = json.Unmarshal(data, &r)
return r, err
}
// PreviewLineup returns a slice of StationPreview containing the channels available in the provided lineupID.
func (c *Client) PreviewLineup(lineupID string) ([]StationPreview, error) {
url := fmt.Sprint(c.BaseURL, APIVersion, "/lineups/preview/", lineupID)
req, httpErr := http.NewRequest("GET", url, nil)
if httpErr != nil {
return nil, httpErr
}
_, data, err := c.SendRequest(req, true)
if err != nil {
return nil, err
}
r := make([]StationPreview, 0)
err = json.Unmarshal(data, &r)
return r, err
}
// AutomapLineup accepts the "lineup.json" output as a byte slice from SiliconDust's HDHomerun devices.
// It then runs a comparison against ScheduleDirect's database and returns potential lineup matches.
func (c *Client) AutomapLineup(hdhrLineupJSON []byte) (map[string]int, error) {
url := fmt.Sprint(c.BaseURL, APIVersion, "/map/lineup")
req, httpErr := http.NewRequest("POST", url, bytes.NewBuffer(hdhrLineupJSON))
if httpErr != nil {
return nil, httpErr
}
_, data, err := c.SendRequest(req, true)
if err != nil {
return nil, err
}
matches := make(map[string]int)
err = json.Unmarshal(data, &matches)
return matches, err
}
// SubmitLineup should be called if AutomapLineup doesn't return candidates after you identify
// the lineup you were trying to find via automapping.
func (c *Client) SubmitLineup(hdhrLineupJSON []byte, lineupID string) error {
url := fmt.Sprint(c.BaseURL, APIVersion, "/map/lineup/", lineupID)
req, httpErr := http.NewRequest("POST", url, bytes.NewBuffer(hdhrLineupJSON))
if httpErr != nil {
return httpErr
}
_, _, err := c.SendRequest(req, true)
return err
}
// GetHeadends returns the map of headends for the given country and postal code.
func (c *Client) GetHeadends(countryCode, postalCode string) ([]Headend, error) {
params := url.Values{}
params.Add("country", countryCode)
params.Add("postalcode", postalCode)
uriPart := fmt.Sprint("/headends?", params.Encode())
url := fmt.Sprint(c.BaseURL, APIVersion, uriPart)
req, httpErr := http.NewRequest("GET", url, nil)
if httpErr != nil {
return nil, httpErr
}
_, data, err := c.SendRequest(req, true)
if err != nil {
return nil, err
}
h := []Headend{}
err = json.Unmarshal(data, &h)
if err != nil {
return nil, err
}
return h, nil
}
// GetChannels returns the channels in a given lineup
func (c *Client) GetChannels(lineupID string, verbose bool) (*ChannelResponse, error) {
url := fmt.Sprint(c.BaseURL, APIVersion, "/lineups/", lineupID)
req, httpErr := http.NewRequest("GET", url, nil)
if httpErr != nil {
return nil, httpErr
}
if verbose {
req.Header.Add("verboseMap", "true")
}
_, data, err := c.SendRequest(req, true)
if err != nil {
return nil, err
}
h := new(ChannelResponse)
err = json.Unmarshal(data, &h)
return h, err
}
// GetLineups returns a LineupResponse which contains all the lineups subscribed
// to by this account.
func (c *Client) GetLineups() (*LineupResponse, error) {
url := fmt.Sprint(c.BaseURL, APIVersion, "/lineups")
s := new(LineupResponse)
req, httpErr := http.NewRequest("GET", url, nil)
if httpErr != nil {
return nil, httpErr
}
_, data, err := c.SendRequest(req, true)
if err != nil {
return nil, err
}
err = json.Unmarshal(data, &s)
return s, err
}