2023-01-28 13:19:59 -05:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
# Hopefully one day this will be a full featured Pleroma client.
|
|
|
|
# Let's see how far we can get. :)
|
|
|
|
|
2023-01-30 19:36:42 -05:00
|
|
|
# Handle subprocesses that may not close with the main program.
|
2023-01-30 20:34:00 -05:00
|
|
|
trap "trap - SIGTERM && kill -- -$$" SIGINT SIGTERM EXIT
|
2023-01-30 19:36:42 -05:00
|
|
|
|
|
|
|
|
2023-01-29 04:23:46 -05:00
|
|
|
# Display usage information.
|
|
|
|
help() {
|
|
|
|
echo "${0##*/}"
|
|
|
|
echo "Released under the terms of the WTFPL License"
|
|
|
|
echo -e "Usage:\n"
|
|
|
|
echo "With no arguments, open the interactive client."
|
|
|
|
for i in "${!command[@]}" ; do
|
|
|
|
echo "-${i/:/ <parameter>}: ${command[${i}]}"
|
|
|
|
done | sort
|
|
|
|
echo
|
|
|
|
echo "Configuration files can be found in ${configPath}"
|
|
|
|
echo
|
|
|
|
exit 0
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-28 13:19:59 -05:00
|
|
|
# Get an oauth token
|
|
|
|
get_oauth_token() {
|
|
|
|
echo "Welcome to ${softwareName}!"
|
|
|
|
echo
|
|
|
|
echo "Let's get you connected to your instance."
|
2023-01-28 18:05:06 -05:00
|
|
|
while [[ -z "${instanceURL}" ]]; do
|
|
|
|
echo
|
|
|
|
read -er -p "Enter the URL of a Pleroma instance: " instanceURL
|
|
|
|
if [[ ! "${instanceURL}" =~ ^https:// ]]; then
|
|
|
|
instanceURL="https://${instanceURL}"
|
2023-01-28 13:19:59 -05:00
|
|
|
fi
|
|
|
|
done
|
2023-01-29 15:42:54 -05:00
|
|
|
redirectURI="urn:ietf:wg:oauth:2.0:oob"
|
2023-01-29 14:32:13 -05:00
|
|
|
website="https://git.stormux.org/storm/ratatoskr"
|
2023-01-28 14:57:45 -05:00
|
|
|
# get client id and secret
|
2023-01-29 14:32:13 -05:00
|
|
|
curl -s -X POST -d client_name="${softwareName}" -d "redirect_uris=${redirectURI}" -d "scopes=read write follow push" -d "website=${website}" "${instanceURL}/api/v1/apps" |
|
2023-01-28 15:43:21 -05:00
|
|
|
jq --raw-output '"client_id=\"\(.client_id)\"\nclient_secret=\"\(.client_secret)\""' > "${configPath}/${configFile}"
|
2023-01-28 15:41:06 -05:00
|
|
|
# Load the new variables from the configuration file
|
|
|
|
source "${configPath}/${configFile}"
|
|
|
|
# Create the url to get the oauth token
|
2023-01-29 04:23:46 -05:00
|
|
|
local url="${instanceURL}/oauth/authorize?client_id=${client_id}&redirect_uri=${redirectURI}&response_type=code&scope=read%20write%20follow%20push"
|
2023-01-28 15:41:06 -05:00
|
|
|
echo "Please open the following url in your browser."
|
2023-01-28 18:05:06 -05:00
|
|
|
echo "Copy the generated token, and paste it here, then press enter to continue."
|
2023-01-28 15:41:06 -05:00
|
|
|
echo
|
2023-01-28 18:12:39 -05:00
|
|
|
if command -v xclip &> /dev/null ; then
|
2023-01-29 16:35:08 -05:00
|
|
|
echo "${url}" | tee >(xclip -selection clipboard -d "${DISPLAY:-:0}" 2> /dev/null &&
|
|
|
|
echo "For convenience the url has been copied to your clipboard.")
|
|
|
|
else
|
|
|
|
echo "${url}"
|
2023-01-28 18:12:39 -05:00
|
|
|
fi
|
2023-01-29 16:35:08 -05:00
|
|
|
echo
|
2023-01-28 18:05:06 -05:00
|
|
|
read -er oauth_token
|
2023-01-29 18:48:29 -05:00
|
|
|
# This is actually a authorization token, so get the actual token.
|
2023-01-29 18:53:17 -05:00
|
|
|
oauth_token="$(curl -sS -d "client_id=${client_id}" -d "client_secret=${client_secret}" -d "code=${oauth_token}" -d "grant_type=authorization_code" "${instanceURL}/oauth/token" | jq --raw-output '(.access_token)')"
|
2023-01-28 18:05:06 -05:00
|
|
|
echo "oauth_token=\"${oauth_token}\"" >> "${configPath}/${configFile}"
|
2023-01-29 14:32:13 -05:00
|
|
|
echo "instanceURL=\"${instanceURL}\"" >> "${configPath}/${configFile}"
|
2023-01-28 13:19:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-30 02:05:54 -05:00
|
|
|
play_sound() {
|
2023-01-30 12:54:28 -05:00
|
|
|
if [[ "${enable_sound}" == "false" ]]; then
|
|
|
|
return
|
|
|
|
fi
|
2023-01-30 02:05:54 -05:00
|
|
|
local soundFile="${configPath}/soundpacks/${sound_pack:-default}/${1}.opus"
|
|
|
|
if [[ -e "${soundFile}" ]]; then
|
|
|
|
sox -qV0 "${soundFile}" -d &> /dev/null &
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-29 04:23:46 -05:00
|
|
|
# Functions that deal with posting.
|
|
|
|
|
2023-01-29 23:59:59 -05:00
|
|
|
# Scrobble music with -S flag
|
|
|
|
scrobble_music() {
|
2023-01-30 01:35:05 -05:00
|
|
|
local result
|
|
|
|
result="$(curl -sS --oauth2-bearer "${oauth_token}" \
|
2023-01-29 14:32:13 -05:00
|
|
|
-d "$(playerctl metadata -f 'album={{album}}')" \
|
|
|
|
-d "$(playerctl metadata -f 'artist={{artist}}')" \
|
|
|
|
-d "$(playerctl metadata -f 'title={{title}}')" \
|
2023-01-30 01:35:05 -05:00
|
|
|
"${instanceURL}/api/v1/pleroma/scrobble")"
|
|
|
|
# Check for errors
|
|
|
|
if [[ $? -ne 0 ]]; then
|
|
|
|
echo "there was a problem contacting the server"
|
2023-01-30 02:05:54 -05:00
|
|
|
play_sound error
|
2023-01-30 01:35:05 -05:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
local error="$(echo "$result" | jq -r '.error')"
|
|
|
|
if [[ "$error" != "null" ]]; then
|
|
|
|
echo "Error: $error"
|
2023-01-30 02:05:54 -05:00
|
|
|
play_sound error
|
2023-01-30 01:35:05 -05:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
echo "Track scrobbled!"
|
2023-01-30 02:05:54 -05:00
|
|
|
play_sound scrobble
|
2023-01-29 04:23:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-29 23:59:59 -05:00
|
|
|
# Post music with -M flag requires playerctl.
|
|
|
|
post_music() {
|
2023-01-30 11:45:29 -05:00
|
|
|
local text="$(playerctl metadata -f 'Now playing "{{title}}" by "{{artist}}" from "{{album}}"')"
|
|
|
|
local link="$(playerctl metadata -f '{{xesam:url}}')"
|
|
|
|
if [[ "${link}" =~ ^file:// ]]; then
|
|
|
|
link="https://www.youtube.com/results?search_query=$(playerctl metadata -f '{{artist}} {{title}}' | urlencode -b)"
|
|
|
|
fi
|
|
|
|
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}')
|
2023-01-30 00:23:54 -05:00
|
|
|
local result
|
|
|
|
result="$(curl -sS --oauth2-bearer "${oauth_token}" -H "Content-Type: application/json" \
|
2023-01-29 23:59:59 -05:00
|
|
|
-d "$json" \
|
2023-01-30 00:23:54 -05:00
|
|
|
"${instanceURL}/api/v1/statuses")"
|
|
|
|
# Check for errors
|
|
|
|
if [[ $? -ne 0 ]]; then
|
|
|
|
echo "there was a problem contacting the server"
|
2023-01-30 02:05:54 -05:00
|
|
|
play_sound error
|
2023-01-30 00:23:54 -05:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
local error="$(echo "$result" | jq -r '.error')"
|
|
|
|
if [[ "$error" != "null" ]]; then
|
|
|
|
echo "Error: $error"
|
2023-01-30 02:05:54 -05:00
|
|
|
play_sound error
|
2023-01-30 00:23:54 -05:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
echo "Music posted!"
|
2023-01-30 12:54:28 -05:00
|
|
|
play_sound post_music
|
2023-01-29 23:59:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-29 19:45:43 -05:00
|
|
|
# Post status with -p flag, command line.
|
|
|
|
post_status() {
|
2023-01-30 01:35:05 -05:00
|
|
|
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"
|
2023-01-30 02:05:54 -05:00
|
|
|
play_sound error
|
2023-01-30 01:35:05 -05:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
local error="$(echo "$result" | jq -r '.error')"
|
|
|
|
if [[ "$error" != "null" ]]; then
|
|
|
|
echo "Error: $error"
|
2023-01-30 02:05:54 -05:00
|
|
|
play_sound error
|
2023-01-30 01:35:05 -05:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
echo "Status posted!"
|
2023-01-30 02:05:54 -05:00
|
|
|
play_sound post
|
2023-01-29 19:45:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-28 13:19:59 -05:00
|
|
|
# Variable initialization
|
|
|
|
configPath="${XDG_CONFIG_HOME:-$HOME/.config}/ratatoskr" # Path for settings, usually ~/.config/ratatoskr
|
2023-01-28 14:57:45 -05:00
|
|
|
configFile="default.conf" # The default config file, eventually will support multiple accounts.
|
2023-01-28 13:19:59 -05:00
|
|
|
softwareName="Ratatoskr" # The name of the client.
|
|
|
|
|
|
|
|
|
|
|
|
# Main code starts here
|
2023-01-28 14:57:45 -05:00
|
|
|
|
2023-01-30 02:05:54 -05:00
|
|
|
# Check for dependencies
|
|
|
|
dependencies=(
|
|
|
|
"jq"
|
|
|
|
"sox"
|
|
|
|
"urlencode"
|
|
|
|
)
|
|
|
|
|
|
|
|
for i in "${dependencies[@]}" ; do
|
|
|
|
if ! command -v "$i" &> /dev/null ; then
|
|
|
|
echo "Missing dependency: $i"
|
|
|
|
exit 2
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
|
|
# make sure the configuration and soundpack paths exist:
|
|
|
|
mkdir -p "${configPath}/soundpacks"
|
2023-01-28 14:57:45 -05:00
|
|
|
|
2023-01-30 19:36:42 -05:00
|
|
|
# Keep track of the backgrounded loop
|
2023-01-30 20:34:00 -05:00
|
|
|
bgLoop=1
|
2023-01-30 19:36:42 -05:00
|
|
|
|
2023-01-29 04:23:46 -05:00
|
|
|
# Associative array of command line parameters and short description of what they do.
|
|
|
|
declare -A command=(
|
2023-01-29 16:35:08 -05:00
|
|
|
[C]="Recreate default configuration file. Acquire new oauth token."
|
2023-01-29 04:23:46 -05:00
|
|
|
[h]="Help, show usage information for ${0##*/}."
|
|
|
|
[M]="Post the currently playing music track, requires playerctl."
|
2023-01-29 19:45:43 -05:00
|
|
|
[p:]="Post from the command line, e.g. ${0##*/} -p \"hello world\""
|
2023-01-29 23:59:59 -05:00
|
|
|
[S]="Scrobble the currently playing music track, requires playerctl."
|
2023-01-29 04:23:46 -05:00
|
|
|
)
|
|
|
|
|
2023-01-28 18:05:06 -05:00
|
|
|
# if the default file doesn't exist, create it
|
|
|
|
if [[ ! -e "${configPath}/${configFile}" ]]; then
|
|
|
|
get_oauth_token
|
2023-01-29 04:23:46 -05:00
|
|
|
else
|
|
|
|
# Read configuration file
|
|
|
|
source "${configPath}/${configFile}"
|
2023-01-28 18:05:06 -05:00
|
|
|
fi
|
2023-01-28 14:57:45 -05:00
|
|
|
|
2023-01-29 04:23:46 -05:00
|
|
|
# Handle command line parameters
|
|
|
|
# Convert the keys of the associative array to a format usable by getopts
|
|
|
|
args="${!command[*]}"
|
|
|
|
args="${args//[[:space:]]/}"
|
|
|
|
while getopts "${args}" i ; do
|
|
|
|
case "$i" in
|
2023-01-29 16:35:08 -05:00
|
|
|
C) get_oauth_token;;
|
2023-01-29 04:23:46 -05:00
|
|
|
h) help;;
|
2023-01-30 12:54:28 -05:00
|
|
|
M)
|
|
|
|
post_music
|
|
|
|
exit 0;;
|
|
|
|
p)
|
|
|
|
post_status "${OPTARG}"
|
|
|
|
exit 0;;
|
|
|
|
S)
|
|
|
|
scrobble_music
|
|
|
|
exit 0;;
|
2023-01-29 04:23:46 -05:00
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
|
2023-01-30 19:36:42 -05:00
|
|
|
# Main loops
|
|
|
|
# Display timelines and requested information.
|
2023-01-30 20:34:00 -05:00
|
|
|
# Important, set bgLoop to 0 or this loop will not close with the program
|
|
|
|
bgLoop=0
|
2023-01-30 18:57:42 -05:00
|
|
|
while : ; do
|
|
|
|
if [[ -n "${since_id}" ]]; then
|
|
|
|
result="$(curl -sS --oauth2-bearer "${oauth_token}" "${instanceURL}/api/va/timelines/${timeline:-home}?since_id=${since_id}")"
|
|
|
|
else
|
|
|
|
result="$(curl -sS --oauth2-bearer "${oauth_token}" "${instanceURL}/api/v1/timelines/${timeline:-home}")"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Error checking
|
|
|
|
# Check if the result is a valid JSON
|
|
|
|
if ! echo "$result" | jq '.' >/dev/null 2>&1; then
|
|
|
|
echo "Error: The response from the server was invalid."
|
|
|
|
play_sound error
|
|
|
|
sleep "${interval:-300}"
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
|
|
|
error=$(echo "$result" | jq -r '.error // "null"' 2> /dev/null)
|
|
|
|
if [[ "${error:-null}" != "null" ]]; then
|
|
|
|
echo "Error fetching ${timeline:-home} timeline: $error"
|
|
|
|
play_sound error
|
|
|
|
sleep "${interval:-300}"
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
|
|
|
# process the response to get the latest event id
|
|
|
|
latest_id="$(jq -r '.data[0].id' <<< "$result")"
|
|
|
|
if [[ "${since_id}" != "${latest_id}" ]]; then
|
|
|
|
# handle new events
|
|
|
|
events="$(jq -r '.data[] | "\(.account.username): \(.content)"' <<< "$result")"
|
|
|
|
echo -e "$events" | while read -r event; do
|
|
|
|
echo "$event"
|
|
|
|
echo
|
|
|
|
done
|
|
|
|
since_id="$latest_id"
|
|
|
|
fi
|
|
|
|
|
|
|
|
sleep "${interval:-300}"
|
2023-01-30 19:36:42 -05:00
|
|
|
done &
|
|
|
|
|
|
|
|
|
|
|
|
# Handle commands
|
|
|
|
while : ; do
|
|
|
|
# Command prompt:
|
|
|
|
read -er -p "<${softwareName}> " command
|
|
|
|
if [[ ! "${command}" =~ ^/ ]]; then
|
|
|
|
post_status "${command}"
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
case "${command}" in
|
|
|
|
"/exit"|"/quit")
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
systemMessage="Error: '${command}' is not a valid command."
|
|
|
|
play_sound error
|
|
|
|
;;
|
|
|
|
esac
|
2023-01-30 18:57:42 -05:00
|
|
|
done
|
|
|
|
|
|
|
|
|
2023-01-28 13:19:59 -05:00
|
|
|
exit 0
|