-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmastodon-shell-post-media.sh
93 lines (78 loc) · 3.41 KB
/
mastodon-shell-post-media.sh
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
#!/bin/bash
#
# A simple script to post to Mastodon from command line
# Elias Torres @[email protected]
# License: https://www.gnu.org/licenses/gpl-3.0.html
# Please use, improve, remix, fork
# and don't forget to share and contribute
# Dependencies:
# - curl https://curl.se/
# - file https://www.darwinsys.com/file/
# - jq https://stedolan.github.io/jq/
# Debian:
# apt install curl file jq
# Usage:
# mastodon-shell-post-media.sh "Status text" "media1.jpg" "Alt1" ["media2.jpg"] ["Alt2"]
# Mastodon API documentation:
# https://docs.joinmastodon.org/methods/statuses/
# https://docs.joinmastodon.org/methods/media/
# Strategy:
# 1. Images are uploaded first, server returns an ID for each one.
# 2. Then we post a status referring to those IDs in an array, 4 max.
# Create an application at:
# https://mastodon.example/settings/applications
ACCESS_TOKEN=""
# FQDN of the mastodon server/instance
MASTODON_SERVER="mastodon.example.com"
# visibility: String. Sets the visibility of the posted status to public, unlisted, private, direct.
VISIBILITY="unlisted"
# language: String. ISO 639 language code for this status.
LANG="en"
# Optionally move config variables to a separate file
#CONFIG_FILE=~/.mastodon-shell-config.conf
#source $CONFIG_FILE || { echo "Config file $CONFIG_FILE not found or not readable" >&2 ; exit 1 ; }
# TODO: Validate config
# End of configuration
# status: REQUIRED String. The text content of the status.
# If media_ids is provided, this becomes optional.
# Attaching a poll is optional while status is provided.
# $@ are all parameters of this script
statustext=$1
# Image 1 and its alt description. # Mastodon API documentation:
# https://docs.joinmastodon.org/methods/statuses/
# Create an application at:
# https://mastodon.example/settings/applications
ACCESS_TOKEN=""
# FQDN of the mastodon server/instance
MASTODON_SERVER="mastodon.example.com"
# visibility: String. Sets the visibility of the posted status to public, unlisted, private, direct.
VISIBILITY="unlisted"
# language: String. ISO 639 language code for this status.
LANG="en"
file="$2"
description="$3"
# Check if the file is readable and is an image
test -f "$file" || { echo "Media file $file not found or not readable" >&2 ; exit 1 ; }
file --mime-type "$file" | grep -q image || { echo "Media file $file looks like is not an image based on command file(1)" >&2 ; exit 1 ; }
header="Authorization: Bearer $ACCESS_TOKEN"
URL="https://$MASTODON_SERVER/api/v2/media"
POST_DATA="status=$statustext&language=$LANG&visibility=$VISIBILITY"
_media_response=$(curl --header "$header" -sS $URL -X POST \
-F "description=$description" -F "file=@$file" )
_id=$(echo $_media_response | jq '.id' | sed s'/"//g')
POST_DATA="$POST_DATA&media_ids[]=$_id"
# Optionally add a second file, arguments 4 and 5
if test -f "$4" && file --mime-type "$4" | grep -q image; then
description="$5"
_media_response=$(curl --header "$header" -sS $URL -X POST \
-F "description=$description" -F "file=@$4" )
_id2=$(echo $_media_response | jq '.id' | sed s'/"//g')
POST_DATA="$POST_DATA&media_ids[]=$_id2"
fi
# TODO: do the same with more optional parameters, Mastodon accepts up to 4 media files.
# API endpoint to post status
URL2="https://$MASTODON_SERVER/api/v1/statuses"
response=$(curl --header "$header" -sS $URL2 -X POST \
-d "$POST_DATA" | jq '.content')
# TODO: do something with the JSON output
#echo $response