-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
search.go
75 lines (65 loc) · 1.73 KB
/
search.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
package main
import (
"context"
"fmt"
"math"
"strings"
"time"
mastodon "github.com/mattn/go-mastodon"
"github.com/muesli/goprogressbar"
"github.com/spf13/cobra"
)
var (
searchCmd = &cobra.Command{
Use: "search <string>",
Short: "searches your toots",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return fmt.Errorf("search requires a search token")
}
return search(args[0])
},
}
)
func search(token string) error {
pb := &goprogressbar.ProgressBar{
Text: fmt.Sprintf("Searching toots for %s", token),
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: progressBarWidth,
}
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 {
if strings.Contains(strings.ToLower(cleanupContent(s.Content)), token) {
fmt.Println("\n\nFound toot:", cleanupContent(s.Content))
fmt.Println("Date:", s.CreatedAt.Format(time.RFC822))
fmt.Println("URL:", s.URL)
fmt.Println()
}
pb.Current++
pb.LazyPrint()
}
// 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
}
}
return nil
}
func init() {
RootCmd.AddCommand(searchCmd)
}