-
Notifications
You must be signed in to change notification settings - Fork 0
/
history.go
157 lines (131 loc) · 3.05 KB
/
history.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
package slat
import (
"bytes"
"encoding/json"
"io/ioutil"
"log"
"os"
"path"
"sort"
"strconv"
"time"
"github.com/nlopes/slack"
)
// ExportHistory updates the slat archive in outdir to include any new
// messages since the archive was last updated.
func ExportHistory(outdir, token string) error {
log.Printf("Catching up: %s", outdir)
api := slack.New(token)
channels, err := api.GetChannels(false)
if err != nil {
return err
}
for _, c := range channels {
err := exportChannelHistory(outdir, api, c)
if err != nil {
return err
}
}
return nil
}
func exportChannelHistory(outdir string, api *slack.Client, channel slack.Channel) error {
users, err := api.GetUsers()
if err != nil {
return err
}
usernames := make(map[string]string)
for _, user := range users {
usernames[user.ID] = user.Name
}
filename := path.Join(outdir, channel.Name+".json")
oldest := latestTimestamp(filename)
history, err := getChannelHistory(api, channel.ID, oldest, "")
if err != nil {
return err
}
msgs := make([]slack.Message, len(history.Messages))
copy(msgs, history.Messages)
for history.HasMore {
history, err = getChannelHistory(api, channel.ID, oldest, minTimestamp(msgs))
if err != nil {
return err
}
msgs = append(msgs, history.Messages...)
}
sort.Slice(msgs, func(i, j int) bool {
a, err := strconv.ParseFloat(msgs[i].Timestamp, 64)
if err != nil {
panic("a")
}
b, err := strconv.ParseFloat(msgs[j].Timestamp, 64)
if err != nil {
panic("b")
}
return a < b
})
log.Printf("Updating: %s: from %s: %d new", channel.Name, oldest, len(msgs))
if len(msgs) == 0 {
return nil
}
out, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_APPEND, os.ModePerm)
if err != nil {
return err
}
defer out.Close()
enc := json.NewEncoder(out)
for _, msg := range msgs {
if err := enc.Encode(msgEvent(usernames, msg)); err != nil {
return err
}
}
return nil
}
func msgEvent(usernames map[string]string, msg slack.Message) *event {
return &event{
Ts: msg.Timestamp,
Type: msg.Type,
Subtype: msg.SubType,
User: usernames[msg.User],
Text: cleanText(usernames, msg.Text),
}
}
func getChannelHistory(api *slack.Client, chanID string, oldest string, latest string) (*slack.History, error) {
params := slack.HistoryParameters{Oldest: oldest, Latest: latest, Count: 1000}
for {
history, err := api.GetChannelHistory(chanID, params)
if rate, ok := err.(*slack.RateLimitedError); ok {
time.Sleep(rate.RetryAfter)
continue
}
if err != nil {
return nil, err
}
return history, nil
}
}
func latestTimestamp(filename string) string {
buf, err := ioutil.ReadFile(filename)
if err != nil {
return ""
}
buf = bytes.TrimRight(buf, "\n")
index := bytes.LastIndexByte(buf, '\n')
if index < 0 {
index = 0
}
buf = buf[index:]
var e *event
if err = json.Unmarshal(buf, &e); err != nil {
return ""
}
return e.Ts
}
func minTimestamp(msgs []slack.Message) string {
min := msgs[0].Timestamp
for _, msg := range msgs {
if msg.Timestamp < min {
min = msg.Timestamp
}
}
return min
}