diff --git a/ratatoskr.sh b/ratatoskr.sh index 8d4087c..5e1f22e 100755 --- a/ratatoskr.sh +++ b/ratatoskr.sh @@ -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 }