forked from lyon-esport/TwitchPromExporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
207 lines (174 loc) · 4.6 KB
/
client.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package main
import (
"encoding/json"
"errors"
"fmt"
log "github.com/sirupsen/logrus"
"io"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"time"
)
// StreamData represents the data a single stream
type StreamData struct {
ID string `json:"id"`
UserID string `json:"user_id"`
UserName string `json:"user_name"`
GameID string `json:"game_id"`
CommunityIds []string `json:"community_ids"`
Type string `json:"type"`
Title string `json:"title"`
ViewerCount int `json:"viewer_count"`
StartedAt time.Time `json:"started_at"`
Language string `json:"language"`
ThumbnailURL string `json:"thumbnail_url"`
}
// UserData struct represents a user as defined by the twitch api
type UserData struct {
ID string `json:"id"`
Login string `json:"login"`
DisplayName string `json:"display_name"`
Type string `json:"type"`
BroadcasterType string `json:"broadcaster_type"`
Description string `json:"description"`
ProfileImageURL string `json:"profile_image_url"`
OfflineImageURL string `json:"offline_image_url"`
ViewCount int `json:"view_count"`
Email string `json:"email"`
}
type UserFollow struct {
Total int `json:"total"`
}
type Streams struct {
Data []StreamData `json:"data"`
}
type Users struct {
Data []UserData `json:"data"`
}
// Client represents a client to interact with the twitch API
type Client struct {
ClientID string
httpClient *http.Client
}
// NewClient will initialize a new client for the twitch api
func NewClient(cid string) *Client {
return &Client{
ClientID: cid,
httpClient: &http.Client{},
}
}
func (c Client) doRequest(method, uri string, body io.Reader) (*http.Response, error) {
r, err := http.NewRequest(method, uri, body)
if err != nil {
return nil, err
}
r.Header.Add("Client-ID", c.ClientID)
res, err := c.httpClient.Do(r)
if err != nil {
return nil, err
}
if res.StatusCode == 401 {
return nil, errors.New("server returned 401. likely caused due to an invalid client id")
} else if res.StatusCode != 200 {
return nil, fmt.Errorf("server returned non 200 status code. status code: %v", res.StatusCode)
}
return res, nil
}
// GetStreams will get a list of live Streams
// The url query parameter are defined by the GetStreamsInput struct
func (c Client) GetStreams(streamsList []string) ([]StreamData, int, error) {
// since first, when uninitialized is 0, we have to set it to the default value
/*if i.First == 0 {
i.First = 20
}*/
var uri *url.URL
uri, err := url.Parse(baseURL + getStreamsEndpoint)
if err != nil {
return nil, 0, err
}
query := uri.Query()
for _, stream := range streamsList {
query.Add("user_login", stream)
}
uri.RawQuery = query.Encode()
res, err := c.doRequest("GET", uri.String(), nil)
if err != nil {
return nil, 0, err
}
defer res.Body.Close()
s := Streams{}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, 0, err
}
var token int
strToken, ok := res.Header["Ratelimit-Remaining"]
if ok {
if len(strToken) > 0 {
token, err = strconv.Atoi(strToken[0])
if err != nil {
token = 0
}
}
} else {
token = 0
}
log.Debugf("Remaining tokens: %d", token)
json.Unmarshal(body, &s)
return s.Data, token, nil
}
// GetStreams will get a list of live Streams
// The url query parameter are defined by the GetStreamsInput struct
func (c Client) GetUsers(usersList []string) ([]UserData, error) {
var uri *url.URL
uri, err := url.Parse(baseURL + getUsersEndpoint)
if err != nil {
return nil, err
}
query := uri.Query()
for _, user := range usersList {
query.Add("login", user)
}
uri.RawQuery = query.Encode()
log.Debug("URI: ", uri.String())
res, err := c.doRequest("GET", uri.String(), nil)
if err != nil {
return nil, err
}
defer res.Body.Close()
s := Users{}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
json.Unmarshal(body, &s)
return s.Data, nil
}
// GetStreams will get a list of live Streams
// The url query parameter are defined by the GetStreamsInput struct
func (c Client) GetFollows(userID string) (int, error) {
var uri *url.URL
uri, err := url.Parse(baseURL + getUserFollow)
if err != nil {
return 0, err
}
query := uri.Query()
query.Add("to_id", userID)
uri.RawQuery = query.Encode()
log.Debug("URI: ", uri.String())
res, err := c.doRequest("GET", uri.String(), nil)
if err != nil {
return 0, err
}
defer res.Body.Close()
s := UserFollow{}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return 0, err
}
json.Unmarshal(body, &s)
// fmt.Println(s.Total)
return s.Total, nil
}