-
Notifications
You must be signed in to change notification settings - Fork 17
/
import.go
90 lines (77 loc) · 1.79 KB
/
import.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
package main
// Implements importing history from a Slack export from:
// https://my.slack.com/services/export
import (
"encoding/json"
"io/ioutil"
"log"
)
type Message struct {
Type string
User string
Text string
TS string
}
// Start an import from a slack export directory
// Does some basic error checking then imports the data in the background
func StartImport(dir *string, channel *string) (err error) {
log.Printf("Starting import from %s", *dir)
// Does this directory exist? Get its contents
contents, err := ioutil.ReadDir(*dir)
if err != nil {
return err
}
// Is there a users.json or channel.json file?
_, err = ioutil.ReadFile(*dir + "/users.json")
if err != nil {
_, err = ioutil.ReadFile(*dir + "/channel.json")
if err != nil {
return err
}
}
// Looks good, import each directory/channel in a goroutine
go func() {
for _, file := range contents {
if file.IsDir() {
if *channel == "" || *channel == file.Name() {
ImportDir(*dir + "/" + file.Name())
}
}
}
// Write the state file
err = markovChain.Save(stateFile)
if err != nil {
log.Fatal(err)
}
log.Printf("Import complete. %d suffixes", len(markovChain.Chain))
}()
return nil
}
// Handles the import of a channel/directory
func ImportDir(dir string) {
contents, err := ioutil.ReadDir(dir)
if err != nil {
log.Fatal(err)
}
for _, file := range contents {
if !file.IsDir() {
contents, err := ioutil.ReadFile(dir + "/" + file.Name())
if err != nil {
log.Fatal(err)
}
// parse into json
var messages []Message
err = json.Unmarshal(contents, &messages)
if err != nil {
log.Fatal(err)
}
for _, message := range messages {
text := message.Text
text = parseText(text)
if text != "" {
markovChain.Write(text)
}
}
}
}
}