forked from encrypt/toot
90 lines
1.7 KiB
Bash
90 lines
1.7 KiB
Bash
#!/bin/sh
|
|
|
|
MASTODON_TOKEN=ENV["MASTODON_TOKEN"]
|
|
MASTODON_SERVER="mastodon.bida.im"
|
|
|
|
toot_help(){
|
|
echo "toot [args..] your status update"
|
|
echo "Argouments: "
|
|
echo " -i=, --image= select file image to post"
|
|
echo " -t=, --token= your API access token"
|
|
echo " -s=, --server= mastodon server"
|
|
echo ""
|
|
echo "Mastodon server can be set in ~/.tootrc otherwise it will be defaulted to $MASTODON_SERVER"
|
|
echo "The token can be passed as, cli argument, var in ~/.tootrc, env var (in this priority order)"
|
|
}
|
|
|
|
|
|
if [ -f ~/.tootrc ]
|
|
then
|
|
source ~/.tootrc
|
|
fi
|
|
|
|
toot_upload_image(){
|
|
image="$1"
|
|
id=$(curl --header "Authorization: Bearer $MASTODON_TOKEN" -sS -X POST https://$MASTODON_SERVER/api/v1/media -F "file=@$image" | jq -r .id)
|
|
if [ $? -eq 0 ] && [ -n "$id" ]
|
|
then
|
|
echo $id
|
|
return 0
|
|
fi
|
|
echo "Image upload: Something went wrong"
|
|
exit 1
|
|
|
|
}
|
|
|
|
toot_post(){
|
|
status="$1"
|
|
image="$2"
|
|
data="status=$status&media_ids[]=$image"
|
|
error=$(curl --header "Authorization: Bearer $MASTODON_TOKEN" -sS -X POST https://$MASTODON_SERVER/api/v1/statuses -d "$data" | jq -r .error)
|
|
if [ "$error" = "null" ]
|
|
then
|
|
echo "Yay!"
|
|
else
|
|
echo $error
|
|
exit 1
|
|
fi
|
|
|
|
}
|
|
|
|
image_to_toot=""
|
|
|
|
if [ -n "$@" ]
|
|
then
|
|
toot_help
|
|
exit 1
|
|
fi
|
|
|
|
for arg in "$@"
|
|
do
|
|
case $arg in
|
|
-i=*|--image=*)
|
|
image_to_toot="${arg#*=}"
|
|
shift
|
|
;;
|
|
-t=*|--token=*)
|
|
MASTODON_TOKEN="${arg#*=}"
|
|
shift
|
|
;;
|
|
-s=*|--server=*)
|
|
MASTODON_SERVER="${arg#*=}"
|
|
shift
|
|
;;
|
|
*)
|
|
;;
|
|
esac
|
|
done
|
|
|
|
if [ $MASTODON_TOKEN = "" ]
|
|
then
|
|
echo "no token set"
|
|
exit 1
|
|
fi
|
|
|
|
if [ -n $image_to_toot ] && [ -f $image_to_toot ]
|
|
then
|
|
image_id=$(toot_upload_image $image_to_toot)
|
|
fi
|
|
|
|
toot_post "$*" "$image_id"
|