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 |
coverart=$(mktemp --dry-run /dev/shm/XXXXXXXX) |
|
vignettes plus grandes de pr...
|
12 |
sizes=(500 large small 250 1200) |
ajout script de récupération...
|
13 | |
profonde ré-écriture
|
14 |
OK () { echo -e "\e[3;32m ${1:-OK} \e[0;m"; return 0; } |
15 |
WARNING () { echo -e "\e[3;33m ${1:-alerte} \e[0;m"; return 1; } |
|
16 |
ERROR () { echo -e "\e[3;31m ${1:-erreur} \e[0;m"; return 1; } |
|
17 | ||
ajout script de récupération...
|
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) |
profonde ré-écriture
|
25 |
if test -w "${album#*:}/$no_cover_flag"; then |
ajout script de récupération...
|
26 |
rm -f "${album#*:}/$no_cover_flag" 2>/dev/null |
27 |
return 1 |
|
28 |
else |
|
29 |
return 0 |
|
30 |
fi |
|
31 |
;; |
|
32 |
force) |
|
profonde ré-écriture
|
33 |
rm -f "${album#*:}/$no_cover_flag" 2>/dev/null |
ajout script de récupération...
|
34 |
return 1 |
35 |
;; |
|
corrige l'obligation de mett...
|
36 |
none) |
profonde ré-écriture
|
37 |
if compgen -G "${album#*:}/$cover_img.*" > /dev/null; then |
38 |
return 0 |
|
39 |
elif test -e "${album#*:}/$no_cover_flag"; then |
|
40 |
return 0 |
|
41 |
else |
|
42 |
return 1 |
|
43 |
fi |
|
ajout script de récupération...
|
44 |
;; |
45 |
esac |
|
46 |
} |
|
47 | ||
48 |
get_image () { |
|
retire spotify + fix images ...
|
49 |
install_image () { |
50 |
mv -f /dev/shm/$2 ${album#*:}/$cover_img.${2##*.} \ |
|
51 |
&& OK \ |
|
52 |
|| ERROR "échec en écriture" |
|
53 |
} |
|
54 | ||
ajout script de récupération...
|
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 |
|
retire spotify + fix images ...
|
58 |
read -t 30 -p "${album%%:*} check ? " |
59 |
rc=$? |
|
60 |
if [[ "$rc$REPLY" =~ 0[OoYy] ]]; then |
|
61 |
display /dev/shm/$2 |
|
62 |
read -p "on garde ? " |
|
63 |
if [[ "$REPLY" =~ [OoYy] ]]; then |
|
64 |
install_image |
|
65 |
else |
|
66 |
rm /dev/shm/$2 |
|
67 |
WARNING "vignette rejetée" |
|
68 |
fi |
|
69 |
else |
|
70 |
install_image |
|
71 |
fi |
|
ajout script de récupération...
|
72 |
else |
73 |
WARNING "${album%%:*} type $mime_type" |
|
74 |
fi |
|
75 |
else |
|
76 |
ERROR "${album%%:*} échec du téléchargement" |
|
77 |
fi |
|
78 |
} |
|
79 | ||
profonde ré-écriture
|
80 |
get_from_mbz () { |
retire spotify + fix images ...
|
81 |
curl -Ls https://coverartarchive.org/release${1:+-group}/${album%%:*} > $coverart |
profonde ré-écriture
|
82 |
if test $(file -bn --mime-type $coverart) = application/json; then |
83 |
unset img |
|
84 |
for size in ${sizes[@]}; do |
|
85 |
img=$(jq -r '.images | .[] | select(.front == true) | .thumbnails | ."'$size'"?' $coverart 2>/dev/null) |
|
retire spotify + fix images ...
|
86 |
test "${img:-null}" != null && break |
profonde ré-écriture
|
87 |
done |
retire spotify + fix images ...
|
88 |
if test "${img:-null}" != null; then |
profonde ré-écriture
|
89 |
get_image "$img" "${album%%:*}.${img##*.}" |
90 |
fi |
|
91 |
else |
|
nouvelle réécriture
|
92 |
return 1 |
profonde ré-écriture
|
93 |
fi |
94 |
} |
|
95 | ||
retire spotify + fix images ...
|
96 |
mbz_api () { |
97 |
curl \ |
|
98 |
--request ${3:-GET} \ |
|
99 |
--user-agent "$mbz_agent" \ |
|
100 |
--silent \ |
|
101 |
--location \ |
|
102 |
--url "http://musicbrainz.org/ws/2/$1/?fmt=json&query=$2" |
|
103 |
sleep 2 |
|
profonde ré-écriture
|
104 |
} |
105 | ||
nouvelle réécriture
|
106 |
if ! test -d $medias; then |
107 |
ERROR "$medias n'est pas un répertoire" |
|
108 |
exit 1 |
|
109 |
fi |
|
110 | ||
111 |
if declare -f pre_get_cover > /dev/null; then |
|
112 |
pre_get_cover |
|
113 |
fi |
|
114 | ||
115 |
IFS=$'\n' |
|
116 |
for arg in $@; do |
|
117 |
if [[ $arg =~ ^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}:/ ]]; then |
|
118 |
from_CLI=1 |
|
119 |
albums[${#albums[@]}]="${arg%/}" |
|
120 |
elif [[ $arg =~ ^/ ]]; then |
|
121 |
from_CLI=1 |
|
122 |
albums[${#albums[@]}]=":${arg%/}" |
|
retire spotify + fix images ...
|
123 |
elif [[ $arg =~ / ]]; then |
124 |
from_CLI=1 |
|
125 |
albums[${#albums[@]}]=$(sql_request 'select mbz_album_id||":"||path |
|
126 |
from media_file |
|
127 |
where artist||"/"||album = "'$arg'"' \ |
|
128 |
| sed -r 's|/[^/]+*$||' \ |
|
129 |
| uniq \ |
|
130 |
| tail -1) |
|
nouvelle réécriture
|
131 |
elif [[ $arg =~ ^(force|retry)$ ]]; then |
132 |
forcing_level=$arg |
|
133 |
else |
|
134 |
WARNING "incohérence sur $arg" |
|
135 |
fi |
|
136 |
done |
|
retire spotify + fix images ...
|
137 | |
nouvelle réécriture
|
138 |
if test -n "$from_CLI"; then |
139 |
forcing_level=force |
|
140 |
fi |
|
141 | ||
142 |
if test ${#albums[@]} -eq 0; then |
|
143 |
albums=($(sql_request 'select printf("%s:'${medias%/}'/%s/%s", mbz_album_id, artist, name) |
|
144 |
from album where cover_art_path = "" |
|
145 |
and name != "[Unknown Album]"')) |
|
146 |
fi |
|
147 | ||
ajout script de récupération...
|
148 |
for album in ${albums[@]}; do |
nouvelle réécriture
|
149 |
mbz_related=0 |
quelques messages supplément...
|
150 |
if ! test -d "${album#*:}"; then |
profonde ré-écriture
|
151 |
if test -n "$from_CLI"; then |
152 |
WARNING "${album#*:}: chemin inconnu" |
|
153 |
fi |
|
quelques messages supplément...
|
154 |
continue |
155 |
fi |
|
nouvelle réécriture
|
156 |
if covered; then |
157 |
continue |
|
158 |
fi |
|
retire spotify + fix images ...
|
159 |
echo "${album#*:}" |
160 | ||
161 |
if test -z "${album%%:*}"; then |
|
162 |
mbz_album_id=$(sql_request "select distinct mbz_album_id |
|
163 |
from media_file |
|
164 |
where path like '${album#*:}/%'") |
|
165 |
album=${mbz_album_id}${album} |
|
166 |
fi |
|
nouvelle réécriture
|
167 |
if test -n "${album%%:*}"; then |
168 |
mbz_related=1 |
|
169 |
if get_from_mbz; then |
|
170 |
continue |
|
retire spotify + fix images ...
|
171 |
else |
172 |
mbz_api release reid:${album%%:*} > $coverart |
|
173 |
rgid=$(jq --raw-output '.releases | .[] | ."release-group" .id // empty' $coverart) |
|
174 |
album="${rgid:-null}:${album#*:}" |
|
175 |
if get_from_mbz release-group; then |
|
176 |
continue |
|
177 |
fi |
|
178 | ||
179 |
if test -n "$rgid"; then |
|
180 |
mbz_api release-group rgid:$rgid > $coverart |
|
181 |
mbz_album_ids=($(jq --raw-output '."release-groups" | .[].releases | .[].id // empty' $coverart)) |
|
182 | ||
183 |
for reid in ${mbz_album_ids[@]}; do |
|
184 |
album=$reid:${album#*:} |
|
185 |
if get_from_mbz; then |
|
186 |
continue 2 |
|
187 |
fi |
|
188 |
done |
|
189 |
fi |
|
nouvelle réécriture
|
190 |
fi |
191 |
fi |
|
192 | ||
193 |
eval "$(sql_request 'select printf("artist=""%s"";album_name=""%s""", artist, name) |
|
194 |
from album |
|
195 |
where artist || "/" || name = "'${album#*:$medias/}'"')" |
|
196 |
eval $(php -r 'echo "_artist=".rawurlencode($argv[1]).";_album_name=".rawurlencode($argv[2]);' -- "$artist" "$album_name") |
|
197 | ||
retire spotify + fix images ...
|
198 |
mbz_api release "release:$_album_name%20AND%20artist:$_artist" > $coverart |
nouvelle réécriture
|
199 | |
200 |
mbids=($(jq --raw-output --arg artist "${artist^^}" --arg album "${album_name^^}" ' |
|
201 |
.releases |
|
202 |
| .[] |
|
203 |
| select(.title | ascii_upcase == $album) |
|
204 |
| select(."artist-credit" | .[].name | ascii_upcase == $artist) |
|
205 |
| .id // empty' $coverart)) |
|
206 | ||
207 |
for mbid in ${mbids[@]}; do |
|
208 |
album="$mbid:${album#*:}" |
|
209 |
if get_from_mbz; then |
|
210 |
continue 2 |
|
ajout script de récupération...
|
211 |
fi |
nouvelle réécriture
|
212 |
done |
213 | ||
retire spotify + fix images ...
|
214 |
WARNING "pas trouvé ($(( ${mbz_related:-0} + ${#mbids[@]} )) relations sur musicbrainz)" |
215 |
touch "${album#*:}/$no_cover_flag" |
|
ajout script de récupération...
|
216 |
done |
profonde ré-écriture
|
217 | |
ajout script de récupération...
|
218 |
rm -f $coverart |
219 |
if declare -f post_get_cover > /dev/null; then |
|
220 |
post_get_cover |
|
221 |
fi |