forked from muesli/mastotool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats.go
424 lines (366 loc) · 9.83 KB
/
stats.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
package main
import (
"context"
"fmt"
"html"
"math"
"os"
"sort"
"strings"
"time"
"github.com/jedib0t/go-pretty/v6/table"
mastodon "github.com/mattn/go-mastodon"
"github.com/microcosm-cc/bluemonday"
"github.com/muesli/goprogressbar"
"github.com/spf13/cobra"
)
var (
topN int
maxToots int
columns int
statsCmd = &cobra.Command{
Use: "stats",
Short: "generates statistics about your account",
RunE: func(cmd *cobra.Command, args []string) error {
return gatherStats()
},
}
stripper = bluemonday.StrictPolicy()
)
const (
SortByLikes = iota
SortByBoosts
SortByScore
SortByReplies
)
type tootStat struct {
Likes int64
Boosts int64
Replies int64
}
type stats struct {
DaysActive int
Followers int64
Following int64
Toots map[string]*tootStat
Tags map[string]*tootStat
Replies map[string]*tootStat
Mentions map[string]int64
Boosts map[string]int64
Responses map[string]int64
}
func gatherStats() error {
stats := &stats{
DaysActive: int(time.Since(self.CreatedAt).Hours() / 24),
Followers: self.FollowersCount,
Following: self.FollowingCount,
Toots: make(map[string]*tootStat),
Tags: make(map[string]*tootStat),
Replies: make(map[string]*tootStat),
Mentions: make(map[string]int64),
Boosts: make(map[string]int64),
Responses: make(map[string]int64),
}
pb := &goprogressbar.ProgressBar{
Text: fmt.Sprintf("Loading toots for %s", self.Username),
Total: self.StatusesCount,
PrependTextFunc: func(p *goprogressbar.ProgressBar) string {
return fmt.Sprintf("%d of %d", p.Current, int64(math.Max(float64(p.Current), float64(self.StatusesCount))))
},
Current: 0,
Width: 40,
}
var pg mastodon.Pagination
for {
pg.SinceID = ""
pg.MinID = ""
pg.Limit = 40
statuses, err := client.GetAccountStatuses(context.Background(), self.ID, &pg)
if err != nil {
return fmt.Errorf("Can't retrieve statuses: %s", err)
}
abort := false
for _, s := range statuses {
err = parseToot(s, stats)
if err != nil {
return fmt.Errorf("Can't parse toot: %s", err)
}
pb.Current += 1
pb.LazyPrint()
if maxToots > 0 && len(stats.Toots) >= maxToots {
abort = true
break
}
}
// For some reason, either because it's Pleroma or because I have too few toots,
// `pg.MaxID` never equals `""` and we get stuck looping forever. Add a simple
// break condition on "no statuses fetched" to avoid the issue.
if abort || pg.MaxID == "" || len(statuses) == 0 {
break
}
}
// print out stats we gathered
fmt.Printf("\n\n")
printAccountStats(stats)
printInteractionStats(stats)
printTootStats(stats)
printTagStats(stats)
return nil
}
func cleanupContent(content string) string {
// clean up toot for terminal output
content = strings.Replace(content, "<br>", "\n", -1)
content = strings.Replace(content, "<p>", "\n", -1)
content = strings.Replace(content, "</p>", "", -1)
content = html.UnescapeString(stripper.Sanitize(content))
content = strings.TrimSpace(strings.Replace(content, "\n", " ", -1))
return content
}
func parseToot(status *mastodon.Status, stats *stats) error {
// handle mentions
for _, m := range status.Mentions {
stats.Mentions[m.Acct]++
}
// handle boosts
if status.Reblog != nil {
stats.Boosts[status.Reblog.Account.Acct]++
return nil
}
var replies int64
// parse tags
if status.InReplyToID == nil {
contexts, err := client.GetStatusContext(context.Background(), status.ID)
if err != nil {
return err
}
// handle replies for this status
for _, d := range contexts.Descendants {
if d.Account.ID == self.ID {
continue
}
replies++
stats.Responses[d.Account.Acct]++
}
for _, t := range status.Tags {
tag := strings.ToLower(t.Name)
stat, ok := stats.Tags[tag]
if ok {
stat.Likes += status.FavouritesCount
stat.Boosts += status.ReblogsCount
stat.Replies += replies
} else {
stat = &tootStat{
Likes: status.FavouritesCount,
Boosts: status.ReblogsCount,
Replies: replies,
}
stats.Tags[tag] = stat
}
}
}
content := cleanupContent(status.Content)
// handle replies
if status.InReplyToID != nil {
stats.Replies[content] = &tootStat{
Likes: status.FavouritesCount,
Boosts: status.ReblogsCount,
Replies: replies,
}
} else {
stats.Toots[content] = &tootStat{
Likes: status.FavouritesCount,
Boosts: status.ReblogsCount,
Replies: replies,
}
}
return nil
}
type StatSorter struct {
SortKey int
Key []string
Stats []*tootStat
}
func (a StatSorter) Len() int {
return len(a.Stats)
}
func (a StatSorter) Swap(i, j int) {
a.Key[i], a.Key[j] = a.Key[j], a.Key[i]
a.Stats[i], a.Stats[j] = a.Stats[j], a.Stats[i]
}
func (a StatSorter) Less(i, j int) bool {
switch a.SortKey {
case SortByReplies:
return a.Stats[i].Replies < a.Stats[j].Replies
case SortByLikes:
return a.Stats[i].Likes < a.Stats[j].Likes
case SortByBoosts:
return a.Stats[i].Boosts < a.Stats[j].Boosts
case SortByScore:
return (a.Stats[i].Boosts*3)+a.Stats[i].Likes <
(a.Stats[j].Boosts*3)+a.Stats[j].Likes
default:
panic("SortKey is incorrect")
}
}
type kv struct {
Key string
Value int64
}
func printTable(cols []string, emptyText string, data []kv) {
sort.Slice(data, func(i, j int) bool {
return data[i].Value > data[j].Value
})
col1 := columns - len(cols[1]) - 8
// col2 := len(cols[1])
tab := table.NewWriter()
tab.SetOutputMirror(os.Stdout)
tab.SetStyle(table.StyleLight)
tab.SetAllowedRowLength(columns)
tab.AppendHeader(table.Row{cols[0], cols[1]})
tab.SetColumnConfigs([]table.ColumnConfig{
{Name: cols[0], WidthMax: col1},
{Name: cols[1]},
})
for i, kv := range data {
if i >= topN {
break
}
/*
if runewidth.StringWidth(kv.Key) > col1-4 {
kv.Key = kv.Key[:col1-4] + "..."
}
*/
tab.AppendRow([]interface{}{kv.Key, kv.Value})
tab.AppendSeparator()
}
tab.Render()
fmt.Println()
}
func printTootTable(cols []string, emptyText string, toots []string, tootStats []*tootStat, sortKey int) {
sort.Sort(sort.Reverse(StatSorter{sortKey, toots, tootStats}))
var ss []kv
for k, v := range toots {
switch sortKey {
case SortByReplies:
if tootStats[k].Replies == 0 {
continue
}
ss = append(ss, kv{v, tootStats[k].Replies})
case SortByLikes:
if tootStats[k].Likes == 0 {
continue
}
ss = append(ss, kv{v, tootStats[k].Likes})
case SortByBoosts:
if tootStats[k].Boosts == 0 {
continue
}
ss = append(ss, kv{v, tootStats[k].Boosts})
case SortByScore:
score := (tootStats[k].Boosts * 3) + tootStats[k].Likes
if score == 0 {
continue
}
ss = append(ss, kv{v, score})
}
}
printTable(cols, emptyText, ss)
}
func printAccountStats(stats *stats) {
var likes, boosts, replies int64
for _, t := range stats.Toots {
likes += t.Likes
boosts += t.Boosts
replies += t.Replies
}
fmt.Printf("Total toots: %d (excluding replies & boosts)\n", len(stats.Toots))
fmt.Printf("Toots per day: %.2f (account created %d days ago)\n",
float64(len(stats.Toots))/float64(stats.DaysActive),
stats.DaysActive)
fmt.Printf("Ratio toots/replies: %.2f\n",
float64(len(stats.Toots))/float64(len(stats.Replies)))
fmt.Printf("New followers per day: %.2f\n",
float64(stats.Followers)/float64(stats.DaysActive))
fmt.Printf("New followings per day: %.2f\n",
float64(stats.Following)/float64(stats.DaysActive))
fmt.Printf("Likes per toot: %.2f (total likes: %d)\n",
float64(likes)/float64(len(stats.Toots)),
likes)
fmt.Printf("Boosts per toot: %.2f (total boosts: %d)\n",
float64(boosts)/float64(len(stats.Toots)),
boosts)
fmt.Printf("Replies per toot: %.2f (total replies: %d)\n",
float64(replies)/float64(len(stats.Toots)),
replies)
fmt.Println()
}
func printInteractionStats(stats *stats) {
var ss []kv
for k, v := range stats.Mentions {
ss = append(ss, kv{k, v})
}
printTable([]string{"Users you mentioned most", "Interactions"},
"No interactions found.",
ss)
ss = []kv{}
for k, v := range stats.Boosts {
ss = append(ss, kv{k, v})
}
printTable([]string{"Users you boosted most", "Interactions"},
"No interactions found.",
ss)
ss = []kv{}
for k, v := range stats.Responses {
ss = append(ss, kv{k, v})
}
printTable([]string{"Users that responded most", "Interactions"},
"No interactions found.",
ss)
}
func printTootStats(stats *stats) {
var toots []string
var tootStats []*tootStat
for toot, s := range stats.Toots {
toots = append(toots, toot)
tootStats = append(tootStats, s)
}
// most replied-to toots
printTootTable([]string{"Most replied-to toots", "Replies"},
"No toots found.",
toots, tootStats, SortByReplies)
// most liked toots
printTootTable([]string{"Most liked toots", "Likes"},
"No toots found.",
toots, tootStats, SortByLikes)
// most boosted toots
printTootTable([]string{"Most boosted toots", "Boosts"},
"No toots found.",
toots, tootStats, SortByBoosts)
// highest scoring toots
printTootTable([]string{"Highest scoring toots", "Score"},
"No toots found.",
toots, tootStats, SortByScore)
}
func printTagStats(stats *stats) {
var tags []string
var tagStats []*tootStat
for tag, s := range stats.Tags {
tags = append(tags, tag)
tagStats = append(tagStats, s)
}
// most liked tags
printTootTable([]string{"Tags used that got the most likes", "Likes"},
"No toots found.",
tags, tagStats, SortByLikes)
// most boosted tags
printTootTable([]string{"Tags used that got the most boosts", "Boosts"},
"No toots found.",
tags, tagStats, SortByBoosts)
}
func init() {
statsCmd.Flags().IntVarP(&topN, "top", "t", 10, "shows the top N items in each category")
statsCmd.Flags().IntVarP(&maxToots, "recent", "r", 0, "only account for the N most recent toots (excl replies & boosts)")
statsCmd.Flags().IntVarP(&columns, "columns", "", 80, "displays tables with N columns")
// user = flag.String("user", "@[email protected]", "shows stats for this user")
RootCmd.AddCommand(statsCmd)
}