-
Notifications
You must be signed in to change notification settings - Fork 4
/
GoSchedulesDirect.go
811 lines (700 loc) · 26.2 KB
/
GoSchedulesDirect.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
// Package GoSchedulesDirect provides structs and functions to interact with
// the SchedulesDirect JSON listings service in Go.
package GoSchedulesDirect
import (
"bufio"
"bytes"
"crypto/sha1"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"time"
)
//The proper SchedulesDirect JSON Service workflow is as follows...
//------Once the client is in a steady state:-------
//-Obtain a token
//-Obtain the current status.
//-If the system status is "OFFLINE" then disconnect; all further processing
// will be rejected at the server. A client should not attempt to reconnect
// for 1 hour.
//-Check the status object and determine if any headends on the server have
// newer "modified" dates than the one that is on the client. If yes, download
// the updated lineup for that headend.
//-If there are no changes to the headends, send a request to the server for
// the MD5 hashes of the schedules that you are interested in. If the MD5
// hash for the schedule is the same as you have locally cached from your
// last download, then the schedule on the server hasn't changed and your
// client should disconnect.
//-If the MD5 hash for the schedule is different, then download the schedules
// that have different hashes.
//-Honor the nextScheduled time in the status object; if your client connects
// during server-side data processing, the nextScheduled time will be
// "closer", however reconnecting while server-side data is being processed
// will not result in newer data.
//-Parse the schedule, determine if the MD5 of the program for a particular
// timeslot has changed. If the program ID for a timeslot is the same, but
// the MD5 has changed, this means that some sort of metadata for that
// program has been updated.
//-Request the "delta" program id's as determined through the MD5 values.
// Some constants for use in the library
var (
APIVersion = "20141201"
DefaultBaseURL = "https://json.schedulesdirect.org/"
UserAgent = "TBD"
ActiveClient *Client
)
// A TokenResponse stores the SD json response message for token request.
type TokenResponse struct {
Code int `json:"code"`
Message string `json:"message"`
ServerID string `json:"serverID"`
Token string `json:"token"`
}
// A VersionResponse stores the SD json response message for a version request.
type VersionResponse struct {
Response string `json:"response"`
Code int `json:"code"`
Client string `json:"client"`
Version string `json:"version,omitempty"`
ServerID string `json:"serverID"`
DateTime time.Time `json:"datetime"`
}
// An AddLineupResponse stores the SD json message returned after attempting
// to add a lineup.
type AddLineupResponse struct {
Response string `json:"response"`
Code int `json:"code"`
ServerID string `json:"serverID"`
Message string `json:"message"`
ChangesRemaining int `json:"changesRemaining"`
DateTime time.Time `json:"datetime"`
}
// A LineupResponse stores the SD json message returned after requesting
// to list subscribed lineups.
type LineupResponse struct {
ServerID string `json:"serverID"`
DateTime time.Time `json:"datetime"`
Lineups []Lineup `json:"lineups"`
}
// A StatusResponse stores the SD json message returned after requesting system
// status. SystemStatus[0].Status should be "Online" before proceeding.
type StatusResponse struct {
Account AccountInfo `json:"account"`
Lineups []Lineup `json:"lineups"`
LastDataUpdate string `json:"lastDataUpdate"`
Notifications []string `json:"notifications"`
SystemStatus []Status `json:"systemStatus"`
ServerID string `json:"serverID"`
Code int `json:"code"`
}
// A StatusError struct stores the error response to a status request.
type StatusError struct {
Response string `json:"response"`
Code int `json:"code"`
ServerID string `json:"serverID"`
Message string `json:"message"`
Datetime string `json:"datetime"`
Token string `json:"token"`
}
// A Status stores the SD json message containing system status information
// usually as part of a StatusResponse.
type Status struct {
Date string `json:"date"`
Status string `json:"status"`
Details string `json:"details"`
}
// An AccountInfo stores the SD json message containing account information
// usually as part of a StatusResponse.
type AccountInfo struct {
Expires string `json:"expires"`
Messages []string `json:"messages"`
MaxLineups int `json:"maxLineups"`
NextSuggestedConnectTime string `json:"nextSuggestedConnectTime"`
}
// A Headend stores the SD json message containing information for a headend.
type Headend struct {
Headend string `json:"headend"`
Transport string `json:"transport"`
Location string `json:"location"`
Lineups []Lineup `json:"lineups"`
}
// A Lineup stores the SD json message containing lineup information.
type Lineup struct {
Lineup string `json:"lineup,omitempty"`
Name string `json:"name,omitempty"`
ID string `json:"ID,omitempty"`
Modified string `json:"modified,omitempty"`
URI string `json:"uri"`
IsDeleted bool `json:"isDeleted,omitempty"`
}
// A BroadcasterInfo stores the information about a broadcaster.
type BroadcasterInfo struct {
City string `json:"city"`
State string `json:"state"`
Postalcode string `json:"postalcode"`
Country string `json:"country"`
}
// A ChannelResponse stores the channel response for a lineup
type ChannelResponse struct {
Map []ChannelMap `json:"map"`
Stations []Station `json:"stations"`
Metadata ChannelResponseMeta `json:"metadata"`
}
// A ChannelResponseMeta stores the metadata field associated with a channel response
type ChannelResponseMeta struct {
Lineup string `json:"lineup"`
Modified time.Time `json:"modified"`
Transport string `json:"transport"`
}
// A Station stores the SD json that describes a station.
type Station struct {
Callsign string `json:"callsign"`
Affiliate string `json:"affiliate"`
IsCommercialFree bool `json:"isCommercialFree"`
Name string `json:"name"`
Broadcaster BroadcasterInfo `json:"broadcaster"`
BroadcastLanguage []string `json:"broadcastLanguage"`
DescriptionLanguage []string `json:"descriptionLanguage "`
Logo StationLogo `json:"logo"`
StationID string `json:"stationID"`
}
// A StationLogo stores the information to locate a station logo
type StationLogo struct {
URL string `json:"URL"`
Height int `json:"height"`
Width int `json:"width"`
Md5 string `json:"md5"`
}
// A ChannelMap stores the station id to channel mapping
type ChannelMap struct {
StationID string `json:"stationID"`
Channel string `json:"channel,omitempty"`
UhfVhf int `json:"uhfVhf,omitempty"`
AtscMajor int `json:"atscMajor,omitempty"`
AtscMinor int `json:"atscMinor,omitempty"`
}
// A Schedule stores the program information for a given stationID
type Schedule struct {
StationID string `json:"stationID"`
MetaData ScheduleMeta `json:"metadata"`
Programs []Program `json:"programs"`
}
// A ScheduleMeta stores the metadata information for a schedule
type ScheduleMeta struct {
Modified string `json:"modified"`
MD5 string `json:"md5"`
StartDate string `json:"startDate"`
EndDate string `json:"endDate"`
Days int `json:"days"`
}
// A SyndicationType stores syndication information for a program
type SyndicationType struct {
Source string `json:"source"`
Type string `json:"type"`
}
// A Program stores the information to describing a single television program.
type Program struct {
ProgramID string `json:"programID,omitempty"`
AirDateTime time.Time `json:"airDateTime,omitempty"`
Md5 string `json:"md5,omitempty"`
Duration int `json:"duration,omitempty"`
New bool `json:"new,omitempty"`
CableInTheClassroom bool `json:"cableInTheClassRoom,omitempty"`
Catchup bool `json:"catchup,omitempty"` // - typically only found outside of North America
Continued bool `json:"continued,omitempty"` // - typically only found outside of North America
Education bool `json:"educational,omitempty"`
JoinedInProgress bool `json:"joinedInProgress,omitempty"`
LeftInProgress bool `json:"leftInProgress,omitempty"`
Premiere bool `json:"premiere,omitempty"` //- Should only be found in Miniseries and Movie program types.
ProgramBreak bool `json:"programBreak,omitempty"` // - Program stops and will restart later (frequently followed by a continued). Typically only found outside of North America.
Repeat bool `json:"repeat,omitempty"` // - An encore presentation. Repeat should only be found on a second telecast of sporting events.
Signed bool `json:"signed,omitempty"` //- Program has an on-screen person providing sign-language translation.
SubjectToBlackout bool `json:"subjectToBlackout,omitempty"` //subjectToBlackout
TimeApproximate bool `json:"timeApproximate,omitempty"`
AudioProperties []string `json:"audioProperties,omitempty"`
Syndication SyndicationType `json:"syndication,omitempty"`
Ratings []ProgramRating `json:"ratings,omitempty"`
ProgramPart Part `json:"multipart,omitempty"`
VideoProperties []string `json:"videoProperties,omitempty"`
}
// A ProgramRating stores ratings board information for a program
type ProgramRating struct {
Body string `json:"body"`
Code string `json:"code"`
}
// A ProgramInfo type stores program information for a program
type ProgramInfo struct {
ProgramID string `json:"programID,omitempty"`
Titles []struct {
Title120 string `json:"title120,omitempty"`
} `json:"titles,omitempty"`
EventDetails *struct {
SubType *string `json:"subType,omitempty"`
} `json:"eventDetails,omitempty"`
Descriptions struct {
Description1000 []struct {
Description string `json:"description"`
DescriptionLanguage string `json:"descriptionLanguage"`
} `json:"description1000"`
} `json:"descriptions"`
OriginalAirDate string `json:"originalAirDate"`
Genres []string `json:"genres,omitempty"`
EpisodeTitle150 string `json:"episodeTitle150,omitempty"`
Metadata []*struct {
Gracenote *struct {
Episode *int `json:"episode,omitzero"`
Season *int `json:"season,omitzero"`
TotalEpisodes *int `json:"totalEpisodes,omitempty"`
} `json:"Gracenote,omitempty"`
} `json:"metadata"`
Movie *Movie `json:"movie,omitempty"`
Cast []*Person `json:"cast,omitempty"`
Crew []*Person `json:"crew,omitempty"`
ContentRating []struct {
Body string `json:"body"`
Code string `json:"code"`
} `json:"contentRating,omitempty"`
EntityType string `json:"entityType,omitempty"`
ShowType string `json:"showType"`
HasImageArtWork bool `json:"hasImageArtwork"`
Md5 string `json:"md5"`
}
// A ProgramMetaItem stores meta information for a program
type ProgramMetaItem struct {
string map[string] struct {
TotalEpisodes int `json:"totalEpisodes,ommitempty"`
Season int `json:"season,omitempty"`
Episode int `json:"episode,omitmepty"`
}
}
// A Movie type stores information about a movie
type Movie struct {
Duration *int `json:"duration,omitempty"`
Year *string `json:"year,omitempty"`
QualityRating []*struct {
Increment *string `json:"increment,omitempty"`
MaxRating *string `json:"maxRating,omitempty"`
MinRating *string `json:"minRating,omitempty"`
Rating *string `json:"rating,omitempty"`
RatingsBody *string `json:"ratingsBody,omitmepty"`
} `json:"qualityRating,omitempty"`
}
// Person stores information for an acting credit.
type Person struct {
PersonID *string `json:"personId,omitmepty"`
NameID *string `json:"nameId,omitempty"`
Name *string `json:"name,omitempty"`
Role *string `json:"role,omitempty"`
CharacterName *string `json:"characterName,omitempty"`
BillingOrder *string `json:"billingOrder,omitempty"`
}
// ProgramInfoError stores the error response for a program request
type ProgramInfoError struct {
Response string `json:"reponse"`
Code int `json:"code"`
ServerID string `json:"serverID"`
Message string `json:"message"`
DateTime time.Time `json:"datetime"`
}
// ProgramDescriptions stores the descriptive summaries for a program
type ProgramDescriptions struct {
Description100 []Description `json:"description100,omitempty"`
Description1000 []Description `json:"description1000,omitempty"`
}
// Description helps store the descriptions for programs
type Description struct {
DescriptionLanguage string `json:"descriptionLanguage"`
Description string `json:"description"`
}
// Part stores the information for a part
type Part struct {
PartNumber int `json:"partNumber"`
TotalParts int `json:"totalParts"`
}
// LastmodifiedRequest stores the information needed to make a last modified request.
type LastmodifiedRequest struct {
StationID string `json:"stationID"`
Days int `json:"days"`
}
// Client type
type Client struct {
// Our HTTP client to communicate with SD
//client *http.Client
// The Base URL for SD requests
BaseURL *url.URL
// HTTP
HTTP *http.Client
// The token
Token string
// User agent string
UserAgent string
}
// NewClient returns a new SD API client. Uses http.DefaultClient if no
// client is provided.
// TODO Add userAgent string once determined
func NewClient(username string, password string) *Client {
baseURL, _ := url.Parse(DefaultBaseURL)
c := &Client{HTTP: &http.Client{}, BaseURL: baseURL}
token, _ := c.GetToken(username, password)
c.Token = token
ActiveClient = c
return c
}
// encryptPassword returns the sha1 hex encoding of the string argument
func encryptPassword(password string) string {
encoded := sha1.New()
encoded.Write([]byte(password))
return hex.EncodeToString(encoded.Sum(nil))
}
// GetToken returns a session token if the supplied username/password
// successfully authenticate.
func (c Client) GetToken(username string, password string) (string, error) {
// The SchedulesDirect token url
url := fmt.Sprint(DefaultBaseURL, APIVersion, "/token")
// encrypt the password
sha1hexPW := encryptPassword(password)
// TODO: Evaluate performance of this string concatenation, not that this
// should run often.
var jsonStr = []byte(
`{"username":"` + username +
`", "password":"` + sha1hexPW + `"}`)
// setup the request
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
req.Header.Set("Content-Type", "application/json")
// perform the POST
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close() //resp.Body.Close() will run when we're finished.
// create a TokenResponse struct, return if err
r := new(TokenResponse)
// decode the response body into the new TokenResponse struct
err = json.NewDecoder(resp.Body).Decode(r)
if err != nil {
return "", err
}
// Print some debugging output
//fmt.Println("response Status:", resp.Status)
//fmt.Println("response Headers:", resp.Header)
//body, _ := ioutil.ReadAll(resp.Body)
//fmt.Println("response Body:", string(body))
// return the token string
return r.Token, nil
}
// GetStatus returns a StatusResponse for this account.
func (c Client) GetStatus() (*StatusResponse, error) {
url := fmt.Sprint(DefaultBaseURL, APIVersion, "/status")
fmt.Println("URL:>", url)
s := new(StatusResponse)
req, err := http.NewRequest("GET", url, nil)
req.Header.Set("token", c.Token)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
return s, err
}
if resp.StatusCode != http.StatusOK {
log.Fatal(resp.Status)
return s, err
}
defer resp.Body.Close() //resp.Body.Close() will run when we're finished.
// Copy the body to Stdout
//_, err = io.Copy(os.Stdout, resp.Body)
err = json.NewDecoder(resp.Body).Decode(s)
if err != nil {
fmt.Println("Error parsing status response")
log.Fatal(err)
return s, err
}
//fmt.Println("Current Status is: ")
//fmt.Println(s.SystemStatus[0].Status)
return s, nil
}
// AddLineup adds the given lineup uri to the users SchedulesDirect account.
func (c Client) AddLineup(lineupURI string) error {
//url := "https://json.schedulesdirect.org" + lineupURI
url := fmt.Sprint(DefaultBaseURL, lineupURI)
fmt.Println("URL:>", url)
req, err := http.NewRequest("PUT", url, nil)
req.Header.Set("token", c.Token)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
return err
}
if resp.StatusCode != http.StatusOK {
log.Fatal(resp.Status)
return err
}
defer resp.Body.Close() //resp.Body.Close() will run when we're finished.
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println("Add Lineup Response:", string(body))
return nil
}
// DelLineup deletes the given lineup uri from the users SchedulesDirect account.
func (c Client) DelLineup(lineupURI string) error {
//url := "https://json.schedulesdirect.org" + lineupURI
url := fmt.Sprint(DefaultBaseURL, lineupURI)
fmt.Println("URL:>", url)
req, err := http.NewRequest("DELETE", url, nil)
req.Header.Set("token", c.Token)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
return err
}
if resp.StatusCode != http.StatusOK {
log.Fatal(resp.Status)
return err
}
defer resp.Body.Close() //resp.Body.Close() will run when we're finished.
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println("Delete Lineup Response: Premiere //- Should only be found in Miniseries and Movie program types.", string(body))
return nil
}
// GetHeadends returns the map of headends for the given postal code.
func (c Client) GetHeadends(postalCode string) ([]Headend, error) {
url := fmt.Sprint(DefaultBaseURL, APIVersion,
"/headends?country=USA&postalcode=", postalCode)
fmt.Println("URL:>", url)
req, err := http.NewRequest("GET", url, nil)
req.Header.Set("token", c.Token)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
return nil, err
}
if resp.StatusCode != http.StatusOK {
log.Fatal(resp.Status)
return nil, err
}
defer resp.Body.Close() //resp.Body.Close() will run when we're finished.
// make the slice of headends
h := []Headend{}
//body, _ := ioutil.ReadAll(resp.Body)
//fmt.Println("PostalResponse Body:", string(body))
// decode the body
err = json.NewDecoder(resp.Body).Decode(&h)
if err != nil {
fmt.Println("Error parsing headend responseline")
log.Fatal(err)
return nil, err
}
return h, nil
}
// GetChannels returns the channels in a given lineup
func (c Client) GetChannels(lineupURI string) (*ChannelResponse, error) {
url := fmt.Sprint(DefaultBaseURL, lineupURI)
fmt.Println("URL:>", url)
req, err := http.NewRequest("GET", url, nil)
req.Header.Set("token", c.Token)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
return nil, err
}
if resp.StatusCode != http.StatusOK {
log.Fatal(resp.Status)
return nil, err
}
defer resp.Body.Close() //resp.Body.Close() will run when we're finished.
// make the map
h := new(ChannelResponse)
// debug code
//body, _ := ioutil.ReadAll(resp.Body)
//fmt.Println(string(body))
// decode the body into the map
err = json.NewDecoder(resp.Body).Decode(&h)
if err != nil {
fmt.Println("Error parsing channel response line")
log.Fatal(err)
return nil, err
}
return h, nil
}
// GetSchedules returns the set of schedules requested. As a whole the response is not valid json but each individual line is valid.
func (c Client) GetSchedules(stationIds []string, dates []string) ([]Schedule, error) {
url := fmt.Sprint(DefaultBaseURL, APIVersion, "/schedules")
fmt.Println("URL:>", url)
//buffer to store the json request
var buffer bytes.Buffer
//creating the request
buffer.WriteString("[")
for index, station := range stationIds {
//fmt.Println(station)
buffer.WriteString(`{"stationID":"`+ station + `","date":[`)
for index2, date := range dates {
buffer.WriteString(`"`+date+`"`)
if index2 != len(dates)-1 {
buffer.WriteString(",")
} else {
buffer.WriteString("]")
}
}
if index != len(stationIds)-1 {
buffer.WriteString("},")
} else {
buffer.WriteString("}")
}
}
buffer.WriteString("]")
//setup the request
req, err := http.NewRequest("POST", url, &buffer)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept-Encoding", "deflate,gzip")
req.Header.Set("token", c.Token)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
return nil, err
}
if resp.StatusCode != http.StatusOK {
log.Fatal(resp.Status)
return nil, err
}
defer resp.Body.Close() //resp.Body.Close() will run when we're finished.
// decode the response
var h []Schedule
// debug code
//body, _ := ioutil.ReadAll(resp.Body)
//fmt.Println(string(body))
// decode the body
err = json.NewDecoder(resp.Body).Decode(&h)
if err != nil {
fmt.Println("Error parsing schedules response")
log.Fatal(err)
return nil, err
}
return h, nil
}
// GetProgramInfo returns the set of program details for the given set of programs
func (c Client) GetProgramInfo(programIDs []string) ([]ProgramInfo, error) {
url := fmt.Sprint(DefaultBaseURL, APIVersion, "/programs")
fmt.Println("URL:>", url)
// buffer to store the json request
var buffer bytes.Buffer
// creating the request
buffer.WriteString("[")
for index, program := range programIDs {
//fmt.Println(program) //debug
if(program != "") {
buffer.WriteString(`"`)
buffer.WriteString(program)
buffer.WriteString(`"`)
if index != len(programIDs)-1 {
buffer.WriteString(",")
}
}
}
buffer.WriteString("]")
// setup the request
req, err := http.NewRequest("POST", url, &buffer)
//req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept-Encoding", "deflate,gzip")
req.Header.Set("token", c.Token)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
return nil, err
}
if resp.StatusCode != http.StatusOK {
log.Fatal(resp.Status)
return nil, err
}
defer resp.Body.Close() //resp.Body.Close() will run when we're finished.
// Copy the body to Stdout
//_, err = io.Copy(os.Stdout, resp.Body)
// debug code
//body, _ := ioutil.ReadAll(resp.Body)
//fmt.Println(string(body))
// create the programs slice
var allPrograms []ProgramInfo
// // decode the body
// err = json.NewDecoder(resp.Body).Decode(&allPrograms)
// if err != nil {
// fmt.Println("Error parsing programs response")
// log.Fatal(err)
// return nil, err
// }
//readbuffer := bytes.NewBuffer(resp.Body)
reader := bufio.NewReader(resp.Body)
// we need to increase the default reader size to get this in one shot
bufio.NewReaderSize(reader, 65536)
// there are a few possible loop termination
// conditions, so just start with an infinite loop.
for {
// ReadString because Schedules Direct puts each schedule on it's own line
// each line is a complete json object but not the whole response.
line, err := reader.ReadString('\n')
//debug
//fmt.Println(line)
// loop termination condition 1: EOF.
// this is the normal loop termination condition.
if err == io.EOF {
break
}
// loop termination condition 2: some other error.
// Errors happen, so check for them and do something with them.
if err != nil {
log.Fatal(err)
}
// create a Schedule variable
//var s ProgramInfo
// decode the scanner bytes into the schedule
errUnmarshal := json.Unmarshal([]byte(line), &allPrograms)
if errUnmarshal != nil {
log.Printf("error unmarshaling program: %s\n", errUnmarshal)
} else {
//allPrograms = append(allPrograms, s)
}
}
return allPrograms, err
}
// GetLastModified returns
func (c Client) GetLastModified(theRequest []LastmodifiedRequest) {
url := fmt.Sprint(DefaultBaseURL, APIVersion, "/schedules/md5")
fmt.Println("URL:>", url)
}
// GetLineups returns a LineupResponse which contains all the lineups subscribed
// to by this account.
func (c Client) GetLineups() (*LineupResponse, error) {
url := fmt.Sprint(DefaultBaseURL, APIVersion, "/lineups")
fmt.Println("URL:>", url)
s := new(LineupResponse)
req, err := http.NewRequest("GET", url, nil)
req.Header.Set("token", c.Token)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
return s, err
}
if resp.StatusCode != http.StatusOK {
log.Fatal(resp.Status)
return s, err
}
defer resp.Body.Close() //resp.Body.Close() will run when we're finished.
//Copy the body to Stdout
//_, err = io.Copy(os.Stdout, resp.Body)
err = json.NewDecoder(resp.Body).Decode(s)
if err != nil {
fmt.Println("Error parsing status response")
log.Fatal(err)
return s, err
}
return s, nil
}