From 661f00217cd215ef6795ba535a994f6c8b61a35d Mon Sep 17 00:00:00 2001 From: stormdragon2976 Date: Sun, 29 Jan 2023 23:59:59 -0500 Subject: [PATCH] Music posting with -M is now working. I still need to deal with the resulting json, so be prepared for spam. --- ratatoskr.sh | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/ratatoskr.sh b/ratatoskr.sh index 20b84d6..9399fe8 100755 --- a/ratatoskr.sh +++ b/ratatoskr.sh @@ -60,8 +60,8 @@ get_oauth_token() { # Functions that deal with posting. -# Post music with -M flag -post_music() { +# Scrobble music with -S flag +scrobble_music() { curl -sS --oauth2-bearer "${oauth_token}" \ -d "$(playerctl metadata -f 'album={{album}}')" \ -d "$(playerctl metadata -f 'artist={{artist}}')" \ @@ -71,10 +71,21 @@ post_music() { } +# Post music with -M flag requires playerctl. +post_music() { + local text="[$(playerctl metadata -f 'Now playing "{{title}}" by "{{artist}}" from "{{album}}"')]" + local link="(https://www.youtube.com/results?search_query=$(playerctl metadata -f '{{artist}} {{title}}' | urlencode -b))" + local json=$(jq -n --arg status "$text$link" --arg spoiler_text "Music" --arg content_type "text/markdown" '{status: $status, spoiler_text: $spoiler_text, content_type: $content_type}') + curl -sS --oauth2-bearer "${oauth_token}" -H "Content-Type: application/json" \ + -d "$json" \ + "${instanceURL}/api/v1/statuses" + exit 0 +} + + # Post status with -p flag, command line. post_status() { - curl -sS --oauth2-bearer "${oauth_token}" \ - -d "content=${content_type:-text/markdown}" \ + curl -sS --oauth2-bearer "${oauth_token}" -H "Content-Type: ${content_type:-text/markdown}" \ -d "status=${@}" \ "${instanceURL}/api/v1/statuses" exit 0 @@ -98,6 +109,7 @@ declare -A command=( [h]="Help, show usage information for ${0##*/}." [M]="Post the currently playing music track, requires playerctl." [p:]="Post from the command line, e.g. ${0##*/} -p \"hello world\"" + [S]="Scrobble the currently playing music track, requires playerctl." ) # if the default file doesn't exist, create it @@ -118,6 +130,7 @@ while getopts "${args}" i ; do h) help;; M) post_music;; p) post_status "${OPTARG}";; + S) scrobble_music;; esac done