1 contributor
#!/bin/bash
DB=/var/lib/navidrome/navidrome.db
no_cover_flag=.no_cover
cover_img=cover
mbz_agent='getCover/0.1 (https://seb.lautre.net/git/seb/scripts)'
test -e $0.conf && source $0.conf
coverartarchive_api="https://coverartarchive.org/release"
coverart=$(mktemp --dry-run /dev/shm/XXXXXXXX)
sizes=(small 250 500 large 1200)
ERROR () { echo -e "\e[3;31m ${1:-erreur} \e[0;m"; }
OK () { echo -e "\e[3;32m ${1:-OK} \e[0;m"; }
WARNING () { echo -e "\e[3;33m ${1:-alerte} \e[0;m"; }
sql_request () {
sqlite3 $DB <<< "$1"
}
covered () {
case "$1" in
retry)
if compgen -G "${album#*:}/$no_cover_flag" > /dev/null; then
rm -f "${album#*:}/$no_cover_flag" 2>/dev/null
return 1
else
return 0
fi
;;
force)
return 1
;;
empty)
compgen -G "${album#*:}/$cover_img.*" > /dev/null \
|| compgen -G "${album#*:}/$no_cover_flag" > /dev/null
;;
esac
}
get_image () {
if curl -Ls $1 > /dev/shm/$2; then
mime_type=$(file -bn --mime-type /dev/shm/$2)
if [[ ${mime_type:-erreur} =~ ^image/ ]]; then
mv -f /dev/shm/$2 ${album#*:}/$cover_img.${2##*.} \
&& OK \
|| ERROR "échec en écriture"
else
WARNING "${album%%:*} type $mime_type"
fi
else
ERROR "${album%%:*} échec du téléchargement"
fi
}
if test -n "$spotifyID" -a -n "$spotifySecret"; then
spotify_access_token=$(curl --silent \
--request POST \
--url https://accounts.spotify.com/api/token \
--header 'Content-Type: application/x-www-form-urlencoded' \
--header "Authorization: Basic $(base64 -w0 <<< $spotifyID:$spotifySecret | sed 's/K$/=/')" \
-d 'grant_type=client_credentials' | jq -r '.access_token // empty')
if test -z "$spotify_access_token"; then
ERROR "problème d'identifant Spotify"
echo ID: $spotifyID
exit 1
fi
fi
if declare -f pre_get_cover > /dev/null; then
pre_get_cover
fi
IFS=$'\n'
for arg in $@; do
if [[ $arg =~ ^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}:/ ]]; then
albums[${#albums[@]}]=$arg
elif ! [[ $arg =~ ^(force|retry)$ ]]; then
WARNING "incohérence sur $arg"
fi
done
if test ${#albums[@]} -eq 0; then
albums=($(sql_request "select mbz_album_id || ':' || path
from media_file
where mbz_album_id is not ''
and path not like '/media/musique/la mule/%'
and has_cover_art = FALSE" | sed -r 's|/[^/]+$||' | sort -u))
fi
for album in ${albums[@]}; do
if ! test -d "${album#*:}"; then
WARNING "${album#*:}: chemin inconnu"
continue
fi
if ! covered ${1:-empty}; then
echo -n "${album#*:} "
curl -Ls $coverartarchive_api/${album%%:*} > $coverart
if test $(file -bn --mime-type $coverart) = application/json; then
unset img
for size in ${sizes[@]}; do
img=$(jq -r '.images | .[] | select(.front == true) | .thumbnails | ."'$size'"?' $coverart 2>/dev/null)
test -n "$img" && break
done
if test -n "$img"; then
get_image "$img" "${album%%:*}.${img##*.}"
fi
elif test -n "$spotify_access_token"; then
curl --user-agent "$mbz_agent" --silent \
"https://musicbrainz.org/ws/2/release/${album%%:*}?fmt=json&inc=artists" > $coverart
artist=$(jq -r '."artist-credit"? | .[0].name // empty' $coverart)
if [[ ${artist:-__erreur__} = @(Various Artists|__erreur__) ]]; then
unset artist
WARNING "artiste inconnu ou multiples (compilation ?)"
continue
fi
album_name=$(jq -r '.title' $coverart)
if test -n "$album_name"; then
curl --request GET --silent --show-error --http1.1 \
--header "Authorization: Bearer $spotify_access_token" \
--header 'Content-Type: application/json' \
--url "https://api.spotify.com/v1/search?type=album&query=album:${album_name// /%20}${artist:+%20artist:${artist// /%20}}" > $coverart
found_albums=$(jq -r '.albums .total // empty' $coverart)
if test ${found_albums:-0} -gt 0; then
img=$(jq --raw-output --arg artist "$artist" --arg album "$album_name" "
.albums .items
| .[]
| select(.name == \$album${artist:+ and (.artists | .[].name == \$artist)})
| .images
| .[]
| select(.height == 300)
| .url // empty" $coverart)
if test -n "$img"; then
get_image "$img" "${img##*/}.jpg"
else
touch "${album#*:}/$no_cover_flag"
WARNING "${album%%:*} aucune image trouvée"
fi
elif test ${found_albums:-1} -eq 0; then
WARNING "album indisponible sur spotify"
else
WARNING "${found_albums:-NULL} albums trouvés sur spotify"
echo $artist
echo $album_name
fi
fi
else
touch "${album#*:}/$no_cover_flag"
WARNING "${album%%:*} aucune image trouvée"
fi
fi
done
rm -f $coverart
if declare -f post_get_cover > /dev/null; then
post_get_cover
fi