You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.1 KiB
Bash

#!/bin/bash
errecho()
{
echo -e "\e[0;31m${1}\e[0m" 1>&2
}
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 "So your domain is $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 "Found out it's an album..."
look_for_id_of=$album_flag
continue
;;
$artist_flag)
echo -n "Found out it's an artist..."
look_for_id_of=$artist_flag
continue
;;
esac
case $look_for_id_of in
$album_flag)
echo "And its ID is ${chunk}..."
response=$(curl -X GET "https://${domain}/api/v1/tracks/?album=${chunk}&ordering=creation_date&page=1&page_size=16&scope=all" -H "accept: application/json" | jq .)
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 "Downloading '${title}'..."
curl -X GET "https://${domain}${listen_url}" -H "accept: */*" -o "${folder}/${title}.${extension}"
break
done
done
exit 0
;;
$artist_flag)
echo -n "And its ID is ${chunk}..."
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"