-
Notifications
You must be signed in to change notification settings - Fork 0
/
atproto
executable file
·153 lines (112 loc) · 3.29 KB
/
atproto
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env bash
### DEPENDENCIES ###
curl=$(type -p curl) || exit
jq=$(type -p jq ) || exit
(type -p echo && type -p printf && type -p cat) >/dev/null || exit
(type -p cut && type -p rev) >/dev/null || exit
function curl() {
$curl -fsSL "$@"
}
function jq() {
$jq "$@"
}
### ATProto ###
function atproto() {
usage=$(cat <<USAGE
The AT Protocol
$(basename $0)
$(basename $0) ping
$(basename $0) whoami
$(basename $0) create <TEXT>
$(basename $0) read <RKEY>
$(basename $0) delete <RKEY>
USAGE
)
ATP_XRPC=${ATP_XRPC-https://public.api.bsky.app/xrpc}
ATP_COLLECTION=${ATP_COLLECTION-app.bsky.feed.post}
function atp.ping() {
json=$(curl ${ATP_XRPC}/_health) || exit
printf "pong with this version: %s\n" $(echo $json | jq -r .version)
}
# Create an authentication session:
function atp.server.createSession() {
_d="{ \"identifier\": \"${ATP_HANDLE}\", \"password\": \"${ATP_PASSWORD}\" }"
json=$(curl -X POST "$ATP_XRPC/com.atproto.server.createSession" -d "$_d" \
-H 'Content-Type: application/json') || exit
echo $json
}
# Create a single new repository record:
function atp.repo.createRecord() {
_a="$@"
_d="{\"repo\": \"${ATP_HANDLE}\", \"collection\": \"${ATP_COLLECTION}\", \"record\": {\"text\": \"${_a}\", \"createdAt\": \"$(date -u +%Y-%m-%dT%H:%M:%SZ)\"}}"
_t=$(atp.server.createSession | jq -r .accessJwt) || exit
json=$(curl -X POST "$ATP_XRPC/com.atproto.repo.createRecord" -d "$_d" \
-H 'Content-Type: application/json' \
-H "Authorization: Bearer $_t") || exit
rkey=$(echo $json | jq -r .uri | rev | cut -d '/' -f 1 | rev)
echo && echo $rkey: && echo
atp.repo.getRecord $rkey && echo
}
# Get a single record from a repository:
function atp.repo.getRecord() {
json=$(curl "$ATP_XRPC/com.atproto.repo.getRecord?repo=${ATP_HANDLE}&collection=${ATP_COLLECTION}&rkey=${1}") || exit
echo $json | jq '.value | { text, createdAt }'
}
# Write a repository record, creating or updating it as needed:
#function atp.repo.putRecord() {
#}
# Delete a repository record, or ensure it doesn't exist:
function atp.repo.deleteRecord() {
_d="{\"repo\": \"${ATP_HANDLE}\", \"collection\": \"${ATP_COLLECTION}\", \"rkey\": \"$1\"}"
_t=$(atp.server.createSession | jq -r .accessJwt) || exit
curl -X POST "$ATP_XRPC/com.atproto.repo.deleteRecord" -d "$_d" \
-H 'Content-Type: application/json' \
-H "Authorization: Bearer $_t" || exit
}
case $1 in
ping)
atp.ping
;;
whoami)
atp.server.createSession | jq -c '. | {handle,email}'
;;
create)
shift
atp.repo.createRecord $@
;;
read)
shift
atp.repo.getRecord $1
;;
delete)
shift
atp.repo.deleteRecord $1
;;
*)
IFS=
echo $usage
;;
esac
}
if !(return 0 2>/dev/null)
then
atp=atproto
prefix=~
config=.config/$atp
if [ ! -s $prefix/$config/${atp}rc ]
then
umask 077 && mkdir -p $prefix/$config || exit
(cat << CONFIG
# ${atp}rc to be stored in \$HOME/$config
# Identity:
ATP_HANDLE=
ATP_PASSWORD=
# PDS/API:
#ATP_XRPC=https://userdir.de/xrpc
CONFIG
) > $prefix/$config/${atp}rc
fi
[ -s $prefix/$config/${atp}rc ] || exit
. $prefix/$config/${atp}rc
$atp "$@"
fi