1
0
Fork 0
MastodonMegafono/toot.sh

121 lines
2.7 KiB
Bash
Raw Permalink Normal View History

2019-02-20 13:52:53 +01:00
#!/bin/bash
2018-04-10 13:46:33 +02:00
2018-04-10 13:53:24 +02:00
# WTFPL!
2019-02-19 13:53:09 +01:00
MASTODON_TOKEN=$MASTODON_TOKEN
2019-03-19 11:42:59 +01:00
MASTODON_SERVER="mastodon.cisti.org"
2018-04-10 13:46:33 +02:00
toot_help(){
echo "toot [args..] your status update"
2019-02-19 13:53:09 +01:00
echo "Arguments: "
2018-04-10 13:46:33 +02:00
echo " -i=, --image= select file image to post"
echo " -a=, --alt= the image description for disabilities"
2018-04-10 13:46:33 +02:00
echo " -t=, --token= your API access token"
echo " -s=, --server= mastodon server"
2019-02-20 13:52:53 +01:00
echo " -w, --warn the image is marked as sensitive"
2018-04-10 13:46:33 +02:00
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
2018-04-10 14:10:45 +02:00
. ~/.tootrc
2018-04-10 13:46:33 +02:00
fi
toot_upload_image(){
local image="$1"
local description="$2"
local desc_data="description=$description"
local data="file=@$image"
local id=$(curl --header "Authorization: Bearer $MASTODON_TOKEN" -sS -X POST https://$MASTODON_SERVER/api/v1/media -F "$desc_data" -F "$data" | jq -r .id)
2018-04-10 13:46:33 +02:00
if [ $? -eq 0 ] && [ -n "$id" ]
then
echo $id
return 0
2018-04-10 13:46:33 +02:00
fi
echo "Image upload: Something went wrong"
exit 1
}
2019-02-22 11:20:11 +01:00
toot_post_with_image(){
local status="$1"
local image="$2"
2019-02-20 13:52:53 +01:00
local sensitive="$3"
local data="status=$status&media_ids[]=$image&sensitive=$sensitive"
local error=$(curl --header "Authorization: Bearer $MASTODON_TOKEN" -sS -X POST https://$MASTODON_SERVER/api/v1/statuses -d "$data" | jq -r .error)
2018-04-10 13:46:33 +02:00
if [ "$error" = "null" ]
then
echo "[$(date)][INFO] Tooted: $status with image: $image, sensitive: $sensitive"
2018-04-10 13:46:33 +02:00
else
echo $error
exit 1
2018-04-10 13:46:33 +02:00
fi
2019-02-22 11:20:11 +01:00
}
2018-04-10 13:46:33 +02:00
2019-02-22 11:20:11 +01:00
toot_post(){
local status="$1"
local data="status=$status"
local 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 "[$(date)][INFO] Tooted: $status"
else
echo $error
exit 1
fi
2018-04-10 13:46:33 +02:00
}
image_to_toot=""
image_alt=""
2019-02-20 13:52:53 +01:00
sensitive="false"
2018-04-10 13:46:33 +02:00
2018-04-10 14:24:37 +02:00
if [ $# -eq 0 ]
2018-04-10 13:46:33 +02:00
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
;;
-a=*|--alt=*)
image_alt="${arg#*=}"
shift
;;
2019-02-20 13:52:53 +01:00
-w|--warn)
sensitive="true"
shift
;;
2018-04-10 13:46:33 +02:00
*)
;;
esac
done
2019-03-19 12:22:54 +01:00
if [ "$MASTODON_TOKEN" = "" ]
2018-04-10 13:46:33 +02:00
then
echo "no token set"
exit 1
fi
2019-02-22 11:20:11 +01:00
if [ "$image_to_toot" != "" ] && [ -f $image_to_toot ]
2018-04-10 13:46:33 +02:00
then
image_id=$(toot_upload_image "$image_to_toot" "$image_alt")
2019-02-22 11:20:11 +01:00
toot_post_with_image "$*" "$image_id" "$sensitive"
else
toot_post "$*" "$image_id" "$sensitive"
2019-03-19 11:42:59 +01:00
fi