Showing 1 changed files with 70 additions and 75 deletions
+70 -75
get_cover
... ...
@@ -8,7 +8,6 @@ mbz_agent='getCover/0.1 (https://seb.lautre.net/git/seb/scripts)'
8 8
 
9 9
 test -e $0.conf && source $0.conf
10 10
 
11
-coverartarchive_api="https://coverartarchive.org/release"
12 11
 coverart=$(mktemp --dry-run /dev/shm/XXXXXXXX)
13 12
 sizes=(500 large small 250 1200)
14 13
 
... ...
@@ -47,11 +46,29 @@ covered () {
47 46
 }
48 47
 
49 48
 get_image () {
49
+    install_image () {
50
+        mv -f /dev/shm/$2 ${album#*:}/$cover_img.${2##*.} \
51
+        && OK \
52
+        || ERROR "échec en écriture"
53
+    }
54
+
50 55
     if curl -Ls $1 > /dev/shm/$2; then
51 56
         mime_type=$(file -bn --mime-type /dev/shm/$2)
52 57
         if [[ ${mime_type:-erreur} =~ ^image/ ]]; then
53
-            mv -f /dev/shm/$2 ${album#*:}/$cover_img.${2##*.} \
54
-            || ERROR "échec en écriture"
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
55 72
         else
56 73
             WARNING "${album%%:*} type $mime_type"
57 74
         fi
... ...
@@ -60,31 +77,15 @@ get_image () {
60 77
     fi
61 78
 }
62 79
 
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
76
-    fi
77
-}
78
-
79 80
 get_from_mbz () {
80
-    curl -Ls $coverartarchive_api/${album%%:*} > $coverart
81
+    curl -Ls https://coverartarchive.org/release${1:+-group}/${album%%:*} > $coverart
81 82
     if test $(file -bn --mime-type $coverart) = application/json; then
82 83
         unset img
83 84
         for size in ${sizes[@]}; do
84 85
             img=$(jq -r '.images | .[] | select(.front == true) | .thumbnails | ."'$size'"?' $coverart 2>/dev/null)
85
-            test -n "$img" && break
86
+            test "${img:-null}" != null && break
86 87
         done
87
-        if test -n "$img"; then
88
+        if test "${img:-null}" != null; then
88 89
             get_image "$img" "${album%%:*}.${img##*.}"
89 90
         fi
90 91
     else
... ...
@@ -92,36 +93,14 @@ get_from_mbz () {
92 93
     fi
93 94
 }
94 95
 
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' \
100
-            --data 'type=album' \
101
-            --data "query==album:${_album_name}%20artist:${_artist}" \
102
-            --url "https://api.spotify.com/v1/search" > $coverart
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
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
125 104
 }
126 105
 
127 106
 if ! test -d $medias; then
... ...
@@ -141,12 +120,21 @@ for arg in $@; do
141 120
     elif [[ $arg =~ ^/ ]]; then
142 121
         from_CLI=1
143 122
         albums[${#albums[@]}]=":${arg%/}"
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)
144 131
     elif [[ $arg =~ ^(force|retry)$ ]]; then
145 132
         forcing_level=$arg
146 133
     else
147 134
         WARNING "incohérence sur $arg"
148 135
     fi
149 136
 done
137
+
150 138
 if test -n "$from_CLI"; then
151 139
     forcing_level=force
152 140
 fi
... ...
@@ -168,12 +156,37 @@ for album in ${albums[@]}; do
168 156
     if covered; then
169 157
         continue
170 158
     fi
171
-    echo -n "${album#*:} "
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
172 167
     if test -n "${album%%:*}"; then
173 168
         mbz_related=1
174 169
         if get_from_mbz; then
175
-            OK
176 170
             continue
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
177 190
         fi
178 191
     fi
179 192
 
... ...
@@ -182,11 +195,7 @@ for album in ${albums[@]}; do
182 195
                         where artist || "/" || name = "'${album#*:$medias/}'"')"
183 196
     eval $(php -r 'echo "_artist=".rawurlencode($argv[1]).";_album_name=".rawurlencode($argv[2]);' -- "$artist" "$album_name")
184 197
 
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
198
+    mbz_api release "release:$_album_name%20AND%20artist:$_artist" > $coverart
190 199
 
191 200
     mbids=($(jq --raw-output --arg artist "${artist^^}" --arg album "${album_name^^}" '
192 201
             .releases
... ...
@@ -198,26 +207,12 @@ for album in ${albums[@]}; do
198 207
     for mbid in ${mbids[@]}; do
199 208
         album="$mbid:${album#*:}"
200 209
         if get_from_mbz; then
201
-            OK
202 210
             continue 2
203 211
         fi
204 212
     done
205 213
 
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"
220
-    fi
214
+    WARNING "pas trouvé ($(( ${mbz_related:-0} + ${#mbids[@]} )) relations sur musicbrainz)"
215
+    touch "${album#*:}/$no_cover_flag"
221 216
 done
222 217
 
223 218
 rm -f $coverart