2023-05-06 11:27:09 -05:00
|
|
|
|
#!/bin/sh
|
|
|
|
|
#―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――
|
|
|
|
|
# Name: fedi-post.sh
|
|
|
|
|
# Desc: Makes a new post using an post archived with fedi-archive.sh.
|
|
|
|
|
# Reqs: curl, jq
|
|
|
|
|
# Date: 2023-05-06
|
|
|
|
|
# Auth: @jadedctrl@jam.xwx.moe
|
|
|
|
|
#―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――
|
|
|
|
|
|
|
|
|
|
|
2023-08-06 21:33:40 -05:00
|
|
|
|
sanitize_text() {
|
|
|
|
|
grep -v ^media: \
|
|
|
|
|
| grep -v ^spoiler: \
|
|
|
|
|
| sed 's%\\%\\\\%g' \
|
|
|
|
|
| sed 's%"%\\"%g' \
|
|
|
|
|
| sed -z 's%\n%\\n%g'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-05-06 11:27:09 -05:00
|
|
|
|
# Outputs only the status text of a user's post, with a link to the original
|
|
|
|
|
# appended to the bottom.
|
|
|
|
|
file_status() {
|
|
|
|
|
local file="$1"
|
|
|
|
|
local id="$(head -1 "$file" | awk '{print $3}')"
|
|
|
|
|
local url="$(head -1 "$file" | awk '{print $4}')"
|
2023-08-06 23:00:07 -05:00
|
|
|
|
tail +2 "$file" \
|
|
|
|
|
| sanitize_text
|
2023-05-06 11:27:09 -05:00
|
|
|
|
if test -n "$url"; then
|
|
|
|
|
printf '<br/>[<a href=\\"%s\\">Originala afiŝo</a>]\\n' "$url"
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Turns a space-delimited list of uploaded media-IDs into a JSON array.
|
|
|
|
|
media_json() {
|
|
|
|
|
local ids="$1"
|
2023-08-06 23:00:07 -05:00
|
|
|
|
if test -n "$ids"; then
|
|
|
|
|
echo "$ids" \
|
|
|
|
|
| sed 's%^ %%' \
|
|
|
|
|
| sed 's% $%%' \
|
|
|
|
|
| sed 's%^%["%' \
|
|
|
|
|
| sed 's% %","%g' \
|
|
|
|
|
| sed 's%$%"]%'
|
|
|
|
|
else
|
|
|
|
|
echo ""
|
|
|
|
|
fi
|
2023-05-06 11:27:09 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Takes a post message and JSON-array of uploaded media-IDs, outputting
|
|
|
|
|
# the post's appropriate JSON.
|
|
|
|
|
post_json() {
|
|
|
|
|
local message="$1"
|
|
|
|
|
local media_ids="$2"
|
2023-08-06 21:33:40 -05:00
|
|
|
|
local spoiler="$3"
|
|
|
|
|
|
|
|
|
|
printf '{ "content_type": "text/html", "visibility": "unlisted",'
|
|
|
|
|
if test -n "$spoiler"; then
|
|
|
|
|
printf ' "spoiler_text": "%s", ' "$(echo "$spoiler" | sanitize_text)"
|
|
|
|
|
fi
|
2023-05-06 11:27:09 -05:00
|
|
|
|
if test -n "$media_ids"; then
|
2023-08-06 21:33:40 -05:00
|
|
|
|
printf ' "media_ids": %s, ' "$media_ids"
|
2023-05-06 11:27:09 -05:00
|
|
|
|
fi
|
|
|
|
|
printf '"status": "%s" }\n' "$message"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Upload a file to the fedi server with the given description.
|
|
|
|
|
post_media() {
|
|
|
|
|
local media_file="$1"
|
|
|
|
|
local description="$2"
|
|
|
|
|
|
|
|
|
|
curl --request POST \
|
|
|
|
|
--header "Authorization: Bearer $FEDI_AUTH" \
|
|
|
|
|
--header "Content-Type: multipart/form-data" \
|
|
|
|
|
--form "file=@$media_file" \
|
|
|
|
|
--form "description=$description" \
|
2024-02-13 09:54:58 -06:00
|
|
|
|
"https://$SERVER/api/v1/media"
|
2023-05-06 11:27:09 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Post a status of the given message and JSON-array of uploaded media-IDs.
|
|
|
|
|
post_status() {
|
|
|
|
|
local message="$1"
|
|
|
|
|
local media_ids="$2"
|
2023-08-06 21:33:40 -05:00
|
|
|
|
local spoiler="$3"
|
2023-05-06 11:27:09 -05:00
|
|
|
|
|
|
|
|
|
curl --request POST \
|
|
|
|
|
--header "Authorization: Bearer $FEDI_AUTH" \
|
|
|
|
|
--header "Content-Type: application/json" \
|
2023-08-06 21:33:40 -05:00
|
|
|
|
--data "$(post_json "$message" "$media_ids" "$spoiler" | tr -d '\n')" \
|
2024-02-13 09:54:58 -06:00
|
|
|
|
"https://$SERVER/api/v1/statuses"
|
2023-05-06 11:27:09 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Take a post file generated by fedi-archive.sh, and post it.
|
|
|
|
|
# Just *do it*. Why not? What're you scared of? Huh, huh? Huh?!
|
|
|
|
|
post_archived_post() {
|
|
|
|
|
local file="$1"
|
|
|
|
|
IFS="
|
|
|
|
|
"
|
|
|
|
|
local ids=""
|
|
|
|
|
for media in $(grep "^media: " "$file"); do
|
|
|
|
|
local url="$(echo "$media" | awk '{print $2}')"
|
|
|
|
|
local desc="$(echo "$media" | awk '{ $1=$2=""; print}' | sed 's%^ %%')"
|
|
|
|
|
|
|
|
|
|
curl -o "$(basename "$url")" "$url"
|
|
|
|
|
ids="$ids $(post_media "$(basename "$url")" "$desc" | jq -r '.id')"
|
|
|
|
|
rm "$(basename "$url")"
|
|
|
|
|
done
|
|
|
|
|
|
2023-08-06 21:33:40 -05:00
|
|
|
|
local spoiler="$(grep "^spoiler: " "$file" | sed 's%^spoiler: %%')"
|
|
|
|
|
|
2023-05-06 11:27:09 -05:00
|
|
|
|
printf '%s ' "$(head -1 "$file" | awk '{print $1, $2}')"
|
2023-08-06 21:33:40 -05:00
|
|
|
|
post_status "$(file_status "$file")" "$(media_json "$ids")" "$spoiler" \
|
2023-05-06 11:27:09 -05:00
|
|
|
|
| jq -r .uri
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
usage() {
|
|
|
|
|
echo "usage: $(basename "$0") ARCHIVED-POST" 1>&2
|
2024-02-13 09:54:58 -06:00
|
|
|
|
echo 1>&2
|
|
|
|
|
echo 'Will post a new status with the same text and attachments as one' 1>&2
|
|
|
|
|
echo 'from an archived post (in fedi-archive.sh format).' 1>&2
|
|
|
|
|
echo 'Your authorization key must be borrowed from your web-browser and' 1>&2
|
2023-05-06 11:27:09 -05:00
|
|
|
|
echo 'placed in the $FEDI_AUTH environment variable.' 1>&2
|
|
|
|
|
exit 2
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if test -z "$FEDI_AUTH"; then
|
|
|
|
|
echo 'You need to set the environment variable $FEDI_AUTH!' 1>&2
|
2024-02-13 09:54:58 -06:00
|
|
|
|
echo 'You can find your auth key by examining the “Authentication: Bearer” header' 1>&2
|
|
|
|
|
echo 'used in requests by your server’s web-client.' 1>&2
|
2023-05-06 11:27:09 -05:00
|
|
|
|
echo 'In Firefox, F12→Network.' 1>&2
|
2024-02-13 09:54:58 -06:00
|
|
|
|
echo 1>&2
|
2023-05-06 11:27:09 -05:00
|
|
|
|
usage
|
|
|
|
|
fi
|
|
|
|
|
|
2024-02-13 09:54:58 -06:00
|
|
|
|
SERVER="$1"
|
|
|
|
|
FILE="$2"
|
|
|
|
|
if test -z "$SERVER" -o -z "$FILE" -o "$1" = "-h" -o "$1" = "--help"; then
|
2023-05-06 11:27:09 -05:00
|
|
|
|
usage
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
2024-02-13 09:54:58 -06:00
|
|
|
|
post_archived_post "$SERVER" "$FILE"
|