···
12
+
"github.com/bluesky-social/indigo/atproto/syntax"
13
+
"github.com/redis/go-redis/v9"
16
+
const PlcExportUrl = `https://plc.directory/export`
17
+
const PlcOpsCountKey = `dev.edavis.muninsky.plc_ops`
20
+
ctx := context.Background()
21
+
client := http.DefaultClient
23
+
rdb := redis.NewClient(&redis.Options{
24
+
Addr: "localhost:6379",
29
+
req, err := http.NewRequestWithContext(ctx, "GET", PlcExportUrl, nil)
34
+
var lastCursor string
36
+
cursor = syntax.DatetimeNow().String()
38
+
q := req.URL.Query()
39
+
q.Add("count", "1000")
40
+
req.URL.RawQuery = q.Encode()
43
+
q := req.URL.Query()
45
+
q.Set("after", cursor)
47
+
req.URL.RawQuery = q.Encode()
49
+
log.Printf("requesting %s\n", req.URL.String())
50
+
resp, err := client.Do(req)
52
+
log.Printf("error doing PLC request: %v\n", err)
54
+
if resp.StatusCode != http.StatusOK {
55
+
log.Printf("PLC request failed status=%d\n", resp.StatusCode)
58
+
respBytes, err := io.ReadAll(resp.Body)
60
+
log.Printf("error reading response body: %v\n", err)
63
+
lines := strings.Split(string(respBytes), "\n")
64
+
if len(lines) == 0 || (len(lines) == 1 && len(lines[0]) == 0) {
65
+
time.Sleep(5 * time.Second)
70
+
for _, l := range lines {
75
+
var op map[string]interface{}
76
+
err = json.Unmarshal([]byte(l), &op)
78
+
log.Printf("error decoding JSON: %v\n", err)
82
+
cursor, ok = op["createdAt"].(string)
84
+
log.Printf("missing createdAt")
87
+
if cursor == lastCursor {
95
+
log.Printf("fetched %d operations", opCount)
96
+
if _, err := rdb.IncrBy(ctx, PlcOpsCountKey, opCount).Result(); err != nil {
97
+
log.Printf("error incrementing op count in redis: %v\n", err)
100
+
time.Sleep(5 * time.Second)