Skip to content

Instantly share code, notes, and snippets.

@hiredman
Created December 10, 2022 00:54
Show Gist options
  • Save hiredman/7934fe424ba8e2eea711a16837687501 to your computer and use it in GitHub Desktop.
Save hiredman/7934fe424ba8e2eea711a16837687501 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -euo pipefail
IFS=$'\n\t'
SERVER=
CLIENT_NAME=
CLIENT_ID=
CLIENT_SECRET=
AUTH_CODE=
ACCESS_TOKEN=
NOAA_URL=https://tgftp.nws.noaa.gov/data/forecasts/zone/wa/waz503.txt
ACCOUNT_ID=
[ -e ~/.mastodon-weather.env ] && source ~/.mastodon-weather.env
if [ -z "$SERVER" ]; then
echo -n "Name of server: "
read SERVER
echo "SERVER=$SERVER" >> ~/.mastodon-weather.env
exec $0
fi
if [ -z "$CLIENT_NAME" ]; then
echo "CLIENT_NAME=`uuidgen`" >> ~/.mastodon-weather.env
exec $0
fi
if [ -z "$CLIENT_SECRET" ]; then
t=$(mktemp)
curl -X POST \
-F "client_name=$CLIENT_NAME" \
-F 'redirect_uris=urn:ietf:wg:oauth:2.0:oob' \
-F 'scopes=read write follow push' \
-F 'website=https://myapp.example' \
https://$SERVER/api/v1/apps > $t
CS=$(jq -r '.client_secret' $t)
CI=$(jq -r '.client_id' $t)
echo "CLIENT_ID=$CI" >> ~/.mastodon-weather.env
echo "CLIENT_SECRET=$CS" >> ~/.mastodon-weather.env
rm $t
exec $0
fi
if [ -z "$AUTH_CODE" ]; then
echo "https://toots.downey.family/oauth/authorize?response_type=code&client_id=$CLIENT_ID&redirect_uri=urn:ietf:wg:oauth:2.0:oob&scope=read,write,follow,push"
echo -n "auth code: "
read AUTH_CODE
echo "AUTH_CODE=$AUTH_CODE" >> ~/.mastodon-weather.env
exec $0
fi
if [ -z "$ACCESS_TOKEN" ]; then
t=$(mktemp)
curl -X POST \
-F "grant_type=authorization_code" \
-F "code=$AUTH_CODE" \
-F "client_id=$CLIENT_ID" \
-F "client_secret=$CLIENT_SECRET" \
-F 'redirect_uri=urn:ietf:wg:oauth:2.0:oob' \
-F 'scopes=read write follow push' \
https://$SERVER/oauth/token > $t
ACCESS_TOKEN=$(jq -r '.access_token' $t)
echo "ACCESS_TOKEN=$ACCESS_TOKEN" >> ~/.mastodon-weather.env
exec $0
fi
if [ -z "$ACCOUNT_ID" ]; then
ACCOUNT_ID=$(curl -H "Authorization: Bearer $ACCESS_TOKEN" https://$SERVER/api/v1/accounts/verify_credentials | jq -r '.id')
echo "ACCOUNT_ID=$ACCOUNT_ID" >> ~/.mastodon-weather.env
exec $0
fi
curl -XPATCH \
-H "Authorization: Bearer $ACCESS_TOKEN" \
-F 'locked=false' \
https://$SERVER/api/v1/accounts/update_credentials
# curl -H "Authorization: Bearer $ACCESS_TOKEN" https://$SERVER/api/v1/accounts/verify_credentials|jq
# exit 0
# curl -XPOST -H "Authorization: Bearer $ACCESS_TOKEN" -d "status=HelloWorld&visibility=public" https://toots.downey.family/api/v1/statuses
t=$(mktemp)
curl -s $NOAA_URL > $t
t2=$(mktemp)
t3=$(mktemp)
STATE=0
LP="foo=bar"
VISIBILITY="public"
cat $t| while read line; do
case $STATE in
0)
if echo "$line" | grep -qE '\.[A-Z]+\.\.\.'; then
echo -n "$line " >> $t2
STATE=1
fi
;;
1)
if echo "$line" | grep -qE '\.[A-Z]+\.\.\.'; then
curl -X POST \
-H "Authorization: Bearer $ACCESS_TOKEN" \
-F "status=<$t2" \
-F "visibility=$VISIBILITY" \
-F $LP \
https://$SERVER/api/v1/statuses > $t3
cat $t3 | jq
VISIBILITY=unlisted
LP="in_reply_to_id=$(jq -r '.id' $t3)"
echo > $t2
echo -n "$line " >> $t2
sleep 1
STATE=1
else
if [ -z "$line" ]; then
curl -X POST \
-H "Authorization: Bearer $ACCESS_TOKEN" \
-F "status=<$t2" \
-F "visibility=$VISIBILITY" \
https://$SERVER/api/v1/statuses > $t3
LP="in_reply_to_id=$(jq -r '.id' $t3)"
echo > $t2
STATE=2
sleep 1
else
echo -n "$line " >> $t2
STATE=1
fi
fi
;;
2)
:
;;
*)
echo $STATE
exit -1;;
esac
done
rm $t $t2 $t3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment