-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstreams.go
136 lines (114 loc) · 3 KB
/
streams.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
package twch
import (
"fmt"
)
type Streams struct {
client *Client
}
type streamChannel struct {
Links interface{} `json:"_links,omitempty"`
Stream Stream `json:"stream,omitempty"`
}
type streamList struct {
Streams []Stream `json:"streams,omitempty"`
*listLinks
*listTotal
}
type featuredResponse struct {
Featured []FeaturedStream `json:"featured,omitempty"`
*listLinks
}
type streamFollowed struct {
Links interface{} `json:"_links,omitempty"`
Total int `json:"_total,omitempty"`
Streams []Stream `json:"streams,omitempty"`
}
type FeaturedStream struct {
Image *string `json:"image,omitempty"`
Text *string `json:"text,omitempty"`
Stream *Stream `json:"stream,omitempty"`
}
type StreamSummary struct {
Viewers int `json:"viewers,omitempty"`
Channels int `json:"channels,omitempty"`
}
type Stream struct {
ID *int `json:"_id,omitempty"`
Viewers *int `json:"viewers,omitempty"`
CreatedAt *string `json:"created_at,omitempty"`
Preview *Asset `json:"preview,omitempty"`
Channel *Channel `json:"channel,omitempty"`
Game *string `json:"game,omitempty"`
}
type StreamOptions struct {
Game string `url:"game,omitempty"`
Channel string `url:"channel,omitempty"`
Embeddable bool `url:"embeddable,omitempty"`
ClientID string `url:"client_id,omitempty"`
RequestOptions
}
// Summary returns viewership and channel count for all streams currently on Twitch
func (s *Streams) GetSummary() (summary *StreamSummary, resp *Response, err error) {
req, err := s.client.NewRequest("GET", "streams/summary")
if err != nil {
return
}
r := new(StreamSummary)
resp, err = s.client.Do(req, r)
if err != nil {
return
}
return r, resp, nil
}
// ListStreams lists all the current streams on Twitch
func (s *Streams) ListStreams(opts *StreamOptions) (streams []Stream, resp *Response, err error) {
u, err := appendOptions("streams", opts)
if err != nil {
return
}
req, err := s.client.NewRequest("GET", u)
if err != nil {
return
}
r := new(streamList)
resp, err = s.client.Do(req, r)
if err != nil {
return
}
streams = r.Streams
return
}
// ListFeaturedStreams returns the streams featured on the front page of twitch
func (s *Streams) ListFeaturedStreams(opts *RequestOptions) (f []FeaturedStream, resp *Response, err error) {
u, err := appendOptions("streams/featured", opts)
if err != nil {
return
}
req, err := s.client.NewRequest("GET", u)
if err != nil {
return
}
r := new(featuredResponse)
resp, err = s.client.Do(req, r)
if err != nil {
return
}
f = r.Featured
return
}
// GetStream returns a channel's stream, if live.
//
// If the channel is offline, a zeroed Stream is returned without error.
func (s *Streams) GetStream(channel string) (stream *Stream, resp *Response, err error) {
uri := fmt.Sprintf("streams/%s", channel)
req, err := s.client.NewRequest("GET", uri)
if err != nil {
return
}
r := new(streamChannel)
resp, err = s.client.Do(req, r)
if err != nil {
return
}
return &r.Stream, resp, nil
}