Showing 1 changed files with 103 additions and 68 deletions
+103 -68
get_cover
... ...
@@ -16,11 +16,6 @@ OK      () { echo -e "\e[3;32m ${1:-OK} \e[0;m";     return 0; }
16 16
 WARNING () { echo -e "\e[3;33m ${1:-alerte} \e[0;m"; return 1; }
17 17
 ERROR   () { echo -e "\e[3;31m ${1:-erreur} \e[0;m"; return 1; }
18 18
 
19
-if ! test -d $medias; then
20
-    ERROR "$medias n'est pas un répertoire"
21
-    exit 1
22
-fi
23
-
24 19
 sql_request () {
25 20
     sqlite3 $DB <<< "$1"
26 21
 }
... ...
@@ -65,47 +60,21 @@ get_image () {
65 60
     fi
66 61
 }
67 62
 
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
85
-
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
89
-        from_CLI=1
90
-        albums[${#albums[@]}]="${arg%/}"
91
-    elif [[ $arg =~ ^/ ]]; then
92
-        from_CLI=1
93
-        albums[${#albums[@]}]=":${arg%/}"
94
-    elif [[ $arg =~ ^(force|retry)$ ]]; then
95
-        forcing_level=$arg
96
-    else
97
-        WARNING "incohérence sur $arg"
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
98 76
     fi
99
-done
100
-if test -n "$from_CLI"; then
101
-    forcing_level=force
102
-fi
103
-
104
-if test ${#albums[@]} -eq 0; then
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]"'))
108
-fi
77
+}
109 78
 
110 79
 get_from_mbz () {
111 80
     curl -Ls $coverartarchive_api/${album%%:*} > $coverart
... ...
@@ -119,19 +88,18 @@ get_from_mbz () {
119 88
             get_image "$img" "${album%%:*}.${img##*.}"
120 89
         fi
121 90
     else
122
-        WARNING "${album%%:*} réponse inattendue de musicbrainz"
91
+        return 1
123 92
     fi
124 93
 }
125 94
 
126 95
 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 96
     if test -n "$album_name" -a -n "$artist"; then
131 97
         curl --request GET --silent \
132 98
             --header "Authorization: Bearer $spotify_access_token" \
133 99
             --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
100
+            --data 'type=album' \
101
+            --data "query==album:${_album_name}%20artist:${_artist}" \
102
+            --url "https://api.spotify.com/v1/search" > $coverart
135 103
         found_albums=$(jq -r '.albums .total // empty' $coverart)
136 104
         if test ${found_albums:-0} -gt 0; then
137 105
             img=$(jq --raw-output --arg artist "${artist^^}" --arg album "${album_name^^}" "
... ...
@@ -156,32 +124,99 @@ get_from_spotify () {
156 124
     fi
157 125
 }
158 126
 
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
+
159 160
 for album in ${albums[@]}; do
161
+    mbz_related=0
160 162
     if ! test -d "${album#*:}"; then
161 163
         if test -n "$from_CLI"; then
162 164
             WARNING "${album#*:}: chemin inconnu"
163 165
         fi
164 166
         continue
165 167
     fi
166
-    if ! covered; then
167
-        echo -n "${album#*:} "
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
174
-                else
175
-                    touch "${album#*:}/$no_cover_flag"
176
-                fi
177
-            fi
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
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
184 203
         fi
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"
185 220
     fi
186 221
 done
187 222