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) |
|
13 |
sizes=(small 250 500 large 1200) |
|
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 | ||
19 |
if ! test -d $medias; then |
|
20 |
ERROR "$medias n'est pas un répertoire" |
|
21 |
exit 1 |
|
22 |
fi |
|
ajout script de récupération...
|
23 | |
24 |
sql_request () { |
|
25 |
sqlite3 $DB <<< "$1" |
|
26 |
} |
|
27 | ||
28 |
covered () { |
|
corrige l'obligation de mett...
|
29 |
case "${forcing_level:-none}" in |
ajout script de récupération...
|
30 |
retry) |
profonde ré-écriture
|
31 |
if test -w "${album#*:}/$no_cover_flag"; then |
ajout script de récupération...
|
32 |
rm -f "${album#*:}/$no_cover_flag" 2>/dev/null |
33 |
return 1 |
|
34 |
else |
|
35 |
return 0 |
|
36 |
fi |
|
37 |
;; |
|
38 |
force) |
|
profonde ré-écriture
|
39 |
rm -f "${album#*:}/$no_cover_flag" 2>/dev/null |
ajout script de récupération...
|
40 |
return 1 |
41 |
;; |
|
corrige l'obligation de mett...
|
42 |
none) |
profonde ré-écriture
|
43 |
if compgen -G "${album#*:}/$cover_img.*" > /dev/null; then |
44 |
return 0 |
|
45 |
elif test -e "${album#*:}/$no_cover_flag"; then |
|
46 |
return 0 |
|
47 |
else |
|
48 |
return 1 |
|
49 |
fi |
|
ajout script de récupération...
|
50 |
;; |
51 |
esac |
|
52 |
} |
|
53 | ||
54 |
get_image () { |
|
55 |
if curl -Ls $1 > /dev/shm/$2; then |
|
56 |
mime_type=$(file -bn --mime-type /dev/shm/$2) |
|
57 |
if [[ ${mime_type:-erreur} =~ ^image/ ]]; then |
|
58 |
mv -f /dev/shm/$2 ${album#*:}/$cover_img.${2##*.} \ |
|
59 |
|| ERROR "échec en écriture" |
|
60 |
else |
|
61 |
WARNING "${album%%:*} type $mime_type" |
|
62 |
fi |
|
63 |
else |
|
64 |
ERROR "${album%%:*} échec du téléchargement" |
|
65 |
fi |
|
66 |
} |
|
67 | ||
68 |
if test -n "$spotifyID" -a -n "$spotifySecret"; then |
|
69 |
spotify_access_token=$(curl --silent \ |
|
70 |
--request POST \ |
|
71 |
--url https://accounts.spotify.com/api/token \ |
|
72 |
--header 'Content-Type: application/x-www-form-urlencoded' \ |
|
73 |
--header "Authorization: Basic $(base64 -w0 <<< $spotifyID:$spotifySecret | sed 's/K$/=/')" \ |
|
74 |
-d 'grant_type=client_credentials' | jq -r '.access_token // empty') |
|
75 |
if test -z "$spotify_access_token"; then |
|
76 |
ERROR "problème d'identifant Spotify" |
|
77 |
echo ID: $spotifyID |
|
78 |
exit 1 |
|
79 |
fi |
|
80 |
fi |
|
81 | ||
82 |
if declare -f pre_get_cover > /dev/null; then |
|
83 |
pre_get_cover |
|
84 |
fi |
|
profonde ré-écriture
|
85 | |
ajout script de récupération...
|
86 |
IFS=$'\n' |
87 |
for arg in $@; do |
|
88 |
if [[ $arg =~ ^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}:/ ]]; then |
|
profonde ré-écriture
|
89 |
from_CLI=1 |
90 |
albums[${#albums[@]}]="${arg%/}" |
|
91 |
elif [[ $arg =~ ^/ ]]; then |
|
92 |
from_CLI=1 |
|
93 |
albums[${#albums[@]}]=":${arg%/}" |
|
corrige l'obligation de mett...
|
94 |
elif [[ $arg =~ ^(force|retry)$ ]]; then |
95 |
forcing_level=$arg |
|
96 |
else |
|
quelques messages supplément...
|
97 |
WARNING "incohérence sur $arg" |
ajout script de récupération...
|
98 |
fi |
99 |
done |
|
profonde ré-écriture
|
100 |
if test -n "$from_CLI"; then |
101 |
forcing_level=force |
|
102 |
fi |
|
103 | ||
ajout script de récupération...
|
104 |
if test ${#albums[@]} -eq 0; then |
profonde ré-écriture
|
105 |
albums=($(sql_request 'select printf("%s:'${medias%/}'/%s/%s", mbz_album_id, artist, name) |
106 |
from album where cover_art_path = "" |
|
107 |
and name != "[Unknown Album]"')) |
|
ajout script de récupération...
|
108 |
fi |
109 | ||
profonde ré-écriture
|
110 |
get_from_mbz () { |
111 |
curl -Ls $coverartarchive_api/${album%%:*} > $coverart |
|
112 |
if test $(file -bn --mime-type $coverart) = application/json; then |
|
113 |
unset img |
|
114 |
for size in ${sizes[@]}; do |
|
115 |
img=$(jq -r '.images | .[] | select(.front == true) | .thumbnails | ."'$size'"?' $coverart 2>/dev/null) |
|
116 |
test -n "$img" && break |
|
117 |
done |
|
118 |
if test -n "$img"; then |
|
119 |
get_image "$img" "${album%%:*}.${img##*.}" |
|
120 |
fi |
|
121 |
else |
|
122 |
WARNING "${album%%:*} réponse inattendue de musicbrainz" |
|
123 |
fi |
|
124 |
} |
|
125 | ||
126 |
get_from_spotify () { |
|
127 |
eval "$(sql_request 'select printf("artist=""%s"";album_name=""%s""", artist, name) |
|
128 |
from album |
|
129 |
where artist || "/" || name = "'${album#*:$medias/}'"')" |
|
130 |
if test -n "$album_name" -a -n "$artist"; then |
|
131 |
curl --request GET --silent \ |
|
132 |
--header "Authorization: Bearer $spotify_access_token" \ |
|
133 |
--header 'Content-Type: application/json' \ |
|
134 |
--url "https://api.spotify.com/v1/search?type=album&query=album:${album_name// /%20}${artist:+%20artist:${artist// /%20}}" > $coverart |
|
135 |
found_albums=$(jq -r '.albums .total // empty' $coverart) |
|
136 |
if test ${found_albums:-0} -gt 0; then |
|
137 |
img=$(jq --raw-output --arg artist "${artist^^}" --arg album "${album_name^^}" " |
|
138 |
.albums .items |
|
139 |
| .[] |
|
140 |
| select(.name | ascii_upcase == \$album) |
|
141 |
| select(.artists | .[].name | ascii_upcase == \$artist) |
|
142 |
| .images |
|
143 |
| .[] |
|
144 |
| select(.height == 300) |
|
145 |
| .url // empty" $coverart) |
|
146 |
if test -n "$img"; then |
|
147 |
get_image "$img" "${img##*/}.jpg" |
|
148 |
else |
|
149 |
WARNING "pas d'image trouvée sur spotify (artist: $artist; album: $album_name)" |
|
150 |
fi |
|
151 |
else |
|
152 |
WARNING "aucun album trouvé sur spotify" |
|
153 |
fi |
|
154 |
else |
|
155 |
ERROR "album: $album_name; artist: $artist" |
|
156 |
fi |
|
157 |
} |
|
158 | ||
ajout script de récupération...
|
159 |
for album in ${albums[@]}; do |
quelques messages supplément...
|
160 |
if ! test -d "${album#*:}"; then |
profonde ré-écriture
|
161 |
if test -n "$from_CLI"; then |
162 |
WARNING "${album#*:}: chemin inconnu" |
|
163 |
fi |
|
quelques messages supplément...
|
164 |
continue |
165 |
fi |
|
corrige l'obligation de mett...
|
166 |
if ! covered; then |
ajout script de récupération...
|
167 |
echo -n "${album#*:} " |
profonde ré-écriture
|
168 |
if test -n "${album%%:*}"; then |
169 |
if get_from_mbz; then |
|
170 |
OK |
|
171 |
elif test -n "$spotify_access_token"; then |
|
172 |
if get_from_spotify; then |
|
173 |
OK |
|
ajout script de récupération...
|
174 |
else |
profonde ré-écriture
|
175 |
touch "${album#*:}/$no_cover_flag" |
ajout script de récupération...
|
176 |
fi |
177 |
fi |
|
profonde ré-écriture
|
178 |
elif test -n "$spotify_access_token"; then |
179 |
if get_from_spotify; then |
|
180 |
OK |
|
181 |
else |
|
182 |
touch "${album#*:}/$no_cover_flag" |
|
183 |
fi |
|
ajout script de récupération...
|
184 |
fi |
185 |
fi |
|
186 |
done |
|
profonde ré-écriture
|
187 | |
ajout script de récupération...
|
188 |
rm -f $coverart |
189 |
if declare -f post_get_cover > /dev/null; then |
|
190 |
post_get_cover |
|
191 |
fi |