Skip to content

Instantly share code, notes, and snippets.

@niclashoyer
Last active May 2, 2020 08:34
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save niclashoyer/10426194 to your computer and use it in GitHub Desktop.
Save niclashoyer/10426194 to your computer and use it in GitHub Desktop.
Download a jamendo album as flac using the new v3.0 API (remember to insert your own application client id)
#!/bin/bash
# deps: bash, curl, sed, metaflac, jq
# remember to insert your own application client id (see CLIENTID below)
# to get an application client id register at http://developer.jamendo.com
set -e
set -u
function filtername() {
tr -cs "[:alnum:] \-&\(\)\?!" _
}
function j() {
echo -n "$1" | jq -r "$2"
}
CLIENTID="YOUR_CLIENT_ID"
ALBUMID=$(echo "$1" | sed -e 's/.\+\/album\/\([0-9]\+\)\/.\+$/\1/')
BASEURL="http://api.jamendo.com/v3.0"
JSON=$(curl -s "$BASEURL/albums/tracks?client_id=$CLIENTID&format=json&limit=1&id=$ALBUMID&imagesize=500&audioformat=flac")
DATA=$(j "$JSON" '.results[0]')
IMGURL=$(j "$DATA" '.image')
ARTIST=$(j "$DATA" '.artist_name')
ALBUM=$(j "$DATA" '.name')
RELEASEDATE=$(j "$DATA" '.releasedate')
ALBUMDIR=$(echo -n "$ARTIST - $ALBUM" | filtername)
TRACKS=$(j "$JSON" '.results[0].tracks')
LENGTH=$(j "$TRACKS" '.|length')
PADLENGTH=$(printf %02d $LENGTH)
echo "$ARTIST - $ALBUM"
echo "Creating directory..."
mkdir -p "$ALBUMDIR"
cd "$ALBUMDIR"
echo "Saving album cover..."
curl -s -z folder.jpg -o folder.jpg "$IMGURL"
for ((i=0; i<$LENGTH; i++)); do
TRACK=$(j "$TRACKS" ".[$i]")
NAME=$(j "$TRACK" '.name')
POS=$(printf "%02d\n" $(j "$TRACK" '.position'))
FILENAME=$(echo -n "$POS - $ARTIST - $NAME" | filtername)".flac"
FILEURL=$(j "$TRACK" '.audiodownload')
LICENSEURL=$(j "$TRACK" '.license_ccurl')
echo "Downloading \"$NAME\"..."
curl --http1.1 --progress-bar -o "$FILENAME" "$FILEURL" # FIXME: workaround for Jamendo HTTP/2 support
echo -ne "\r\033[1A\033[0K\r"
metaflac --remove-all-tags \
--set-tag="ARTIST=$ARTIST" \
--set-tag="TITLE=$NAME" \
--set-tag="ALBUM=$ALBUM" \
--set-tag="LICENSE=$LICENSEURL" \
--set-tag="COPYRIGHT=$LICENSEURL" \
--set-tag="TRACKNUMBER=$POS" \
--set-tag="TRACKTOTAL=$PADLENGTH" \
--set-tag="DATE=$RELEASEDATE" \
--set-tag="DISCNUMBER=1" \
--set-tag="CONTACT=$1" \
--import-picture-from="folder.jpg" \
"$FILENAME"
done
@vitrif
Copy link

vitrif commented Apr 14, 2020

Hi @niclashoyer , thanks for the nice script!

I encountered an error with curl ('curl: (92) HTTP/2 stream 0 was not closed cleanly: Unknown error code (err 1)').
I think it is a problem with the HTTP/2 support of jamendo's servers, this workaround fixed it for me:

curl --http1.1 --progress-bar -o "$FILENAME" "$FILEURL"

Best Regards

@niclashoyer
Copy link
Author

@vitrif thanks! It has been a while since I used this script. I added your fix. Glad to see it being useful 🎈

@vitrif
Copy link

vitrif commented Apr 14, 2020

It certainly is, thanks for the fast 'merge' 👍 I'm gonna enjoy some high-quality EDM now 😅
Viele Grüße aus Bremen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment