-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
210 lines (190 loc) · 5.88 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"time"
"github.com/mattn/go-mastodon"
"github.com/microcosm-cc/bluemonday"
)
var configPtr = flag.String("config", "config.json", "path to config.json")
var allStatusesPtr = flag.String("statuses", "statuses.json", "path to json for statuses")
var dummyConfigPtr = flag.Bool("dummy", false, "create a dummy config, can be used together with config flag")
// Config includes all parameters to connect to a Mastodon instance
type Config struct {
Server string
ClientID string
ClientSecret string
MastodonAccount string
MastodonPasswort string
}
// Toot represents a Mastodon status with only the essential parameters
type Toot struct {
ID mastodon.ID `json:"id"`
CreatedAt time.Time `json:"created_at"`
Content string `json:"content"`
URL string `json:"url"`
}
func readConfig(path string) (config *Config) {
jsonFile, err := os.Open(path)
if err != nil {
log.Fatal(err)
}
defer jsonFile.Close()
byteConfig, _ := ioutil.ReadAll(jsonFile)
json.Unmarshal(byteConfig, &config)
return config
}
func createDummyConfig(configPath string) {
c := &Config{Server: "https://social.tchncs.de", ClientID: "132456", ClientSecret: "s0s3cr3t", MastodonAccount: "[email protected]", MastodonPasswort: "t0pS3cr3t"}
jsonConfig, err := json.MarshalIndent(c, "", " ")
if err != nil {
log.Println("createDummyConfig json marshal error")
log.Fatal(err)
}
err = ioutil.WriteFile(configPath, jsonConfig, 0644)
if err != nil {
log.Println("createDummyConfig write error")
log.Fatal(err)
}
}
func handleFlags() {
flag.Parse()
// log.Println("config:", *configPtr)
// log.Println("followers:", *followersPtr)
// log.Println("dummy config:", *dummyConfigPtr)
if *dummyConfigPtr {
createDummyConfig(*configPtr)
os.Exit(0)
}
}
func saveToots(toots []Toot, path string) {
jsonToots, err := json.MarshalIndent(toots, "", " ")
if err != nil {
log.Println("saveToots json marshal error")
log.Fatal(err)
}
//log.Println(string(jsonFollowers))
err = ioutil.WriteFile(path, jsonToots, 0644)
if err != nil {
log.Println("saveToots write error")
log.Fatal(err)
}
}
func loadToots(path string) (toots []Toot) {
jsonFile, err := os.Open(path)
if err != nil {
fmt.Println(err)
}
defer jsonFile.Close()
byteToots, _ := ioutil.ReadAll(jsonFile)
json.Unmarshal(byteToots, &toots)
return toots
}
func convertMstdnStatusToToot(s []*mastodon.Status, bluemondayPolicy *bluemonday.Policy) []Toot {
var toots []Toot
for i := range s {
content := bluemondayPolicy.Sanitize(s[i].Content)
tootURL := s[i].URL
if s[i].Reblog != nil {
content = "RT @" + s[i].Reblog.Account.Username + ": " + content
tootURL = strings.TrimSuffix(tootURL, "/activity")
}
t := Toot{s[i].ID, s[i].CreatedAt, content, tootURL}
toots = append(toots, t)
}
return toots
}
func printStatuses(t []Toot) {
for i := range t {
log.Printf("%v\t%v\t%v\t%v", t[i].ID, t[i].CreatedAt.Local(), t[i].Content, t[i].URL)
}
}
func isTootExportedAlready(historicToots []Toot, t Toot) bool {
for i := range historicToots {
if historicToots[i].ID == t.ID {
// log.Println("historicToots[i].ID == t.ID =", t.ID)
return true
}
}
return false
}
func mergeHistoricAndNewStatuses(historicToots []Toot, newStatuses []Toot) []Toot {
var allStatuses []Toot
firstDuplicate := len(newStatuses)
for i := range newStatuses {
if isTootExportedAlready(historicToots, newStatuses[i]) {
firstDuplicate = i
// log.Println("found firstDuplicate")
break
}
}
if firstDuplicate == len(newStatuses) {
allStatuses = append(allStatuses, newStatuses...)
log.Println("firstDuplicate not found:", firstDuplicate, "content:", newStatuses[len(newStatuses)-1].Content)
} else {
allStatuses = append(allStatuses, newStatuses[:firstDuplicate]...)
log.Println("firstDuplicate:", firstDuplicate, "content:", newStatuses[firstDuplicate].Content)
}
allStatuses = append(allStatuses, historicToots...)
return allStatuses
}
func main() {
// init, load config, login to mastodon
log.SetFlags(log.LstdFlags | log.Lshortfile)
startTime := time.Now()
handleFlags()
config := readConfig(*configPtr)
// https://github.com/microcosm-cc/bluemonday#usage
// Do this once for each unique policy, and use the policy for the life of the program
// Policy creation/editing is not safe to use in multiple goroutines
bluemondayPolicy := bluemonday.StrictPolicy()
// load saved toots
historicToots := loadToots(*allStatusesPtr)
c := mastodon.NewClient(&mastodon.Config{
Server: config.Server,
ClientID: config.ClientID,
ClientSecret: config.ClientSecret,
})
err := c.Authenticate(context.Background(), config.MastodonAccount, config.MastodonPasswort)
if err != nil {
log.Println("auth error")
log.Fatal(err)
}
// get my user account
account, err := c.GetAccountCurrentUser(context.Background())
if err != nil {
log.Fatal(err)
}
// load all statuses
var pg mastodon.Pagination
var newStatuses []Toot
for {
// log.Println("Getting toots with pg.MaxID:", pg.MaxID)
statuses, err := c.GetAccountStatuses(context.Background(), account.ID, &pg)
if err != nil {
log.Fatal(err)
}
// log.Println("Number of new toots from this page:", len(statuses))
toots := convertMstdnStatusToToot(statuses, bluemondayPolicy)
newStatuses = append(newStatuses, toots...)
isKnown := isTootExportedAlready(historicToots, newStatuses[len(newStatuses)-1])
if pg.MaxID == "" || len(statuses) == 0 || isKnown {
break
}
pg.SinceID = ""
pg.MinID = ""
}
allStatuses := mergeHistoricAndNewStatuses(historicToots, newStatuses)
// printStatuses(allStatuses)
saveToots(allStatuses, *allStatusesPtr)
log.Println("historicToots:", len(historicToots))
log.Println("newStatuses", len(newStatuses))
log.Println("allStatuses", len(allStatuses))
log.Println("duration:", time.Now().Sub(startTime))
}