-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.go
73 lines (58 loc) · 1.36 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
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/jlewi/bsctl/pkg"
"os"
"sort"
"strings"
"github.com/bluesky-social/indigo/api/bsky"
"github.com/urfave/cli/v2"
)
func doSearch(cCtx *cli.Context) error {
if !cCtx.Args().Present() {
return cli.ShowSubcommandHelp(cCtx)
}
xrpcc, err := pkg.MakeXRPCC(cCtx)
if err != nil {
return fmt.Errorf("cannot create client: %w", err)
}
n := cCtx.Int64("n")
terms := strings.Join(cCtx.Args().Slice(), " ")
var results []*bsky.FeedDefs_PostView
var cursor string
for {
resp, err := bsky.FeedSearchPosts(context.TODO(), xrpcc, "", cursor, "", "", 100, "", terms, "", "", nil, "", "")
if err != nil {
return fmt.Errorf("cannot perform search: %w", err)
}
if resp.Cursor != nil {
cursor = *resp.Cursor
} else {
cursor = ""
}
results = append(results, resp.Posts...)
if cursor == "" || int64(len(results)) > n {
break
}
}
sort.Slice(results, func(i, j int) bool {
ri := pkg.Timep(results[i].Record.Val.(*bsky.FeedPost).CreatedAt)
rj := pkg.Timep(results[j].Record.Val.(*bsky.FeedPost).CreatedAt)
return ri.Before(rj)
})
if int64(len(results)) > n {
results = results[len(results)-int(n):]
}
if cCtx.Bool("json") {
for _, p := range results {
json.NewEncoder(os.Stdout).Encode(p)
}
} else {
for _, p := range results {
pkg.PrintPost(p)
}
}
return nil
}