Skip to content

Commit ea295a7

Browse files
committedMay 10, 2020
Add fetch-messages subcmd
1 parent dea4276 commit ea295a7

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed
 

‎subcmd/fetch_messages.go

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package subcmd
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
7+
"strconv"
8+
"time"
9+
10+
"github.com/vim-jp/slacklog-generator/internal/slacklog"
11+
)
12+
13+
func FetchMessages(args []string) error {
14+
slackToken := os.Getenv("SLACK_TOKEN")
15+
if slackToken == "" {
16+
return fmt.Errorf("$SLACK_TOKEN required")
17+
}
18+
19+
if len(args) < 2 {
20+
fmt.Println("Usage: go run scripts/main.go update-messages {data-dir} {yyyy-mm-dd}")
21+
return nil
22+
}
23+
24+
dataDir := filepath.Clean(args[0])
25+
26+
loc, err := time.LoadLocation("Asia/Tokyo")
27+
if err != nil {
28+
return err
29+
}
30+
31+
date, err := time.ParseInLocation("2006-01-02", args[1], loc)
32+
if err != nil {
33+
return err
34+
}
35+
dateEndTime := date.AddDate(0, 0, 1)
36+
37+
channelsJSONPath := filepath.Join(dataDir, "channels.json")
38+
channelTable, err := slacklog.NewChannelTable(channelsJSONPath, []string{"*"})
39+
if err != nil {
40+
return err
41+
}
42+
43+
for _, channel := range channelTable.Channels {
44+
channelDir := filepath.Join(dataDir, channel.ID)
45+
46+
extraParams := map[string]string{
47+
"channel": channel.ID,
48+
"oldest": strconv.FormatInt(date.Unix(), 10) + ".000000",
49+
"latest": strconv.FormatInt(dateEndTime.Unix(), 10) + ".000000",
50+
"limit": "200",
51+
}
52+
outFile := filepath.Join(channelDir, date.Format("2006-01-02")+".json")
53+
err := slacklog.DownloadEntitiesToFile(slackToken, "conversations.history", extraParams, "messages", true, outFile)
54+
if err != nil {
55+
return err
56+
}
57+
}
58+
59+
return nil
60+
}

‎subcmd/subcmd.go

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ func Run() error {
1313
convert-exported-logs
1414
download-emoji
1515
download-files
16+
fetch-messages
1617
generate-html
1718
update-channel-list
1819
update-user-list`)
@@ -28,6 +29,8 @@ func Run() error {
2829
return DownloadEmoji(args)
2930
case "download-files":
3031
return DownloadFiles(args)
32+
case "fetch-messages":
33+
return FetchMessages(args)
3134
case "generate-html":
3235
return GenerateHTML(args)
3336
case "update-channel-list":

0 commit comments

Comments
 (0)