This repository has been archived by the owner on Feb 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatters.go
229 lines (198 loc) · 5.65 KB
/
chatters.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
package main
import (
"database/sql"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"strconv"
"sync"
"time"
"github.com/go-redis/redis"
_ "github.com/lib/pq"
"github.com/op/go-logging"
)
var (
log = logging.MustGetLogger("chatters")
format = logging.MustStringFormatter(
`%{color}[%{time:2006-01-02 15:04:05.000}] [%{level:.4s}] %{color:reset}%{message}`,
)
rclient *redis.Client
)
type Stream struct {
Streamer string `json:"streamer"`
DataSourceName string `json:"dsn"`
db *sql.DB
Online bool
BasePoints int `json:"base_points"`
BasePointsSubbed int `json:"base_points_subbed"`
// OfflineChatPointRate specifies how fast offline chatters should gain points, if at all.
// 0.0 by default which means no points for offline chatters
OfflineChatPointRate float32 `json:"offline_chat_point_rate"`
}
type Config struct {
Streams []Stream `json:"streams"`
}
type ChattersList struct {
ChatterCount int `json:"chatter_count"`
Chatters map[string][]string `json:"chatters"`
}
func httpRequest(url string) ([]byte, error) {
response, err := http.Get(url)
if err != nil {
return nil, err
}
defer response.Body.Close()
contents, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Error(err)
return nil, err
}
return contents, nil
}
func handleUsers(sql_tx *sql.Tx, redis_tx redis.Pipeliner, stream Stream, chatters *ChattersList) error {
_, err := sql_tx.Exec("CREATE TEMPORARY TABLE chatters(username TEXT PRIMARY KEY NOT NULL) ON COMMIT DROP")
if err != nil {
return err
}
for _, chatterCategory := range chatters.Chatters {
for _, username := range chatterCategory {
_, err := sql_tx.Exec("INSERT INTO chatters(username) VALUES ($1)", username)
if err != nil {
return err
}
}
}
// The script is currently set to run every 10 minutes
update_interval := 10
stream_online := stream.Online
base_points := 2
base_sub_points := 10
offline_point_rate := stream.OfflineChatPointRate
if stream.BasePoints > 0 {
base_points = stream.BasePoints
}
if stream.BasePointsSubbed > 0 {
base_sub_points = stream.BasePointsSubbed
}
_, err = sql_tx.Exec(`
INSERT INTO "user"(username, username_raw, level, points, subscriber, minutes_in_chat_online, minutes_in_chat_offline)
(SELECT chatters.username AS username,
chatters.username AS username_raw,
100 AS level,
$3 AS points,
FALSE AS subscriber,
CASE WHEN $2 THEN $1 ELSE 0 END AS minutes_in_chat_online,
CASE WHEN NOT $2 THEN $1 ELSE 0 END AS minutes_in_chat_offline
FROM chatters)
ON CONFLICT (username) DO UPDATE SET
points = "user".points + round(
CASE WHEN "user".subscriber THEN $4 ELSE $3 END *
CASE WHEN $2 THEN 1.0 ELSE $5 END
),
minutes_in_chat_online = "user".minutes_in_chat_online + CASE WHEN $2 THEN $1 ELSE 0 END,
minutes_in_chat_offline = "user".minutes_in_chat_offline + CASE WHEN NOT $2 THEN $1 ELSE 0 END
`, update_interval, stream_online, base_points, base_sub_points, offline_point_rate)
if err != nil {
return err
}
now_formatted := strconv.FormatInt(time.Now().Unix(), 10)
last_seen_key := fmt.Sprintf("%s:users:last_seen", stream.Streamer)
multiset_args := make(map[string]interface{})
for _, chatterCategory := range chatters.Chatters {
for _, username := range chatterCategory {
multiset_args[username] = now_formatted
}
}
redis_tx.HMSet(last_seen_key, multiset_args)
return nil
}
func handleStream(stream Stream) error {
log.Debugf("Loading chatters for %s", stream.Streamer)
// Initialize DB Connection for this stream
db, err := sql.Open("postgres", stream.DataSourceName)
if err != nil {
return err
}
stream.db = db
// Check online status for streamer
res, err := rclient.HGet("stream_data", fmt.Sprintf("%s:online", stream.Streamer)).Result()
if err == redis.Nil {
stream.Online = false
} else if err != nil {
return err
} else {
stream.Online = res == "True"
}
// Load chatters JSON data
url := fmt.Sprintf("https://tmi.twitch.tv/group/user/%s/chatters", stream.Streamer)
resp, err := httpRequest(url)
if err != nil {
return err
}
var chatters ChattersList
err = json.Unmarshal(resp, &chatters)
if err != nil {
return err
}
// Initialize database transaction
err = WithTransaction(stream.db, func(sql_tx *sql.Tx) error {
// Initialize redis MULTI pipeline
_, err = rclient.TxPipelined(func(pipe redis.Pipeliner) error {
err := handleUsers(sql_tx, pipe, stream, &chatters)
if err != nil {
return err
}
return nil
})
if err != nil {
return err
}
return nil
})
if err != nil {
return err
}
log.Debugf("Updated data for %d chatters for streamer %s", chatters.ChatterCount, stream.Streamer)
return nil
}
func main() {
// Initialize logging
backend := logging.NewLogBackend(os.Stdout, "", 0)
backendFormatter := logging.NewBackendFormatter(backend, format)
logging.SetBackend(backendFormatter)
log.Debug("Starting chatters update")
// Connect to redis
rclient = redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "",
DB: 0,
})
// Read config file
file, err := ioutil.ReadFile("config.json")
if err != nil {
log.Fatal(err)
}
var config Config
err = json.Unmarshal(file, &config)
if err != nil {
log.Fatal(err)
}
var wg sync.WaitGroup
wg.Add(len(config.Streams))
exitCode := 0
for _, stream := range config.Streams {
go func(stream Stream) {
defer wg.Done()
err := handleStream(stream)
if err != nil {
log.Errorf("Error fetching stream data for %s: %s", stream.Streamer, err)
exitCode = 1
}
}(stream)
}
wg.Wait()
log.Debug("Done updating chatters")
os.Exit(exitCode)
}