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 () { |
|
corrige l'obligation de mett...
|
23 |
case "${forcing_level:-none}" in |
ajout script de récupération...
|
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 |
;; |
|
corrige l'obligation de mett...
|
35 |
none) |
ajout script de récupération...
|
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 |
|
corrige l'obligation de mett...
|
78 |
elif [[ $arg =~ ^(force|retry)$ ]]; then |
79 |
forcing_level=$arg |
|
80 |
else |
|
quelques messages supplément...
|
81 |
WARNING "incohérence sur $arg" |
ajout script de récupération...
|
82 |
fi |
83 |
done |
|
84 |
if test ${#albums[@]} -eq 0; then |
|
85 |
albums=($(sql_request "select mbz_album_id || ':' || path |
|
86 |
from media_file |
|
87 |
where mbz_album_id is not '' |
|
88 |
and path not like '/media/musique/la mule/%' |
|
89 |
and has_cover_art = FALSE" | sed -r 's|/[^/]+$||' | sort -u)) |
|
90 |
fi |
|
91 | ||
92 |
for album in ${albums[@]}; do |
|
quelques messages supplément...
|
93 |
if ! test -d "${album#*:}"; then |
94 |
WARNING "${album#*:}: chemin inconnu" |
|
95 |
continue |
|
96 |
fi |
|
corrige l'obligation de mett...
|
97 |
if ! covered; then |
ajout script de récupération...
|
98 |
echo -n "${album#*:} " |
99 |
curl -Ls $coverartarchive_api/${album%%:*} > $coverart |
|
100 |
if test $(file -bn --mime-type $coverart) = application/json; then |
|
101 |
unset img |
|
102 |
for size in ${sizes[@]}; do |
|
103 |
img=$(jq -r '.images | .[] | select(.front == true) | .thumbnails | ."'$size'"?' $coverart 2>/dev/null) |
|
104 |
test -n "$img" && break |
|
105 |
done |
|
106 |
if test -n "$img"; then |
|
107 |
get_image "$img" "${album%%:*}.${img##*.}" |
|
108 |
fi |
|
109 |
elif test -n "$spotify_access_token"; then |
|
110 |
curl --user-agent "$mbz_agent" --silent \ |
|
111 |
"https://musicbrainz.org/ws/2/release/${album%%:*}?fmt=json&inc=artists" > $coverart |
|
112 |
artist=$(jq -r '."artist-credit"? | .[0].name // empty' $coverart) |
|
113 |
if [[ ${artist:-__erreur__} = @(Various Artists|__erreur__) ]]; then |
|
114 |
unset artist |
|
115 |
WARNING "artiste inconnu ou multiples (compilation ?)" |
|
116 |
continue |
|
117 |
fi |
|
118 |
album_name=$(jq -r '.title' $coverart) |
|
119 |
if test -n "$album_name"; then |
|
120 |
curl --request GET --silent --show-error --http1.1 \ |
|
121 |
--header "Authorization: Bearer $spotify_access_token" \ |
|
122 |
--header 'Content-Type: application/json' \ |
|
123 |
--url "https://api.spotify.com/v1/search?type=album&query=album:${album_name// /%20}${artist:+%20artist:${artist// /%20}}" > $coverart |
|
124 |
found_albums=$(jq -r '.albums .total // empty' $coverart) |
|
125 |
if test ${found_albums:-0} -gt 0; then |
|
126 |
img=$(jq --raw-output --arg artist "$artist" --arg album "$album_name" " |
|
127 |
.albums .items |
|
128 |
| .[] |
|
129 |
| select(.name == \$album${artist:+ and (.artists | .[].name == \$artist)}) |
|
130 |
| .images |
|
131 |
| .[] |
|
132 |
| select(.height == 300) |
|
133 |
| .url // empty" $coverart) |
|
134 |
if test -n "$img"; then |
|
135 |
get_image "$img" "${img##*/}.jpg" |
|
136 |
else |
|
137 |
touch "${album#*:}/$no_cover_flag" |
|
138 |
WARNING "${album%%:*} aucune image trouvée" |
|
139 |
fi |
|
140 |
elif test ${found_albums:-1} -eq 0; then |
|
141 |
WARNING "album indisponible sur spotify" |
|
142 |
else |
|
143 |
WARNING "${found_albums:-NULL} albums trouvés sur spotify" |
|
144 |
echo $artist |
|
145 |
echo $album_name |
|
146 |
fi |
|
147 |
fi |
|
148 |
else |
|
149 |
touch "${album#*:}/$no_cover_flag" |
|
150 |
WARNING "${album%%:*} aucune image trouvée" |
|
151 |
fi |
|
152 |
fi |
|
153 |
done |
|
154 |
rm -f $coverart |
|
155 |
if declare -f post_get_cover > /dev/null; then |
|
156 |
post_get_cover |
|
157 |
fi |