-
Notifications
You must be signed in to change notification settings - Fork 6
/
nbc.go
173 lines (160 loc) · 4.05 KB
/
nbc.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
package nbc
import (
"bytes"
"encoding/json"
"errors"
"net/http"
"net/url"
"strconv"
"strings"
"time"
)
type On_Demand struct {
Playback_URL string `json:"playbackUrl"`
}
func (m Metadata) On_Demand() (*On_Demand, error) {
req, err := http.NewRequest("GET", "https://lemonade.nbc.com", nil)
if err != nil {
return nil, err
}
req.URL.Path = func() string {
b := []byte("/v1/vod/")
b = strconv.AppendInt(b, m.MPX_Account_ID, 10)
b = append(b, '/')
b = strconv.AppendInt(b, m.MPX_GUID, 10)
return string(b)
}()
req.URL.RawQuery = url.Values{
"platform": {"web"},
"programmingType": {m.Programming_Type},
}.Encode()
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, errors.New(res.Status)
}
video := new(On_Demand)
if err := json.NewDecoder(res.Body).Decode(video); err != nil {
return nil, err
}
return video, nil
}
type Metadata struct {
Air_Date string `json:"airDate"`
Episode_Number int64 `json:"episodeNumber,string"`
MPX_Account_ID int64 `json:"mpxAccountId,string"`
MPX_GUID int64 `json:"mpxGuid,string"`
Programming_Type string `json:"programmingType"`
Season_Number int64 `json:"seasonNumber,string"`
Secondary_Title string `json:"secondaryTitle"`
Series_Short_Title string `json:"seriesShortTitle"`
}
const query = `
query bonanzaPage(
$app: NBCUBrands!
$name: String!
$oneApp: Boolean
$platform: SupportedPlatforms!
$type: EntityPageType!
$userId: String!
) {
bonanzaPage(
app: $app
name: $name
oneApp: $oneApp
platform: $platform
type: $type
userId: $userId
) {
metadata {
... on VideoPageData {
airDate
episodeNumber
mpxAccountId
mpxGuid
programmingType
seasonNumber
secondaryTitle
seriesShortTitle
}
}
}
}
`
func New_Metadata(guid int64) (*Metadata, error) {
body, err := func() ([]byte, error) {
var p page_request
p.Variables.Name = strconv.FormatInt(guid, 10)
p.Query = graphQL_compact(query)
p.Variables.App = "nbc"
p.Variables.One_App = true
p.Variables.Platform = "android"
p.Variables.Type = "VIDEO"
return json.MarshalIndent(p, "", " ")
}()
if err != nil {
return nil, err
}
res, err := http.Post(
"https://friendship.nbc.co/v2/graphql", "application/json",
bytes.NewReader(body),
)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, errors.New(res.Status)
}
var s struct {
Data struct {
Bonanza_Page struct {
Metadata Metadata
} `json:"bonanzaPage"`
}
Errors []struct {
Message string
}
}
if err := json.NewDecoder(res.Body).Decode(&s); err != nil {
return nil, err
}
if len(s.Errors) >= 1 {
return nil, errors.New(s.Errors[0].Message)
}
return &s.Data.Bonanza_Page.Metadata, nil
}
func (m Metadata) Series() string {
return m.Series_Short_Title
}
func (m Metadata) Season() (int64, error) {
return m.Season_Number, nil
}
func (m Metadata) Episode() (int64, error) {
return m.Episode_Number, nil
}
func (m Metadata) Title() string {
return m.Secondary_Title
}
func (m Metadata) Date() (time.Time, error) {
return time.Parse(time.RFC3339, m.Air_Date)
}
// this is better than strings.Replace and strings.ReplaceAll
func graphQL_compact(s string) string {
f := strings.Fields(s)
return strings.Join(f, " ")
}
type page_request struct {
Query string `json:"query"`
Variables struct {
App string `json:"app"` // String cannot represent a non string value
Name string `json:"name"`
One_App bool `json:"oneApp"`
Platform string `json:"platform"`
Type string `json:"type"` // can be empty
User_ID string `json:"userId"`
} `json:"variables"`
}