39 lines
1.2 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env zsh
# get now playing from subsonic compatible API
# cfg file: host\nuser\npassword (host=everything before /rest)
setopt LOCAL_OPTIONS ERR_RETURN NO_UNSET PIPE_FAIL
local -a cfg
while IFS=\n read -r line; do
cfg+=${line}
done < ${XDG_CONFIG_HOME:-~/.config}/np-subsonic.cfg
local pwgen_cmd="print ${RANDOM}"
# if type pwgen >& -; then
# pwgen_cmd="pwgen 20 1"
# elif type pwqgen >& -; then
# pwgen_cmd=pwqgen
# fi
local host=${cfg[1]}
local api_version=1.16.1
local useragent=np-subsonic
local user=${cfg[2]}
local password=${cfg[3]}
local salt=$(eval ${pwgen_cmd})
local token=$(print -n "${password}${salt}" | md5sum | cut -d' ' -f1)
local response=$(curl --silent \
"${host}/rest/getNowPlaying?v=${api_version}&c=${useragent}&u=${user}&s=${salt}&t=${token}&f=json")
local entry=$(jq '."subsonic-response".nowPlaying.entry[] | select(.username == "'${user}'")' <<<${response})
local title=$(jq --raw-output .title <<< ${entry})
local artist=$(jq --raw-output .artist <<< ${entry})
if [[ "${title}" != "" && "${artist}" != "" ]] \
&& [[ "${title}" != "null" && "${artist}" != "null" ]]; then
print ${title} ${artist}
else
return 1
fi