-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtwitch.go
40 lines (36 loc) · 1000 Bytes
/
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
package main
import (
"encoding/json"
"github.com/franela/goreq"
"net/url"
)
// Requests streams from the given channels (comma-delimited).
func GetTwitchStreams(channels string) (sr TwitchStreamsResponse, err error) {
query := url.Values{}
query.Add("channel", channels)
req := goreq.Request{
Uri: "https://api.twitch.tv/kraken/streams",
QueryString: query,
}
req.AddHeader("Client-ID", twitchClientId)
res, err := req.Do()
if err != nil {
return TwitchStreamsResponse{}, err
}
defer res.Body.Close()
dec := json.NewDecoder(res.Body)
if err = dec.Decode(&sr); err != nil {
return TwitchStreamsResponse{}, err
}
return
}
// Filters streams from `GetTwitchStreams` matching `gameFilter`.
func FilterStreams(sr TwitchStreamsResponse) (items []ResponseItem) {
for _, stream := range sr.Streams {
if !gameFilter.MatchString(stream.Game) {
continue
}
items = append(items, ResponseItem{Name: stream.Channel.DisplayName, URL: stream.Channel.URL})
}
return
}