-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
138 lines (116 loc) · 3.34 KB
/
api.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
package gotau
import (
"encoding/json"
"fmt"
"math"
"strings"
)
// GetStreamers can be used to get a list of all the streamers that TAU is listening for going live alerts.
func (c *Client) GetStreamers() ([]*TAUStreamer, error) {
body, err := c.apiRequest("streamers", nil, nil, "GET")
if err != nil {
return nil, err
}
var streamers []*TAUStreamer
err = json.Unmarshal(body, &streamers)
if err != nil {
return nil, err
}
return streamers, nil
}
// GetLatestStreamForStreamer gets the latest stream for a given streamer
func (c *Client) GetLatestStreamForStreamer(ID string) (*TAUStream, error) {
ID = strings.TrimSpace(ID)
if ID == "" {
return nil, BadRequestError{
"invalid request, ID can't be blank",
}
}
body, err := c.apiRequest(fmt.Sprintf("streamers/%s/streams/latest", ID), nil, nil, "GET")
if err != nil {
return nil, err
}
stream := new(TAUStream)
err = json.Unmarshal(body, stream)
if err != nil {
return nil, err
}
return stream, nil
}
// FollowStreamerOnTau follows the users and subscribes for notifications when they go live
func (c *Client) FollowStreamerOnTau(username string) (*TAUStreamer, error) {
type tmp struct {
Username string `json:"twitch_username"`
Streaming bool `json:"streaming"`
Disabled bool `json:"disabled"`
}
username = strings.TrimSpace(username)
if username == "" {
return nil, BadRequestError{
Err: "invalid request, username can't be blank",
}
}
data := tmp{
Username: username,
}
body, err := json.Marshal(data)
if err != nil {
return nil, err
}
responseBody, err := c.apiRequest("streamers", nil, body, "POST")
if err != nil {
return nil, err
}
streamer := new(TAUStreamer)
err = json.Unmarshal(responseBody, streamer)
if err != nil {
return nil, err
}
return streamer, nil
}
// GetStreamsForStreamer will get n streams for a streamer. If maximumStreams is set to -1 then all
// streams will be gathered. This may take some time due to pagination. In the case where there are fewer
// results than the maximumStreams, those results will be returned. The number of results you get may be slightly
// more than maximumResults, based on the pagination of the results. If you request 0 results, you will get 0 results.
func (c *Client) GetStreamsForStreamer(streamerID string, maximumStreams int) ([]TAUStream, error) {
type tmp struct {
Streams []TAUStream `json:"results"`
Previous *string `json:"previous"`
Next *string `json:"next"`
Count int `json:"count"`
}
streamerID = strings.TrimSpace(streamerID)
if streamerID == "" {
return nil, BadRequestError{
Err: "invalid request, streamer id can't be blank",
}
}
results := make([]TAUStream, 0)
url := fmt.Sprintf("streamers/%s/streams", streamerID)
getAll := maximumStreams < 0
// if there are more results than that, we've fucked up
if getAll {
maximumStreams = math.MaxInt64
}
count := 0
for count <= maximumStreams {
body, err := c.apiRequest(url, nil, nil, "GET")
if err != nil {
return nil, err
}
tmpData := new(tmp)
err = json.Unmarshal(body, tmpData)
if err != nil {
return nil, err
}
results = append(results, tmpData.Streams...)
// no data left so abort early whether or not we've got enough data
if tmpData.Next == nil {
break
} else {
url = *tmpData.Next
}
count += len(tmpData.Streams)
}
return results, nil
}