Skip to content

Commit 4160b59

Browse files
committed
collect posts
0 parents  commit 4160b59

4 files changed

Lines changed: 125 additions & 0 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
posts.db
2+
cave

go.mod

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module cave
2+
3+
go 1.24.4
4+
5+
require (
6+
github.com/dustin/go-humanize v1.0.1 // indirect
7+
github.com/google/uuid v1.6.0 // indirect
8+
github.com/gorilla/websocket v1.5.3 // indirect
9+
github.com/mattn/go-isatty v0.0.20 // indirect
10+
github.com/ncruces/go-strftime v1.0.0 // indirect
11+
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
12+
golang.org/x/exp v0.0.0-20251023183803-a4bb9ffd2546 // indirect
13+
golang.org/x/sys v0.37.0 // indirect
14+
modernc.org/libc v1.67.6 // indirect
15+
modernc.org/mathutil v1.7.1 // indirect
16+
modernc.org/memory v1.11.0 // indirect
17+
modernc.org/sqlite v1.45.0 // indirect
18+
)

go.sum

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
2+
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
3+
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
4+
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
5+
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
6+
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
7+
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
8+
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
9+
github.com/ncruces/go-strftime v1.0.0 h1:HMFp8mLCTPp341M/ZnA4qaf7ZlsbTc+miZjCLOFAw7w=
10+
github.com/ncruces/go-strftime v1.0.0/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
11+
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
12+
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
13+
golang.org/x/exp v0.0.0-20251023183803-a4bb9ffd2546 h1:mgKeJMpvi0yx/sU5GsxQ7p6s2wtOnGAHZWCHUM4KGzY=
14+
golang.org/x/exp v0.0.0-20251023183803-a4bb9ffd2546/go.mod h1:j/pmGrbnkbPtQfxEe5D0VQhZC6qKbfKifgD0oM7sR70=
15+
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
16+
golang.org/x/sys v0.37.0 h1:fdNQudmxPjkdUTPnLn5mdQv7Zwvbvpaxqs831goi9kQ=
17+
golang.org/x/sys v0.37.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
18+
modernc.org/libc v1.67.6 h1:eVOQvpModVLKOdT+LvBPjdQqfrZq+pC39BygcT+E7OI=
19+
modernc.org/libc v1.67.6/go.mod h1:JAhxUVlolfYDErnwiqaLvUqc8nfb2r6S6slAgZOnaiE=
20+
modernc.org/mathutil v1.7.1 h1:GCZVGXdaN8gTqB1Mf/usp1Y/hSqgI2vAGGP4jZMCxOU=
21+
modernc.org/mathutil v1.7.1/go.mod h1:4p5IwJITfppl0G4sUEDtCr4DthTaT47/N3aT6MhfgJg=
22+
modernc.org/memory v1.11.0 h1:o4QC8aMQzmcwCK3t3Ux/ZHmwFPzE6hf2Y5LbkRs+hbI=
23+
modernc.org/memory v1.11.0/go.mod h1:/JP4VbVC+K5sU2wZi9bHoq2MAkCnrt2r98UGeSK7Mjw=
24+
modernc.org/sqlite v1.45.0 h1:r51cSGzKpbptxnby+EIIz5fop4VuE4qFoVEjNvWoObs=
25+
modernc.org/sqlite v1.45.0/go.mod h1:CzbrU2lSB1DKUusvwGz7rqEKIq+NUd8GWuBBZDs9/nA=

main.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package main
2+
3+
import (
4+
"database/sql"
5+
"encoding/json"
6+
"log"
7+
"net/url"
8+
9+
_ "modernc.org/sqlite"
10+
"github.com/gorilla/websocket"
11+
)
12+
13+
type MinimalEvent struct {
14+
TimeUS int64 `json:"time_us"`
15+
Commit struct {
16+
Record struct {
17+
Text string `json:"text"`
18+
} `json:"record"`
19+
} `json:"commit"`
20+
}
21+
22+
func main() {
23+
24+
db, err := sql.Open("sqlite", "posts.db")
25+
if err != nil {
26+
log.Fatal(err)
27+
}
28+
defer db.Close()
29+
30+
_, err = db.Exec(`
31+
CREATE TABLE IF NOT EXISTS posts (
32+
id INTEGER PRIMARY KEY AUTOINCREMENT,
33+
time_us INTEGER,
34+
text TEXT
35+
) STRICT
36+
`)
37+
if err != nil {
38+
log.Fatal(err)
39+
}
40+
41+
stmt, err := db.Prepare("INSERT INTO posts(time_us, text) VALUES (?, ?)")
42+
if err != nil {
43+
log.Fatal(err)
44+
}
45+
defer stmt.Close()
46+
47+
u := url.URL{
48+
Scheme: "wss",
49+
Host: "jetstream1.us-east.bsky.network",
50+
Path: "/subscribe",
51+
RawQuery: "collections=app.bsky.feed.post",
52+
}
53+
54+
conn, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
55+
if err != nil {
56+
log.Fatal("Dial error:", err)
57+
}
58+
defer conn.Close()
59+
60+
for {
61+
_, message, err := conn.ReadMessage()
62+
if err != nil {
63+
log.Fatal("Read error:", err)
64+
}
65+
66+
var e MinimalEvent
67+
if err := json.Unmarshal(message, &e); err != nil {
68+
continue
69+
}
70+
71+
if e.Commit.Record.Text == "" {
72+
continue
73+
}
74+
75+
_, err = stmt.Exec(e.TimeUS, e.Commit.Record.Text)
76+
if err != nil {
77+
log.Println("Insert error:", err)
78+
}
79+
}
80+
}

0 commit comments

Comments
 (0)