-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
179 lines (153 loc) · 4.62 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
package main
import (
"database/sql"
"encoding/json"
"fmt"
"github.com/fatih/color"
"github.com/gobwas/ws"
"github.com/gobwas/ws/wsutil"
"log"
"math/rand"
_ "modernc.org/sqlite"
"net/http"
"strings"
"time"
)
const batchSize = 10
const webSocketUrl string = "wss://ws-us2.pusher.com/app/eb1d5f283081a78b932c?protocol=7&client=js&version=7.6.0&flash=false"
const chatroomSubcribeCommand string = "{\"event\":\"pusher:subscribe\",\"data\":{\"auth\":\"\",\"channel\":\"chatrooms.%d.v2\"}}"
func main() {
db, err := sql.Open("sqlite", "file:sqlite-data/database.db?cache=shared")
if err != nil {
log.Fatal(err)
}
defer db.Close()
if err = CreateTable(db); err != nil {
return
}
dataChannel := make(chan Data, 100)
go HandleDataChannelInserts(db, dataChannel)
for channelName, chatRoomId := range ChannelAndChatIdMap {
go startListeningChat(channelName, chatRoomId, dataChannel)
}
// Block main goroutine
select {}
}
func CreateTable(db *sql.DB) error {
sqlStmt := `
CREATE TABLE IF NOT EXISTS messages (
id INTEGER PRIMARY KEY AUTOINCREMENT,
content TEXT NOT NULL,
username TEXT NOT NULL,
channel TEXT NOT NULL,
date TEXT NOT NULL
);
`
_, err := db.Exec(sqlStmt)
if err != nil {
log.Printf("%q: %s\n", err, sqlStmt)
}
return err
}
func WriteToDb(db *sql.DB, dataList []Data) {
defer timer("WriteToDb")()
if len(dataList) == 0 {
return
}
valueStrings := make([]string, 0, len(dataList))
valueArgs := make([]interface{}, 0, len(dataList)*4) // Each data has 4 fields
// Construct the query string with placeholders for each data
for _, data := range dataList {
valueStrings = append(valueStrings, "(?, ?, ?, ?)")
valueArgs = append(valueArgs, data.Content, data.Sender.Username, ChatIdAndChannelMap[data.ChatroomID], data.CreatedAt)
}
stmt := fmt.Sprintf("INSERT INTO messages (content, username, channel, date) VALUES %s",
strings.Join(valueStrings, ", "))
// Execute the query with all parameters
_, err := db.Exec(stmt, valueArgs...)
if err != nil {
log.Print("Failed to insert multiple rows:", err)
}
}
func HandleDataChannelInserts(db *sql.DB, dataChannel <-chan Data) {
dataBatch := make([]Data, 0, batchSize)
start := time.Now()
for data := range dataChannel {
dataBatch = append(dataBatch, data)
if len(dataBatch) >= batchSize {
color.White("-> Batch filled at %v\n", time.Since(start))
start = time.Now()
WriteToDb(db, dataBatch)
dataBatch = make([]Data, 0, batchSize)
}
}
}
func startListeningChat(streamerName string, chatRoomId int, dataChannel chan<- Data) {
req, err := http.NewRequest(http.MethodGet, webSocketUrl, nil)
if err != nil {
log.Fatalf("error creating request: %v", err)
}
// Connect to the server.
conn, _, _, err := ws.Dialer{}.Dial(req.Context(), webSocketUrl)
if err != nil {
log.Fatalf("error connecting to websocket: %v", err)
}
defer conn.Close()
// Send a message to the WebSocket server to subscribe to chatroom.
message := []byte(fmt.Sprintf(chatroomSubcribeCommand, chatRoomId))
err = wsutil.WriteClientMessage(conn, ws.OpText, message)
if err != nil {
log.Fatalf("error sending message: %v", err)
}
c := GetRandomColorForLog()
for {
msg, _, err := wsutil.ReadServerData(conn)
if err != nil {
log.Printf("error reading message: %v", err)
time.Sleep(1 * time.Minute)
go startListeningChat(streamerName, chatRoomId, dataChannel)
return
}
go UnmarshallAndSendToChannel(streamerName, msg, dataChannel, c)
}
}
func UnmarshallAndSendToChannel(streamerName string, msgByte []byte, dataChannel chan<- Data, c *color.Color) {
var event Message
if err := json.Unmarshal(msgByte, &event); err != nil {
fmt.Println("Error unmarshaling event:", err)
return
}
var data Data
if err := json.Unmarshal([]byte(event.Data), &data); err != nil {
fmt.Println("Error unmarshaling data:", err)
return
}
if data.Type != "message" {
return
}
dataChannel <- data
c.Printf("%s:%s:%s \n", streamerName, data.Sender.Username, data.Content)
}
func GetRandomColorForLog() *color.Color {
colors := []*color.Color{
color.New(color.FgRed),
color.New(color.FgGreen),
color.New(color.FgYellow),
color.New(color.FgCyan),
color.New(color.FgBlue),
color.New(color.FgMagenta),
}
return colors[rand.Intn(len(colors))]
}
// timer returns a function that prints the name argument and
// the elapsed time between the call to timer and the call to
// the returned function. The returned function is intended to
// be used in a defer statement:
//
// defer timer("sum")()
func timer(name string) func() {
start := time.Now()
return func() {
color.White("-> %s took %v\n", name, time.Since(start))
}
}