ajout script de récupération...
|
1 |
#!/bin/bash |
2 | ||
3 |
DB=/var/lib/navidrome/navidrome.db |
|
profonde ré-écriture
|
4 |
medias=/music |
ajout script de récupération...
|
5 |
no_cover_flag=.no_cover |
6 |
cover_img=cover |
|
7 |
mbz_agent='getCover/0.1 (https://seb.lautre.net/git/seb/scripts)' |
|
8 | ||
9 |
test -e $0.conf && source $0.conf |
|
10 | ||
11 |
coverartarchive_api="https://coverartarchive.org/release" |
|
12 |
coverart=$(mktemp --dry-run /dev/shm/XXXXXXXX) |
|
vignettes plus grandes de pr...
|
13 |
sizes=(500 large small 250 1200) |
ajout script de récupération...
|
14 | |
profonde ré-écriture
|
15 |
OK () { echo -e "\e[3;32m ${1:-OK} \e[0;m"; return 0; } |
16 |
WARNING () { echo -e "\e[3;33m ${1:-alerte} \e[0;m"; return 1; } |
|
17 |
ERROR () { echo -e "\e[3;31m ${1:-erreur} \e[0;m"; return 1; } |
|
18 | ||
ajout script de récupération...
|
19 |
sql_request () { |
20 |
sqlite3 $DB <<< "$1" |
|
21 |
} |
|
22 | ||
23 |
covered () { |
|
corrige l'obligation de mett...
|
24 |
case "${forcing_level:-none}" in |
ajout script de récupération...
|
25 |
retry) |
profonde ré-écriture
|
26 |
if test -w "${album#*:}/$no_cover_flag"; then |
ajout script de récupération...
|
27 |
rm -f "${album#*:}/$no_cover_flag" 2>/dev/null |
28 |
return 1 |
|
29 |
else |
|
30 |
return 0 |
|
31 |
fi |
|
32 |
;; |
|
33 |
force) |
|
profonde ré-écriture
|
34 |
rm -f "${album#*:}/$no_cover_flag" 2>/dev/null |
ajout script de récupération...
|
35 |
return 1 |
36 |
;; |
|
corrige l'obligation de mett...
|
37 |
none) |
profonde ré-écriture
|
38 |
if compgen -G "${album#*:}/$cover_img.*" > /dev/null; then |
39 |
return 0 |
|
40 |
elif test -e "${album#*:}/$no_cover_flag"; then |
|
41 |
return 0 |
|
42 |
else |
|
43 |
return 1 |
|
44 |
fi |
|
ajout script de récupération...
|
45 |
;; |
46 |
esac |
|
47 |
} |
|
48 | ||
49 |
get_image () { |
|
50 |
if curl -Ls $1 > /dev/shm/$2; then |
|
51 |
mime_type=$(file -bn --mime-type /dev/shm/$2) |
|
52 |
if [[ ${mime_type:-erreur} =~ ^image/ ]]; then |
|
53 |
mv -f /dev/shm/$2 ${album#*:}/$cover_img.${2##*.} \ |
|
54 |
|| ERROR "échec en écriture" |
|
55 |
else |
|
56 |
WARNING "${album%%:*} type $mime_type" |
|
57 |
fi |
|
58 |
else |
|
59 |
ERROR "${album%%:*} échec du téléchargement" |
|
60 |
fi |
|
61 |
} |
|
62 | ||
nouvelle réécriture
|
63 |
get_spotify_access_token () { |
64 |
if test -n "$spotifyID" -a -n "$spotifySecret"; then |
|
65 |
spotify_access_token=$(curl --silent \ |
|
66 |
--request POST \ |
|
67 |
--url https://accounts.spotify.com/api/token \ |
|
68 |
--header 'Content-Type: application/x-www-form-urlencoded' \ |
|
69 |
--header "Authorization: Basic $(base64 -w0 <<< $spotifyID:$spotifySecret | sed 's/K$/=/')" \ |
|
70 |
-d 'grant_type=client_credentials' | jq -r '.access_token // empty') |
|
71 |
if test -z "$spotify_access_token"; then |
|
72 |
ERROR "problème d'identifant Spotify" |
|
73 |
spotify_access_token_error=1 |
|
74 |
echo ID: $spotifyID |
|
75 |
fi |
|
ajout script de récupération...
|
76 |
fi |
nouvelle réécriture
|
77 |
} |
ajout script de récupération...
|
78 | |
profonde ré-écriture
|
79 |
get_from_mbz () { |
80 |
curl -Ls $coverartarchive_api/${album%%:*} > $coverart |
|
81 |
if test $(file -bn --mime-type $coverart) = application/json; then |
|
82 |
unset img |
|
83 |
for size in ${sizes[@]}; do |
|
84 |
img=$(jq -r '.images | .[] | select(.front == true) | .thumbnails | ."'$size'"?' $coverart 2>/dev/null) |
|
85 |
test -n "$img" && break |
|
86 |
done |
|
87 |
if test -n "$img"; then |
|
88 |
get_image "$img" "${album%%:*}.${img##*.}" |
|
89 |
fi |
|
90 |
else |
|
nouvelle réécriture
|
91 |
return 1 |
profonde ré-écriture
|
92 |
fi |
93 |
} |
|
94 | ||
95 |
get_from_spotify () { |
|
96 |
if test -n "$album_name" -a -n "$artist"; then |
|
97 |
curl --request GET --silent \ |
|
98 |
--header "Authorization: Bearer $spotify_access_token" \ |
|
99 |
--header 'Content-Type: application/json' \ |
|
nouvelle réécriture
|
100 |
--data 'type=album' \ |
101 |
--data "query==album:${_album_name}%20artist:${_artist}" \ |
|
102 |
--url "https://api.spotify.com/v1/search" > $coverart |
|
profonde ré-écriture
|
103 |
found_albums=$(jq -r '.albums .total // empty' $coverart) |
104 |
if test ${found_albums:-0} -gt 0; then |
|
105 |
img=$(jq --raw-output --arg artist "${artist^^}" --arg album "${album_name^^}" " |
|
106 |
.albums .items |
|
107 |
| .[] |
|
108 |
| select(.name | ascii_upcase == \$album) |
|
109 |
| select(.artists | .[].name | ascii_upcase == \$artist) |
|
110 |
| .images |
|
111 |
| .[] |
|
112 |
| select(.height == 300) |
|
113 |
| .url // empty" $coverart) |
|
114 |
if test -n "$img"; then |
|
115 |
get_image "$img" "${img##*/}.jpg" |
|
116 |
else |
|
117 |
WARNING "pas d'image trouvée sur spotify (artist: $artist; album: $album_name)" |
|
118 |
fi |
|
119 |
else |
|
120 |
WARNING "aucun album trouvé sur spotify" |
|
121 |
fi |
|
122 |
else |
|
123 |
ERROR "album: $album_name; artist: $artist" |
|
124 |
fi |
|
125 |
} |
|
126 | ||
nouvelle réécriture
|
127 |
if ! test -d $medias; then |
128 |
ERROR "$medias n'est pas un répertoire" |
|
129 |
exit 1 |
|
130 |
fi |
|
131 | ||
132 |
if declare -f pre_get_cover > /dev/null; then |
|
133 |
pre_get_cover |
|
134 |
fi |
|
135 | ||
136 |
IFS=$'\n' |
|
137 |
for arg in $@; do |
|
138 |
if [[ $arg =~ ^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}:/ ]]; then |
|
139 |
from_CLI=1 |
|
140 |
albums[${#albums[@]}]="${arg%/}" |
|
141 |
elif [[ $arg =~ ^/ ]]; then |
|
142 |
from_CLI=1 |
|
143 |
albums[${#albums[@]}]=":${arg%/}" |
|
144 |
elif [[ $arg =~ ^(force|retry)$ ]]; then |
|
145 |
forcing_level=$arg |
|
146 |
else |
|
147 |
WARNING "incohérence sur $arg" |
|
148 |
fi |
|
149 |
done |
|
150 |
if test -n "$from_CLI"; then |
|
151 |
forcing_level=force |
|
152 |
fi |
|
153 | ||
154 |
if test ${#albums[@]} -eq 0; then |
|
155 |
albums=($(sql_request 'select printf("%s:'${medias%/}'/%s/%s", mbz_album_id, artist, name) |
|
156 |
from album where cover_art_path = "" |
|
157 |
and name != "[Unknown Album]"')) |
|
158 |
fi |
|
159 | ||
ajout script de récupération...
|
160 |
for album in ${albums[@]}; do |
nouvelle réécriture
|
161 |
mbz_related=0 |
quelques messages supplément...
|
162 |
if ! test -d "${album#*:}"; then |
profonde ré-écriture
|
163 |
if test -n "$from_CLI"; then |
164 |
WARNING "${album#*:}: chemin inconnu" |
|
165 |
fi |
|
quelques messages supplément...
|
166 |
continue |
167 |
fi |
|
nouvelle réécriture
|
168 |
if covered; then |
169 |
continue |
|
170 |
fi |
|
171 |
echo -n "${album#*:} " |
|
172 |
if test -n "${album%%:*}"; then |
|
173 |
mbz_related=1 |
|
174 |
if get_from_mbz; then |
|
175 |
OK |
|
176 |
continue |
|
177 |
fi |
|
178 |
fi |
|
179 | ||
180 |
eval "$(sql_request 'select printf("artist=""%s"";album_name=""%s""", artist, name) |
|
181 |
from album |
|
182 |
where artist || "/" || name = "'${album#*:$medias/}'"')" |
|
183 |
eval $(php -r 'echo "_artist=".rawurlencode($argv[1]).";_album_name=".rawurlencode($argv[2]);' -- "$artist" "$album_name") |
|
184 | ||
185 |
curl \ |
|
186 |
--request GET \ |
|
187 |
--user-agent "$mbz_agent" \ |
|
188 |
--silent --location \ |
|
189 |
--url "http://musicbrainz.org/ws/2/release/?fmt=json&query=release:$_album_name%20AND%20artist:$_artist" > $coverart |
|
190 | ||
191 |
mbids=($(jq --raw-output --arg artist "${artist^^}" --arg album "${album_name^^}" ' |
|
192 |
.releases |
|
193 |
| .[] |
|
194 |
| select(.title | ascii_upcase == $album) |
|
195 |
| select(."artist-credit" | .[].name | ascii_upcase == $artist) |
|
196 |
| .id // empty' $coverart)) |
|
197 | ||
198 |
for mbid in ${mbids[@]}; do |
|
199 |
album="$mbid:${album#*:}" |
|
200 |
if get_from_mbz; then |
|
201 |
OK |
|
202 |
continue 2 |
|
ajout script de récupération...
|
203 |
fi |
nouvelle réécriture
|
204 |
done |
205 | ||
206 |
if test -z "$spotify_access_token" -a -z "$spotify_access_token_error"; then |
|
207 |
get_spotify_access_token |
|
208 |
fi |
|
209 | ||
210 |
if test -n "$spotify_access_token"; then |
|
211 |
if get_from_spotify; then |
|
212 |
OK |
|
213 |
else |
|
214 |
WARNING "pas trouvé (spotify)" |
|
215 |
touch "${album#*:}/$no_cover_flag" |
|
216 |
fi |
|
217 |
else |
|
218 |
WARNING "pas trouvé ($(( ${mbz_related:-0} + ${#mbids[@]} )) relations sur musicbrainz)" |
|
219 |
touch "${album#*:}/$no_cover_flag" |
|
ajout script de récupération...
|
220 |
fi |
221 |
done |
|
profonde ré-écriture
|
222 | |
ajout script de récupération...
|
223 |
rm -f $coverart |
224 |
if declare -f post_get_cover > /dev/null; then |
|
225 |
post_get_cover |
|
226 |
fi |