-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
191 lines (167 loc) · 4.59 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
package main
import (
"encoding/json"
"fmt"
"log"
"os"
"path"
"github.com/slack-go/slack"
)
func writeJson(name string, objs ...interface{}) {
res, err := json.MarshalIndent(objs, "", " ")
if err != nil {
log.Fatalln("failed to serialize json ", name, ":", err)
return
}
name += ".json"
err = os.WriteFile(name, res, 0600)
if err != nil {
log.Fatalln("failed to write data to ", name, ":", err)
return
}
}
func main() {
token := os.Getenv("SLACK_TOKEN")
if token == "" {
log.Fatalln("Envvar SLACK_TOKEN is not set")
}
api := slack.New(token, slack.OptionDebug(false))
fmt.Print("Fetching users... ")
users, err := api.GetUsers()
if err != nil {
log.Fatalln("failed:", err)
return
}
fmt.Println("got", len(users))
writeJson("users", users)
fmt.Print("Fetching conversations... ")
channels, _, err := api.GetConversations(&slack.GetConversationsParameters{Limit: 500, Types: []string{"public_channel", "private_channel", "mpim", "im"}})
if err != nil {
log.Fatalln("failed:", err)
return
}
fmt.Println("got", len(channels))
channelPathBase := "channels"
if err != nil {
log.Fatalln("failed to create channel folder:", err)
return
}
for _, channel := range channels {
channelName := channel.Name
if channelName == "" && channel.IsIM {
channelName = "im-" + channel.User
}
channelPath := channelPathBase + "/" + channelName + "-" + channel.ID
err = os.MkdirAll(channelPath, 0755)
writeJson(channelPath+"/info", channel)
fmt.Printf(" Fetching %s... ", channelName)
var messages []slack.Message
cursor := ""
for {
resp, err := api.GetConversationHistory(&slack.GetConversationHistoryParameters{ChannelID: channel.ID, Limit: 500, Cursor: cursor})
if err != nil {
log.Fatal(err)
return
}
messages = append(messages, resp.Messages...)
if resp.HasMore {
fmt.Printf("got %d [last ts %s].. ", len(messages), messages[len(messages)-1].Timestamp)
cursor = resp.ResponseMetaData.NextCursor
} else {
break
}
}
fmt.Println("got", len(messages))
if len(messages) > 0 {
writeJson(channelPath+"/messages", messages)
}
}
fmt.Print("Fetching files... ")
var files []slack.File
page_num := 0
for {
files_chunk, page, err := api.GetFiles(slack.GetFilesParameters{Count: 500, ShowHidden: true, Page: page_num})
if err != nil {
log.Fatal(err)
return
}
files = append(files, files_chunk...)
fmt.Printf("got %d [last ts %d].. ", len(files), files[len(files)-1].Timestamp.Time().Unix())
if page.Page != page.Pages {
page_num += 1
} else {
break
}
}
fmt.Println("got", len(files))
writeJson("files", files)
fmt.Println("Downloading files... ")
filePathBase := "files"
err = os.MkdirAll(filePathBase, 0755)
if err != nil {
log.Fatalln("failed to create files folder:", err)
return
}
for _, file_meta := range files {
fmt.Print(" ", file_meta.Name, " ... ")
filePath := filePathBase + "/" + file_meta.Name
// check for existing
file_stat, err := os.Stat(filePath)
if err == nil {
if file_stat.Size() == int64(file_meta.Size) {
fmt.Println(" same file exists (skipping)")
continue
} else {
fileName := file_meta.Name[0:len(file_meta.Name)-len(file_meta.Filetype)-1] + "-" + file_meta.ID + "." + file_meta.Filetype
filePath = filePathBase + "/" + fileName
fmt.Print(" -> ", fileName, " ... ")
file_stat, err = os.Stat(filePath)
if err == nil && file_stat.Size() == int64(file_meta.Size) {
fmt.Println(" same file exists (skipping)")
continue
}
}
}
file, err := os.Create(filePath)
if err != nil {
fmt.Println("failed to create file ", file_meta.Name, " :", err)
continue
}
err = api.GetFile(file_meta.URLPrivateDownload, file)
if err != nil {
fmt.Println("failed to download file ", file_meta.Name, " :", err)
continue
}
file.Close()
fmt.Println("done")
}
fmt.Println("Fetching emojis... ")
emojis, err := api.GetEmoji()
if err != nil {
log.Fatalln("failed to fetch emojis:", err)
return
}
fmt.Println("got", len(emojis))
fmt.Println("Downloading emojis... ")
emojiPathBase := "emojis"
err = os.MkdirAll(emojiPathBase, 0755)
if err != nil {
log.Fatalln("failed to create emoji folder:", err)
return
}
for emoji, url := range emojis {
fmt.Print(" ", emoji, " ... ")
filePath := emojiPathBase + "/" + emoji + path.Ext(url)
file, err := os.Create(filePath)
if err != nil {
fmt.Println("failed to create emoji file ", emoji, " :", err)
continue
}
err = api.GetFile(url, file)
if err != nil {
fmt.Println("failed to download emoji file ", emoji, " :", err)
continue
}
fmt.Println("done")
}
}