#!/bin/sh

youtube-dl --add-metadata -icx --no-cache-dir --no-call-home --audio-format mp3 --audio-quality 0 --prefer-ffmpeg --no-post-overwrites --geo-bypass -o "$HOME/snd/Albums/%(artist)s/%(album)s/%(artist)s - %(album)s - %(track_number)d %(track)s.%(ext)s" "$1"

echo "Buscando la tapa..."
HTML_TEMP=$(mktemp)
curl -s "$1" > "$HTML_TEMP"
titulo=$(grep -Po "<title>.*</title>" "$HTML_TEMP" | sed 's/<[^>]*>//g')
disco=${titulo% | *}
grupo=${titulo#* | }
sed -n '/image_/s/.*rel="image_src"\s\+href="\([^"]\+\).*/\1/p' "$HTML_TEMP" | xargs -r curl -s --create-dirs -o "$HOME/snd/Albums/${grupo}/${disco}/cover.jpg" && echo "Listo" || echo "No la encontre :("
