funkblubber/funkblubber

102 lines
2.6 KiB
Bash
Executable File

#!/bin/bash
errecho()
{
echo -e "\e[0;31m${1}\e[0m" 1>&2
}
getalbum()
{
aldum_id=$1
domain=$2
response=$(curl -X GET "https://${domain}/api/v1/tracks/?album=${album_id}&ordering=creation_date&page=1&page_size=16&scope=all" -H "accept: application/json")
echo "${response}" | jq '.results' | jq -c '.[]' | while read song
do
echo "${song}" | jq '.uploads' | jq -c '.[]' | while read upload
do
extension=$(echo ${upload} | jq -r '.extension')
folder=$(echo ${song} | jq -r '.album.title' | sed 's#/#_#g')
mkdir -p "${folder}"
title=$(echo ${song} | jq -r '.title' | sed 's#/#_#g')
listen_url=$(echo ${song} | jq -r '.listen_url')
echo "https://${domain}${listen_url}"
echo " - '${title}'"
echo " - $extension"
curl -X GET "https://${domain}${listen_url}" -H "accept: */*" -o "${folder}/${title}.${extension}"
break
done
done
}
getartist()
{
artist_id=$1
domain=$2
response=$(curl -X GET "https://${domain}/api/v1/albums/?artist=${artist_id}&ordering=creation_date&page=1&page_size=16&scope=all" -H "accept: application/json")
echo "${response}" | jq '.results' | jq -c '.[]' | while read album
do
album_id=$(echo "${album}" | jq -r '.id')
album_title=$(echo "${album}" | jq -r '.title')
echo " - Album: ${album_title}"
getalbum $album_id $domain
done
}
for dep in jq curl
do
if [ -z $(which $dep) ]
then
errecho "Missing $dep package, please install"
exit 1
fi
done
if [ -z ${1} ]
then
errecho "Provide a URL"
errecho "'$0 --help' for more"
exit 1
fi
domain=$(echo "$1" | cut -f 3 -d '/')
echo "Parsing $domain"
album_flag="albums"
artist_flag="artists"
look_for_id_of=""
IFS='/' read -ra chunks <<< "$1"
for chunk in "${chunks[@]}"
do
case $chunk in
$album_flag)
echo "Parsing album"
look_for_id_of=$album_flag
continue
;;
$artist_flag)
echo -n "Parsing artist"
look_for_id_of=$artist_flag
continue
;;
esac
case $look_for_id_of in
$album_flag)
echo "Get ID ${chunk}"
getalbum $chunk $domain
exit 0
;;
$artist_flag)
echo "Get ID ${chunk}"
getartist $chunk $domain
exit 0
;;
esac
done
errecho "Invalid URL"
errecho "Should be either https://<domain>/library/albums/<ID>/ or https://<domain>/library/artists/<ID>/"
errecho "'$0 --help' for more"