Improved the post, -p, function.

This commit is contained in:
stormdragon2976 2023-01-30 01:35:05 -05:00
parent 13aeccd78f
commit 7b12f2797a

View File

@ -62,11 +62,23 @@ get_oauth_token() {
# Scrobble music with -S flag
scrobble_music() {
curl -sS --oauth2-bearer "${oauth_token}" \
local result
result="$(curl -sS --oauth2-bearer "${oauth_token}" \
-d "$(playerctl metadata -f 'album={{album}}')" \
-d "$(playerctl metadata -f 'artist={{artist}}')" \
-d "$(playerctl metadata -f 'title={{title}}')" \
"${instanceURL}/api/v1/pleroma/scrobble"
"${instanceURL}/api/v1/pleroma/scrobble")"
# Check for errors
if [[ $? -ne 0 ]]; then
echo "there was a problem contacting the server"
exit 1
fi
local error="$(echo "$result" | jq -r '.error')"
if [[ "$error" != "null" ]]; then
echo "Error: $error"
exit 1
fi
echo "Track scrobbled!"
exit 0
}
@ -97,9 +109,28 @@ post_music() {
# Post status with -p flag, command line.
post_status() {
curl -sS --oauth2-bearer "${oauth_token}" -H "Content-Type: ${content_type:-text/markdown}" \
-d "status=${@}" \
"${instanceURL}/api/v1/statuses"
local text="$@"
visibility="${visibility:-public}"
local content_type="${content_type:-text/markdown}"
local json="$(jq -n --arg status "$text" \
--arg spoiler_text "$spoiler_text" \
--arg visibility "$visibility" \
--arg content_type "$content_type" \
'{ status: $status, spoiler_text: $spoiler_text, visibility: $visibility, content_type: $content_type }')"
local result
result="$(curl -sS --oauth2-bearer "${oauth_token}" -H "Content-Type: application/json" \
-d "$(echo "$json" | jq 'if .spoiler_text == "" then del(.spoiler_text) else . end | if .visibility == "" then del(.visibility) else . end')" \
"${instanceURL}/api/v1/statuses")"
if [[ $? -ne 0 ]]; then
echo "there was a problem contacting the server"
exit 1
fi
local error="$(echo "$result" | jq -r '.error')"
if [[ "$error" != "null" ]]; then
echo "Error: $error"
exit 1
fi
echo "Status posted!"
exit 0
}