-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
230 lines (184 loc) · 5.16 KB
/
main.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package main
import (
"bufio"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"net/textproto"
"os"
"strconv"
"strings"
"sync"
"time"
)
const (
staleLogThresholdInSeconds = 60 // how old a log must be in seconds to be considered stale
spoilerDelayInSeconds = 15 // how long to delay sending logs to prevent spoilers (stream delay)
logRefreshTimeInSeconds = 10 // how long to wait between checking for log updates
twitchIRCRetryTimeInSeconds = 30 // how long to wait between failed connection attempts
twitchIRCHostPort = "irc.chat.twitch.tv:6667"
channelsFileName = "channels.json"
userNameEnvName = "LOGS_BOT_USERNAME"
oauthKeyEnvName = "LOGS_BOT_OAUTH_KEY"
)
type logResponse struct {
Date int64 `json:"date"`
ID int `json:"id"`
Title string `json:"title"`
}
type botConfig struct {
conn net.Conn
steamIDToTwitchChannel map[string]string
mutex *sync.Mutex
steamIDToLastTime map[string]time.Time
userName string
oauthKey string
}
func main() {
userName := os.Getenv(userNameEnvName)
oauthKey := os.Getenv(oauthKeyEnvName)
if userName == "" || oauthKey == "" {
log.Printf("Environment variables %v and %v not set.", userNameEnvName, oauthKeyEnvName)
os.Exit(1)
}
steamIDToTwitchChannel, err := loadChannelsFromFile()
if err != nil {
log.Printf("Failed to load channels from %v: %v\n", channelsFileName, err)
os.Exit(1)
}
b := &botConfig{
userName: userName,
oauthKey: oauthKey,
steamIDToTwitchChannel: steamIDToTwitchChannel,
mutex: &sync.Mutex{},
steamIDToLastTime: map[string]time.Time{},
}
for {
err := b.Serve()
log.Printf("Error serving: %v, retrying in %v seconds\n", err, twitchIRCRetryTimeInSeconds)
time.Sleep(twitchIRCRetryTimeInSeconds * time.Second)
}
}
func (b *botConfig) Serve() error {
log.Printf("Connecting to Twitch IRC server\n")
if err := b.connect(); err != nil {
return fmt.Errorf("Failed to connect to Twitch IRC server\n")
}
log.Printf("Connected!\n")
// spawn a worker processes that periodically checks for log updates and shuts down when
// the IRC server connection errors/drops
die := make(chan struct{})
go func(die chan struct{}) {
for {
select {
case <-die:
return
default:
for steamid, channel := range b.steamIDToTwitchChannel {
go b.checkLogsForPlayer(steamid, channel)
}
time.Sleep(logRefreshTimeInSeconds * time.Second)
}
}
}(die)
// read messages endlessly until an error occurs, then shut down worker process
err := b.readMessages()
die <- struct{}{}
return err
}
func (b *botConfig) connect() error {
conn, err := net.Dial("tcp", twitchIRCHostPort)
if err != nil {
return err
}
fmt.Fprintf(conn, "PASS %s\r\n", string(b.oauthKey))
fmt.Fprintf(conn, "NICK %s\r\n", b.userName)
b.conn = conn
return nil
}
func (b *botConfig) readMessages() error {
tp := textproto.NewReader(bufio.NewReader(b.conn))
for {
time.Sleep(1 * time.Second)
line, err := tp.ReadLine()
if err != nil {
return err
}
// respond to pings to keep the bot alive
if strings.Contains(line, "PING") {
fmt.Fprintf(b.conn, "PONG :tmi.twitch.tv\r\n")
}
}
}
func (b *botConfig) checkLogsForPlayer(steamid, channel string) error {
res, err := getNewestLogForPlayer(steamid)
if err != nil {
return err
}
id := strconv.Itoa(res.ID)
timestamp := time.Unix(res.Date, 0)
b.mutex.Lock()
defer b.mutex.Unlock()
lastTime, _ := b.steamIDToLastTime[steamid]
// if the log is stale or it hasn't been updated (its timestamp is the same as the last one we've seen),
// then do nothing
elapsed := time.Since(timestamp)
if elapsed.Seconds() > staleLogThresholdInSeconds || timestamp.Equal(lastTime) {
return nil
}
if err := b.sendLogToChannel(id, channel); err != nil {
return err
}
// save the last seen timestamp
b.steamIDToLastTime[steamid] = timestamp
return nil
}
func (b *botConfig) sendLogToChannel(logID, channel string) error {
// sleep to prevent spoilers due to stream delay
time.Sleep(spoilerDelayInSeconds * time.Second)
_, err := fmt.Fprintf(b.conn, "PRIVMSG #"+channel+" :http://logs.tf/"+logID+"\r\n")
if err != nil {
return err
}
log.Printf("Sent log id=%v channel=%v\n", logID, channel)
return nil
}
func getNewestLogForPlayer(steamid string) (*logResponse, error) {
res, err := http.Get("http://logs.tf/json_search?player=" + steamid + "&limit=1")
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
type queryResponse struct {
Logs []logResponse `json:"logs"`
Results int `json:"results"`
Success bool `json:"success"`
}
var q queryResponse
err = json.Unmarshal(body, &q)
if err != nil {
return nil, err
}
if q.Success == false || q.Results == 0 {
return nil, fmt.Errorf("Failed to get log for steamid=%v, response:\n%v\n", steamid, string(body))
}
return &(q.Logs[0]), nil
}
func loadChannelsFromFile() (map[string]string, error) {
var channels map[string]string
b, err := ioutil.ReadFile(channelsFileName)
if err != nil {
return nil, err
}
err = json.Unmarshal(b, &channels)
if err != nil {
return nil, err
}
return channels, nil
}