-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
239 lines (204 loc) · 5.93 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
231
232
233
234
235
236
237
238
239
package main
import (
"database/sql"
"encoding/json"
"flag"
"fmt"
"log"
"os"
"path/filepath"
"strconv"
"strings"
"time"
_ "github.com/mattn/go-sqlite3"
)
type Message struct {
MessageID string
SenderID string
Content string
Timestamp string
EditTimestamp string
ChannelID string
}
type JsonMessage struct {
ID string `json:"id"`
Type string `json:"type"`
Timestamp string `json:"timestamp"`
TimestampEdited string `json:"timestampEdited"`
IsPinned bool `json:"isPinned"`
Content string `json:"content"`
Author Author `json:"author"`
Attachments []interface{} `json:"attachments"`
Embeds []interface{} `json:"embeds"`
Stickers []interface{} `json:"stickers"`
Reactions []interface{} `json:"reactions"`
Mentions []interface{} `json:"mentions"`
}
type Author struct {
ID string `json:"id"`
Name interface{} `json:"name"`
Discriminator interface{} `json:"discriminator"`
Nickname interface{} `json:"nickname"`
Color interface{} `json:"color"`
IsBot bool `json:"isBot"`
AvatarURL interface{} `json:"avatarUrl"`
}
type Guild struct {
ID string `json:"id"`
Name string `json:"name"`
IconUrl string `json:"iconUrl"`
}
type Channel struct {
ID string `json:"id"`
Type string `json:"type"`
CategoryID string `json:"categoryId"`
Category string `json:"category"`
Name string `json:"name"`
Topic interface{} `json:"topic"`
}
type User struct {
ID string `json:"id"`
Name string `json:"name"`
AvatarURL string `json:"avatarUrl"`
Discriminator string `json:"discriminator"`
}
type DateRange struct {
After interface{} `json:"after"`
Before interface{} `json:"before"`
}
type JsonOutput struct {
Guild Guild `json:"guild"`
Channel Channel `json:"channel"`
DateRange DateRange `json:"dateRange"`
Messages []JsonMessage `json:"messages"`
MessageCount int `json:"messageCount"`
}
var (
versionFlag = flag.Bool("v", false, "prints the version")
dbPath = flag.String("in", "input.db", "path to the input SQLite database")
jsonPath = flag.String("out", "output.json", "path to the output JSON file")
useCidAsName = flag.Bool("id-as-name", false, "If set to true then the output json file will be the channel id. Overrides jsonPath.")
channelID = flag.String("channel", "", "Comma separated list of one or more channel id")
)
var channelName string
var user User
const version = "0.2.4"
func main() {
flag.Parse()
if *versionFlag {
fmt.Println(version)
return
}
if *channelID == "" {
log.Fatal("Channel ID is required")
}
channelIDS := strings.Split(*channelID, ",")
fmt.Println("Opening database...")
db, err := sql.Open("sqlite3", *dbPath)
if err != nil {
log.Fatal(err)
}
defer db.Close()
for i, id := range channelIDS {
if *useCidAsName == true {
*jsonPath = id + ".json"
} else {
ext := filepath.Ext(*jsonPath)
name := strings.TrimSuffix(*jsonPath, ext)
*jsonPath = name + strconv.Itoa(i) + ext
}
fmt.Println("Getting messages from database...")
rows, err := db.Query(`
SELECT m.message_id, m.sender_id, m.channel_id, m.text, m.timestamp, COALESCE(e.edit_timestamp, ''), c.name
FROM messages m
LEFT JOIN edit_timestamps e ON m.message_id = e.message_id
LEFT JOIN channels c ON m.channel_id = c.id
WHERE m.channel_id = ?
`, *channelID)
if err != nil {
log.Fatal(err)
}
defer rows.Close()
messages := make([]Message, 0)
for rows.Next() {
var msg Message
if err := rows.Scan(&msg.MessageID, &msg.SenderID, &msg.ChannelID, &msg.Content, &msg.Timestamp, &msg.EditTimestamp, &channelName); err != nil {
log.Fatal(err)
}
messages = append(messages, msg)
}
jsonMessages := make([]JsonMessage, 0)
for _, msg := range messages {
timestampMillis, err := strconv.ParseInt(msg.Timestamp, 10, 64)
if err != nil {
log.Fatal(err)
}
timestamp := time.Unix(0, timestampMillis*int64(time.Millisecond))
formattedTimestamp := timestamp.Format("2006-01-02T15:04:05.999-07:00")
err = db.QueryRow("SELECT id, name, avatar_url, discriminator FROM users WHERE id = ?", msg.SenderID).Scan(&user.ID, &user.Name, &user.AvatarURL, &user.Discriminator)
if err != nil {
log.Fatal(err)
}
jsonMsg := JsonMessage{
ID: msg.MessageID,
Type: "Default",
Timestamp: formattedTimestamp,
TimestampEdited: formattedTimestamp,
IsPinned: false,
Content: msg.Content,
Author: Author{
ID: msg.SenderID,
Name: user.Name,
Discriminator: user.Discriminator,
Nickname: user.Name,
Color: nil,
IsBot: false,
AvatarURL: user.AvatarURL,
},
Attachments: []interface{}{},
Embeds: []interface{}{},
Stickers: []interface{}{},
Reactions: []interface{}{},
Mentions: []interface{}{},
}
jsonMessages = append(jsonMessages, jsonMsg)
}
jsonOutput := JsonOutput{
Guild: Guild{
ID: "0",
Name: "Direct Messages",
IconUrl: "null",
},
Channel: Channel{
ID: *channelID,
Type: "DirectTextChat",
CategoryID: "0",
Category: "Private",
Name: channelName,
Topic: nil,
},
DateRange: DateRange{
After: nil,
Before: nil,
},
Messages: jsonMessages,
MessageCount: len(jsonMessages),
}
fmt.Println("Formatting JSON...")
jsonOutputBytes, err := json.MarshalIndent(jsonOutput, "", " ")
if err != nil {
log.Fatal(err)
}
fmt.Println("Writing to file...")
file, err := os.Create(*jsonPath)
if err != nil {
log.Fatal(err)
}
defer file.Close()
_, err = file.Write(jsonOutputBytes)
if err != nil {
log.Fatal(err)
}
fmt.Println("Done with channel: " + id)
}
}