ajout script de récupération...
|
1 |
#!/bin/bash |
2 | ||
3 |
DB=/var/lib/navidrome/navidrome.db |
|
4 |
no_cover_flag=.no_cover |
|
5 |
cover_img=cover |
|
6 |
mbz_agent='getCover/0.1 (https://seb.lautre.net/git/seb/scripts)' |
|
7 | ||
8 |
test -e $0.conf && source $0.conf |
|
9 | ||
10 |
coverartarchive_api="https://coverartarchive.org/release" |
|
11 |
coverart=$(mktemp --dry-run /dev/shm/XXXXXXXX) |
|
12 |
sizes=(small 250 500 large 1200) |
|
13 | ||
14 |
ERROR () { echo -e "\e[3;31m ${1:-erreur} \e[0;m"; } |
|
15 |
OK () { echo -e "\e[3;32m ${1:-OK} \e[0;m"; } |
|
16 |
WARNING () { echo -e "\e[3;33m ${1:-alerte} \e[0;m"; } |
|
17 | ||
18 |
sql_request () { |
|
19 |
sqlite3 $DB <<< "$1" |
|
20 |
} |
|
21 | ||
22 |
covered () { |
|
23 |
case "$1" in |
|
24 |
retry) |
|
25 |
if compgen -G "${album#*:}/$no_cover_flag" > /dev/null; then |
|
26 |
rm -f "${album#*:}/$no_cover_flag" 2>/dev/null |
|
27 |
return 1 |
|
28 |
else |
|
29 |
return 0 |
|
30 |
fi |
|
31 |
;; |
|
32 |
force) |
|
33 |
return 1 |
|
34 |
;; |
|
35 |
empty) |
|
36 |
compgen -G "${album#*:}/$cover_img.*" > /dev/null \ |
|
37 |
|| compgen -G "${album#*:}/$no_cover_flag" > /dev/null |
|
38 |
;; |
|
39 |
esac |
|
40 |
} |
|
41 | ||
42 |
get_image () { |
|
43 |
if curl -Ls $1 > /dev/shm/$2; then |
|
44 |
mime_type=$(file -bn --mime-type /dev/shm/$2) |
|
45 |
if [[ ${mime_type:-erreur} =~ ^image/ ]]; then |
|
46 |
mv -f /dev/shm/$2 ${album#*:}/$cover_img.${2##*.} \ |
|
47 |
&& OK \ |
|
48 |
|| ERROR "échec en écriture" |
|
49 |
else |
|
50 |
WARNING "${album%%:*} type $mime_type" |
|
51 |
fi |
|
52 |
else |
|
53 |
ERROR "${album%%:*} échec du téléchargement" |
|
54 |
fi |
|
55 |
} |
|
56 | ||
57 |
if test -n "$spotifyID" -a -n "$spotifySecret"; then |
|
58 |
spotify_access_token=$(curl --silent \ |
|
59 |
--request POST \ |
|
60 |
--url https://accounts.spotify.com/api/token \ |
|
61 |
--header 'Content-Type: application/x-www-form-urlencoded' \ |
|
62 |
--header "Authorization: Basic $(base64 -w0 <<< $spotifyID:$spotifySecret | sed 's/K$/=/')" \ |
|
63 |
-d 'grant_type=client_credentials' | jq -r '.access_token // empty') |
|
64 |
if test -z "$spotify_access_token"; then |
|
65 |
ERROR "problème d'identifant Spotify" |
|
66 |
echo ID: $spotifyID |
|
67 |
exit 1 |
|
68 |
fi |
|
69 |
fi |
|
70 | ||
71 |
if declare -f pre_get_cover > /dev/null; then |
|
72 |
pre_get_cover |
|
73 |
fi |
|
74 |
IFS=$'\n' |
|
75 |
for arg in $@; do |
|
76 |
if [[ $arg =~ ^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}:/ ]]; then |
|
77 |
albums[${#albums[@]}]=$arg |
|
quelques messages supplément...
|
78 |
elif ! [[ $arg =~ ^(force|retry)$ ]]; then |
79 |
WARNING "incohérence sur $arg" |
|
ajout script de récupération...
|
80 |
fi |
81 |
done |
|
82 |
if test ${#albums[@]} -eq 0; then |
|
83 |
albums=($(sql_request "select mbz_album_id || ':' || path |
|
84 |
from media_file |
|
85 |
where mbz_album_id is not '' |
|
86 |
and path not like '/media/musique/la mule/%' |
|
87 |
and has_cover_art = FALSE" | sed -r 's|/[^/]+$||' | sort -u)) |
|
88 |
fi |
|
89 | ||
90 |
for album in ${albums[@]}; do |
|
quelques messages supplément...
|
91 |
if ! test -d "${album#*:}"; then |
92 |
WARNING "${album#*:}: chemin inconnu" |
|
93 |
continue |
|
94 |
fi |
|
95 |
if ! covered ${1:-empty}; then |
|
ajout script de récupération...
|
96 |
echo -n "${album#*:} " |
97 |
curl -Ls $coverartarchive_api/${album%%:*} > $coverart |
|
98 |
if test $(file -bn --mime-type $coverart) = application/json; then |
|
99 |
unset img |
|
100 |
for size in ${sizes[@]}; do |
|
101 |
img=$(jq -r '.images | .[] | select(.front == true) | .thumbnails | ."'$size'"?' $coverart 2>/dev/null) |
|
102 |
test -n "$img" && break |
|
103 |
done |
|
104 |
if test -n "$img"; then |
|
105 |
get_image "$img" "${album%%:*}.${img##*.}" |
|
106 |
fi |
|
107 |
elif test -n "$spotify_access_token"; then |
|
108 |
curl --user-agent "$mbz_agent" --silent \ |
|
109 |
"https://musicbrainz.org/ws/2/release/${album%%:*}?fmt=json&inc=artists" > $coverart |
|
110 |
artist=$(jq -r '."artist-credit"? | .[0].name // empty' $coverart) |
|
111 |
if [[ ${artist:-__erreur__} = @(Various Artists|__erreur__) ]]; then |
|
112 |
unset artist |
|
113 |
WARNING "artiste inconnu ou multiples (compilation ?)" |
|
114 |
continue |
|
115 |
fi |
|
116 |
album_name=$(jq -r '.title' $coverart) |
|
117 |
if test -n "$album_name"; then |
|
118 |
curl --request GET --silent --show-error --http1.1 \ |
|
119 |
--header "Authorization: Bearer $spotify_access_token" \ |
|
120 |
--header 'Content-Type: application/json' \ |
|
121 |
--url "https://api.spotify.com/v1/search?type=album&query=album:${album_name// /%20}${artist:+%20artist:${artist// /%20}}" > $coverart |
|
122 |
found_albums=$(jq -r '.albums .total // empty' $coverart) |
|
123 |
if test ${found_albums:-0} -gt 0; then |
|
124 |
img=$(jq --raw-output --arg artist "$artist" --arg album "$album_name" " |
|
125 |
.albums .items |
|
126 |
| .[] |
|
127 |
| select(.name == \$album${artist:+ and (.artists | .[].name == \$artist)}) |
|
128 |
| .images |
|
129 |
| .[] |
|
130 |
| select(.height == 300) |
|
131 |
| .url // empty" $coverart) |
|
132 |
if test -n "$img"; then |
|
133 |
get_image "$img" "${img##*/}.jpg" |
|
134 |
else |
|
135 |
touch "${album#*:}/$no_cover_flag" |
|
136 |
WARNING "${album%%:*} aucune image trouvée" |
|
137 |
fi |
|
138 |
elif test ${found_albums:-1} -eq 0; then |
|
139 |
WARNING "album indisponible sur spotify" |
|
140 |
else |
|
141 |
WARNING "${found_albums:-NULL} albums trouvés sur spotify" |
|
142 |
echo $artist |
|
143 |
echo $album_name |
|
144 |
fi |
|
145 |
fi |
|
146 |
else |
|
147 |
touch "${album#*:}/$no_cover_flag" |
|
148 |
WARNING "${album%%:*} aucune image trouvée" |
|
149 |
fi |
|
150 |
fi |
|
151 |
done |
|
152 |
rm -f $coverart |
|
153 |
if declare -f post_get_cover > /dev/null; then |
|
154 |
post_get_cover |
|
155 |
fi |