-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitch.go
164 lines (138 loc) · 3.34 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package twitch
import (
"errors"
"fmt"
"sync"
"time"
"github.com/nicklaw5/helix"
)
type onlineCallbackFunc func(stream Stream)
type offlineCallbackFunc func(channelID string)
// Stream struct containing stream information
type Stream helix.Stream
// Params struct for required values
type Params struct {
ClientID string
OAuth string
}
// Client ...
type Client struct {
streamIDs []string
siMu sync.RWMutex
params *Params
hx *helix.Client
stopChan chan struct{}
forceCheckChan chan struct{}
interval time.Duration
onOnlineCallback onlineCallbackFunc
onOfflineCallback offlineCallbackFunc
}
// New creates a new Client
func New(params *Params) (*Client, error) {
if params == nil {
return nil, errors.New("missing option")
}
h, err := helix.NewClient(&helix.Options{
ClientID: params.ClientID,
UserAccessToken: params.OAuth,
RateLimitFunc: rateLimitCallback,
})
return &Client{
params: params,
hx: h,
stopChan: make(chan struct{}, 1),
forceCheckChan: make(chan struct{}, 1),
interval: time.Minute,
}, err
}
// AddStreamer ...
func (c *Client) AddStreamer(userID ...string) {
c.siMu.Lock()
for _, uid := range userID {
for _, si := range c.streamIDs {
if uid == si {
return
}
}
c.streamIDs = append(c.streamIDs, uid)
}
c.siMu.Unlock()
}
// SetInterval set how often to check if streams are online -
// when you set the interval after .Start() you need to .Stop() and .Start()
// again for it to take effect
func (c *Client) SetInterval(d time.Duration) {
c.interval = d
}
// CheckNow forces a online check without waiting for the next cycle
func (c *Client) CheckNow() {
c.forceCheckChan <- struct{}{}
}
// OnOnlineCallback add the func getting called when a stream goes on
func (c *Client) OnOnlineCallback(f func(Stream)) {
c.onOnlineCallback = f
}
// OnOfflineCallback add the func getting called when a stream goes off
func (c *Client) OnOfflineCallback(f func(string)) {
c.onOfflineCallback = f
}
// Stop ...
func (c *Client) Stop() {
c.stopChan <- struct{}{}
}
// Start ...
func (c *Client) Start() {
c.check()
tick := time.NewTicker(c.interval)
for {
select {
case <-tick.C:
c.check()
case <-c.stopChan:
tick.Stop()
return
case <-c.forceCheckChan:
c.check()
}
}
}
func (c *Client) check() {
c.siMu.RLock()
defer c.siMu.RUnlock()
resp, err := c.hx.GetStreams(&helix.StreamsParams{
First: 100,
Type: "live",
UserIDs: c.streamIDs,
})
if err != nil {
return
}
streams := make(map[string]helix.Stream, len(c.streamIDs))
for _, stream := range resp.Data.Streams {
streams[stream.UserID] = stream
}
for _, s := range c.streamIDs {
stream, ok := streams[s]
if ok && c.onOnlineCallback != nil {
c.onOnlineCallback(Stream(stream))
} else if !ok && c.onOfflineCallback != nil {
c.onOfflineCallback(s)
}
}
}
func rateLimitCallback(lastResponse *helix.Response) error {
if lastResponse.GetRateLimitRemaining() > 0 {
return nil
}
var reset64 int64
reset64 = int64(lastResponse.GetRateLimitReset())
currentTime := time.Now().Unix()
if currentTime < reset64 {
timeDiff := time.Duration(reset64 - currentTime)
if timeDiff > 0 {
fmt.Printf("Waiting on rate limit to pass before sending next request (%d seconds)\n", timeDiff)
time.Sleep(timeDiff * time.Second)
}
}
return nil
}