dotfiles/.config/zsh/functions/np-subsonic

31 lines
945 B
Plaintext
Raw Normal View History

2024-04-04 12:16:48 +02:00
#!/usr/bin/env zsh
# get now playing from subsonic compatible API
2024-04-04 12:51:37 +02:00
# cfg file: host\nuser\password (host=everything before /rest)
2024-04-04 12:16:48 +02:00
setopt LOCAL_OPTIONS ERR_RETURN NO_UNSET PIPE_FAIL
local -a cfg
while IFS=\n read -r line; do
cfg+=${line}
2024-04-04 12:49:31 +02:00
done < ${XDG_CONFIG_HOME:-~/.config}/np-subsonic.cfg
2024-04-04 12:16:48 +02:00
local host=${cfg[1]}
local api_version=1.16.1
2024-04-04 12:51:37 +02:00
local useragent=np-subsonic
2024-04-04 12:16:48 +02:00
local user=${cfg[2]}
local password=${cfg[3]}
local salt=$(pwgen 20 1)
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[0]' <<<${response})
local title=$(jq --raw-output .title <<< ${entry})
local artist=$(jq --raw-output .artist <<< ${entry})
if [[ "${title}" != "null" && "${artist}" != "null" ]]; then
print ${title} ${artist}
else
return 1
fi