Instantly share code, notes, and snippets.

Embed
What would you like to do?
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 --progress-bar -o "$FILENAME" "$FILEURL"
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" \
--import-picture-from="folder.jpg" \
"$FILENAME"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment