diff --git a/src/sh/api.sh b/src/sh/api.sh index b67c911..a73d3e5 100644 --- a/src/sh/api.sh +++ b/src/sh/api.sh @@ -36,7 +36,7 @@ __api_mb_get_artist() { $CURL \ --get \ --data fmt=json \ - --data inc="url-rels" \ + --data inc="url-rels+artist-rels+aliases" \ -A "$APP_NAME/$APP_VERSION ($APP_WEBSITE)" \ "https://musicbrainz.org/ws/2/artist/$1" } diff --git a/src/sh/cache.sh b/src/sh/cache.sh index 872cd89..1f1ddd6 100644 --- a/src/sh/cache.sh +++ b/src/sh/cache.sh @@ -24,7 +24,7 @@ __put_artist_json() { f="$artistdir/$2" tmpf=$(mktemp) cat >"$tmpf" - [ -s "$tmpf" ] && mv "$tmpf" "$f" || echo "{}" >"$f" + [ -s "$tmpf" ] && mv "$tmpf" "$f" || printf "{}" >"$f" } # Returns the cached MusicBrainz data for the artist given by the MusicBrainz diff --git a/src/sh/mb.sh b/src/sh/mb.sh index d011144..82055ed 100644 --- a/src/sh/mb.sh +++ b/src/sh/mb.sh @@ -6,7 +6,7 @@ __mb_artist_cache_or_fetch() { __api_mb_get_artist "$1" | cache_put_artist_mb "$1" mbartist="$(cache_get_artist_mb "$1")" fi - echo "$mbartist" + printf "%s" "$mbartist" } # Get MusicBrainz json for artist @@ -29,7 +29,8 @@ mb_artist_wikidata() { __api_wikidata_get_sitelinks "$wikidataid" | cache_put_artist_wikidata "$1" wikidata=$(cache_get_artist_wikidata "$1") fi - echo "$wikidata" + [ ! "$wikidata" ] || [ "$wikidata" != "null" ] || return + printf "%s" "$wikidata" } # Get Wikipedia (English) summary json for artist @@ -45,12 +46,31 @@ mb_artist_enwikipedia() { # For obvious reasons it is recommended to link to wikidata only. So, we # take the second route. wikidata=$(mb_artist_wikidata "$1" || true) - wikiid=$(echo "$wikidata" | + wikiid=$(printf "%s" "$wikidata" | $JQ -r '.enwiki.url' | awk -F "/" '{print $NF}') - [ ! "$wikiid" ] || [ "$wikiid" != "null" ] || return + [ ! "$wikiid" ] && return || [ "$wikiid" = "null" ] && return __api_wikipedia_en_get_summary "$wikiid" | cache_put_artist_enwikipedia "$1" enwiki="$(cache_get_artist_enwikipedia "$1")" fi - [ ! "$enwiki" ] || [ "$enwiki" != "null" ] || return + [ ! "$enwiki" ] && return || [ "$enwiki" = "null" ] && return + printf "%s" "$enwiki" +} + +# Get Discogs json for artist +# @argument $1: MusicBrainz Artist ID +mb_artist_discogs() { + discogs="$(cache_get_artist_discogs "$1" || true)" + if [ ! "$discogs" ] || [ "$discogs" = "null" ]; then + discogsid=$(mb_artist "$1" | + $JQ -r '.relations | + map(select(.type=="discogs")) | + .[0].url.resource' | + awk -F "/" '{print $NF}') + [ ! "$discogsid" ] && return || [ "$discogsid" = "null" ] && return + __api_discogs_get_artist "$discogsid" | cache_put_artist_discogs "$1" + discogs="$(cache_get_artist_discogs "$1" || true)" + fi + [ ! "$discogs" ] && return || [ "$discogs" = "null" ] && return + printf "%s" "$discogs" } diff --git a/src/sh/preview.sh b/src/sh/preview.sh index 190fee3..9fbec72 100644 --- a/src/sh/preview.sh +++ b/src/sh/preview.sh @@ -1,6 +1,9 @@ # Print preview of artist # @input $1: MusicBrainz Artist ID __preview_artist() { - desc=$(mb_artist_enwikipedia "$1" | $JQ -r ".extract") + desc=$(mb_artist_enwikipedia "$1" | $JQ -r '.extract') + if [ ! "$desc" ]; then + desc=$(mb_artist_discogs "$1" | $JQ -r '.profile' | sed 's/\[a=\([^]]*\)\]/\1/g') + fi echo "$desc" | fold -s -w "$FZF_PREVIEW_COLUMNS" | $CAT }