1
0
Files
verse/bin/verse.zsh
2025-08-04 11:54:41 +09:00

129 lines
2.5 KiB
Bash
Executable File

#!/bin/zsh
function at_env_arg() {
host=syu.is
handle=ai.syui.ai
col=ai.syui.verse.user
}
function at_env() {
if [ -n "$1" ];then
handle=$1
fi
if [ -n "$2" ];then
col=$2
fi
d=${0:a:h}
cd $d
dd=${0:a:h:h}
pds=$host
f=$d/${handle}.json
if [ ! -f $f ];then
echo password
read password
else
did=`cat $f|jq -r .did`
access=`cat $f|jq -r .accessJwt`
refresh=`cat $f|jq -r .refreshJwt`
fi
j_verse=`cat $dd/json/verse.json`
j_user=`cat $dd/json/${handle}.user.json`
case $col in
ai.syui.verse)
j=${j_verse}
rkey=self
;;
ai.syui.verse.user)
j=${j_user}
rkey=self
rkey=`echo $did|cut -d : -f 3`
;;
esac
if ! echo $j|jq .;then
exit
fi
if [ -n "`which gdate`" ];then
date=`gdate --iso-8601=seconds`
else
date=`date --iso-8601=seconds`
fi
}
function at_create_session(){
if [ ! -f $f ];then
req=com.atproto.server.createSession
url=https://$pds/xrpc/$req
curl -sL -X POST -H "Content-Type: application/json" -d "{\"identifier\":\"$handle\",\"password\":\"$password\"}" $url >! $f
did=`cat $f|jq -r .did`
access=`cat $f|jq -r .accessJwt`
refresh=`cat $f|jq -r .refreshJwt`
fi
}
function at_refresh_session(){
req=com.atproto.server.refreshSession
url=https://${pds}/xrpc/$req
curl -sL -X POST -H "Content-Type: application/json" -H "Authorization: Bearer $refresh" $url >! $f
did=`cat $f|jq -r .did`
access=`cat $f|jq -r .accessJwt`
refresh=`cat $f|jq -r .refreshJwt`
}
function at_put_record() {
req=com.atproto.repo.putRecord
url=https://$pds/xrpc/$req
# jqでrecordを構築
record=$(echo "$j" | jq ". + {\"createdAt\": \"$date\", \"updatedAt\": \"$date\"}")
json=$(jq -n \
--arg repo "$handle" \
--arg did "$did" \
--arg collection "$col" \
--arg rkey "$rkey" \
--argjson record "$record" \
'{
"repo": $repo,
"did": $did,
"collection": $collection,
"rkey": $rkey,
"record": $record
}')
if echo $json|jq . ;then
t=`curl -sL -X POST -H "Content-Type: application/json" -H "Authorization: Bearer $access" -d "$json" $url`
echo $t|jq .
fi
}
function at_delete_record() {
req=com.atproto.repo.DeleteRecord
url=https://$pds/xrpc/$req
echo collection:
read col
echo rkey:
read rkey
json="{\"collection\":\"$col\", \"rkey\":\"$rkey\", \"repo\":\"$handle\"}"
if echo $json|jq . ;then
t=`curl -sL -X POST -H "Content-Type: application/json" -H "Authorization: Bearer $access" -d $json $url`
echo $t|jq .
fi
}
at_env_arg
at_env $1 $2
at_create_session
at_refresh_session
case $1 in
d)
at_delete_record
;;
*)
at_put_record
;;
esac