/
slackhist.go
309 lines (243 loc) · 6.94 KB
/
slackhist.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package main
import (
"archive/zip"
"encoding/json"
"fmt"
"github.com/codegangsta/cli"
"github.com/tealeg/xlsx"
"os"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
"time"
)
type message struct {
User, Type, Subtype, Text, Ts string
Timestamp time.Time
}
// Implement the sort interface for type messages
type messages []message
func (m messages) Len() int {
return len(m)
}
func (m messages) Less(i, j int) bool {
return m[i].Timestamp.Before(m[j].Timestamp)
}
func (m messages) Swap(i, j int) {
m[i], m[j] = m[j], m[i]
}
type meta struct {
ID, Name string
RealName string `json:"real_name,omitempty"`
}
// Set global CLI vars
var filename, outputDir, timezone string
var metadata = make(map[string][]meta)
func main() {
app := cli.NewApp()
app.Usage = "A command-line utility for exporting Slack history to Excel (.xlsx)"
app.HideVersion = true
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "name, n",
Value: time.Now().Format("2006-Jan-02") + "_SlackExport.xlsx",
Usage: "Set the name of the exported spreadsheet",
Destination: &filename,
},
cli.StringFlag{
Name: "destination, d",
Value: "./",
Usage: "Specify the output directory for\n\t the exported xlsx workbook",
Destination: &outputDir,
},
cli.StringFlag{
Name: "timezone, t",
Value: "Local",
Usage: "Specify an alternate timezone.\n\t See here for available options https://goo.gl/Tmq0oR",
Destination: &timezone,
},
}
app.Action = func(c *cli.Context) {
if len(c.Args()) != 1 {
fmt.Printf("\n*** Error: The location of the target zip file must be the last and only argument!\n***\n" +
"*** For additional help, enter \"slackhist help\"\n\n")
os.Exit(1)
}
if suffix := strings.HasSuffix(filename, ".xlsx"); suffix == false {
filename = filename + ".xlsx"
}
messages := processData(c.Args()[0], metadata)
createWorkbook(messages)
}
app.Run(os.Args)
}
func processData(f string, md map[string][]meta) map[string]messages {
payload := make(map[string]messages)
var dirname string
// Open a readable stream from the zip file
r, err := zip.OpenReader(f)
if err != nil {
panic(err)
}
defer r.Close()
// Collect the required meta to process the information HACK
for _, file := range r.File {
switch file.FileHeader.Name {
case "channels.json", "users.json":
getMeta(file, md)
}
}
// For every item in the entire zip folder (recursive)
for _, file := range r.File {
isDirectory := file.FileInfo().IsDir()
// Switch on file = directory
switch isDirectory {
case true:
// Retrieve the current directory's base name
path, err := filepath.Abs(file.FileHeader.Name)
if err != nil {
panic(err)
}
dirname = filepath.Base(path)
// Make the directory's base name (thus, the channel name) the key
// and instantiate an empty slice within payload for the messages to
// be stored
payload[dirname] = make([]message, 0)
case false:
var thisJSON []message
// If the file is one that we've already processed, break
// from this particular loop cycle
switch file.FileHeader.Name {
case "integration_logs.json", "channels.json", "users.json":
continue
}
// Grab the parent directory's name
dirname = filepath.Base(filepath.Dir(file.FileHeader.Name))
// Open a readable stream from the current file
rc, err := file.Open()
if err != nil {
panic(err)
}
defer rc.Close()
// Create JSON decoder and decode the readable stream to "thisJSON"
decoder := json.NewDecoder(rc)
for decoder.More() {
if err := decoder.Decode(&thisJSON); err != nil {
panic(err)
}
}
// After a full file decode, iterate through the decoded JSON for
// messages or attachments, and parse the timezone according to the
// current user's local time zone format
for _, value := range thisJSON {
if value.Type == "message" && value.Subtype == "" || value.Subtype == "file_share" {
value.Timestamp = parseTimestamp(value.Ts)
_, value.User = parseUser(value.User, md)
re := regexp.MustCompile(`(<@[a-zA-Z0-9]{9}(\|[a-zA-Z0-9._]+)?>)`)
if matches := re.FindAllString(value.Text, -1); matches != nil {
value.Text = re.ReplaceAllStringFunc(value.Text, func(match string) string {
uid := match[2 : len(match)-1]
if value.Subtype == "file_share" {
uid = strings.Split(uid, "|")[0]
}
username, _ := parseUser(uid, md)
return "@" + username
})
}
payload[dirname] = append(payload[dirname], value)
}
}
}
}
return payload
}
func createWorkbook(m map[string]messages) {
var channelNames sort.StringSlice
workbook := xlsx.NewFile()
fullPath := filepath.Clean(filepath.Join(outputDir, filename))
// First, sort the map by channel name
for channel := range m {
channelNames = append(channelNames, channel)
}
channelNames.Sort()
for _, channel := range channelNames {
sheet, err := workbook.AddSheet(channel)
if err != nil {
panic(err)
}
sort.Sort(sort.Reverse(m[channel]))
headingRow := sheet.AddRow()
headingRow.AddCell().SetString("Timestamp")
headingRow.AddCell().SetString("User")
headingRow.AddCell().SetString("Message")
if err := sheet.SetColWidth(0, 1, 20); err != nil {
panic(err)
}
if err := sheet.SetColWidth(2, 2, 200); err != nil {
panic(err)
}
for _, message := range m[channel] {
messageRow := sheet.AddRow()
messageRow.AddCell().SetString(message.Timestamp.Format("Jan 02, 2006 | 15:04"))
messageRow.AddCell().SetString(message.User)
messageRow.AddCell().SetString(message.Text)
}
}
if err := os.MkdirAll(filepath.Dir(fullPath), os.ModePerm); err != nil {
panic(err)
}
if err := workbook.Save(fullPath); err != nil {
panic(err)
}
}
/**************************************************************************
* Utility functions *
**************************************************************************/
func getMeta(f *zip.File, m map[string][]meta) {
filename := f.FileHeader.Name
var thisJSON []meta
rc, err := f.Open()
if err != nil {
panic(err)
}
defer rc.Close()
decoder := json.NewDecoder(rc)
for decoder.More() {
if err := decoder.Decode(&thisJSON); err != nil {
panic(err)
}
}
switch filename {
case "users.json":
m["users"] = thisJSON
case "channels.json":
m["channels"] = thisJSON
default:
panic(filename)
}
}
func parseTimestamp(ts string) time.Time {
timestring, err := strconv.ParseInt(strings.Split(ts, ".")[0], 10, 64)
if err != nil {
panic(err)
}
tm := time.Unix(timestring, 0)
loc, err := time.LoadLocation(timezone)
if err != nil {
panic(err)
}
return tm.In(loc)
}
func parseUser(uid string, m map[string][]meta) (string, string) {
var username, realname string
for _, value := range m["users"] {
if value.ID == uid {
username = value.Name
realname = value.RealName
break
}
}
return username, realname
}