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