···
+
"github.com/bluesky-social/indigo/atproto/syntax"
+
"github.com/redis/go-redis/v9"
+
const PlcExportUrl = `https://plc.directory/export`
+
const PlcOpsCountKey = `dev.edavis.muninsky.plc_ops`
+
ctx := context.Background()
+
client := http.DefaultClient
+
rdb := redis.NewClient(&redis.Options{
+
Addr: "localhost:6379",
+
req, err := http.NewRequestWithContext(ctx, "GET", PlcExportUrl, nil)
+
cursor = syntax.DatetimeNow().String()
+
req.URL.RawQuery = q.Encode()
+
req.URL.RawQuery = q.Encode()
+
log.Printf("requesting %s\n", req.URL.String())
+
resp, err := client.Do(req)
+
log.Printf("error doing PLC request: %v\n", err)
+
if resp.StatusCode != http.StatusOK {
+
log.Printf("PLC request failed status=%d\n", resp.StatusCode)
+
respBytes, err := io.ReadAll(resp.Body)
+
log.Printf("error reading response body: %v\n", err)
+
lines := strings.Split(string(respBytes), "\n")
+
if len(lines) == 0 || (len(lines) == 1 && len(lines[0]) == 0) {
+
time.Sleep(5 * time.Second)
+
for _, l := range lines {
+
var op map[string]interface{}
+
err = json.Unmarshal([]byte(l), &op)
+
log.Printf("error decoding JSON: %v\n", err)
+
cursor, ok = op["createdAt"].(string)
+
log.Printf("missing createdAt")
+
if cursor == lastCursor {
+
log.Printf("fetched %d operations", opCount)
+
if _, err := rdb.IncrBy(ctx, PlcOpsCountKey, opCount).Result(); err != nil {
+
log.Printf("error incrementing op count in redis: %v\n", err)
+
time.Sleep(5 * time.Second)