-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitch.go
executable file
·143 lines (108 loc) · 2.66 KB
/
twitch.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
package twitch
import (
"fmt"
"net/http"
"net/url"
"encoding/json"
"strings"
)
type FollowsResponse struct {
Follows []Follow `json:"follows"`
}
type Follow struct {
Channel Channel `json:"channel"`
}
type Channel struct {
ID string `json:"_id"`
Status string `json:"status"`
Name string `json:"name"`
Game string `json:"game"`
}
type StreamsResponse struct {
Streams []Stream `json:"streams"`
}
type Stream struct {
Viewers float64 `json:"viewers"`
FPS float64 `json:"average_fps"`
Height float64 `json:"video_height"`
Channel StreamChannel `json:"channel"`
}
type StreamChannel struct {
ID int64 `json:"_id"`
Status string `json:"status"`
Name string `json:"name"`
Game string `json:"game"`
}
type ClientConfiguration struct {
UserID int64
ClientID string
}
type TwitchClient struct {
ClientConfiguration
}
func New(conf ClientConfiguration) *TwitchClient {
return &TwitchClient{
ClientConfiguration: conf,
}
}
func (t *TwitchClient) Follows() ([]Channel, error) {
u, _ := url.Parse(fmt.Sprintf("https://api.twitch.tv/kraken/users/%d/follows/channels", t.UserID))
q := u.Query()
q.Set("limit", "100")
u.RawQuery = q.Encode()
req, err := http.NewRequest("GET", u.String(), nil)
req.Header.Add("Accept", "application/vnd.twitchtv.v5+json")
req.Header.Add("Client-ID", t.ClientID)
if err != nil {
return nil, err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("%d Status Returned: %s", resp.StatusCode, resp.Status)
}
dec := json.NewDecoder(resp.Body)
var r FollowsResponse
json_err := dec.Decode(&r)
if json_err != nil {
return nil, json_err
}
names := make([]Channel, len(r.Follows))
for i, f := range r.Follows {
names[i] = f.Channel
}
return names, nil
}
func (t *TwitchClient) Online() ([]Stream, error) {
channels, err := t.Follows()
if err != nil {
return nil, err
}
ids := make([]string, len(channels))
for i, c := range channels {
ids[i] = c.ID
}
online_url, _ := url.Parse("https://api.twitch.tv/kraken/streams/")
q := online_url.Query()
q.Add("channel", strings.Join(ids, ","))
online_url.RawQuery = q.Encode()
req, _ := http.NewRequest("GET", online_url.String(), nil)
req.Header.Add("Accept", "application/vnd.twitchtv.v5+json")
req.Header.Add("Client-ID", t.ClientID)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("%d Status Returned: %s", resp.StatusCode, resp.Status)
}
dec := json.NewDecoder(resp.Body)
var r StreamsResponse
json_err := dec.Decode(&r)
if json_err != nil {
return nil, json_err
}
return r.Streams, nil
}