-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitch.go
43 lines (35 loc) · 903 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
41
42
43
package main
import (
"encoding/json"
"errors"
"io/ioutil"
"math/rand"
"net/http"
)
const groupClusterURL = "http://tmi.twitch.tv/servers?cluster=group"
type TwitchCluster struct {
Cluster string `json:"cluster"`
Servers []string `json:"servers"`
WebsocketsServers []string `json:"websockets_servers"`
}
func getWhisperServerAddress() (string, error) {
resp, err := http.DefaultClient.Get(groupClusterURL)
if err != nil {
return "", err
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
var twitchCluster TwitchCluster
if err := json.Unmarshal(b, &twitchCluster); err != nil {
return "", err
}
if len(twitchCluster.Servers) == 0 {
return "", errors.New("no servers found in 'group' cluster")
}
// get random server
n := rand.Intn(len(twitchCluster.Servers))
return twitchCluster.Servers[n], nil
}