~aleteoryx/tclfeed-bsky

ref: e7e6818c81278aafe4035e9dac60958e6e72bbc1 tclfeed-bsky/bin/rmpost.tcl -rwxr-xr-x 1.0 KiB
e7e6818cAleteoryx maybe workaround db locking? 25 days ago
                                                                                
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
#!/bin/env tclsh

if {$argc < 3} { return -code error "Usage: bin/rmpost.tcl DB FEED URI ?URI ...?" }
set uris [lassign $argv db feed]

cd [file dirname [file dirname [dict get [info frame [info frame]] file]]]

package require sqlite3
package require json
package require http
package require tls
::http::register https 443 ::tls::socket

sqlite3 db $db

foreach uri $uris {
  switch -regexp -matchvar matches -- $uri {
    {^at://.+} {}
    {^(?:(?:(?:(?:(?:https://)?bsky.app)?/)?profile)?/)?([^/]+)/post/(.+)$} {
      lassign $matches _ pub rkey

      set endpoint {https://public.api.bsky.app/xrpc/com.atproto.repo.getRecord}

      set http_state [http::geturl "$endpoint?repo=$pub&collection=app.bsky.feed.post&rkey=$rkey"]
      if {[::http::ncode $http_state] != 200} { return -code error "couldn't resolve bsky.app URI!\n[::http::data $http_state]" }
      set uri [dict get [::json::json2dict [::http::data $http_state]] uri]
    }
    default { return -code error "unknown URI format!" }
  }

  db eval "DELETE FROM $feed WHERE uri = :uri;"
}