90 lines
3.1 KiB
Bash
90 lines
3.1 KiB
Bash
# The only IDs uses here are MusicBrainz IDs
|
|
|
|
# Helper methods to retrieve from cache, if it exists, and otherwise populate
|
|
# cache and retrieve
|
|
__mb_artist_cache_or_fetch() {
|
|
if ! cache_get_artist_mb "$1"; then
|
|
__api_mb_get_artist "$1" | cache_put_artist_mb "$1"
|
|
cache_get_artist_mb "$1"
|
|
fi
|
|
}
|
|
|
|
__mb_artist_cache_or_fetch_releasegroups() {
|
|
if ! cache_get_artist_mb_releasegroups "$1"; then
|
|
__api_mb_browse_release_groups "$1" | cache_put_artist_mb_releasegroups "$1"
|
|
rg="$(cache_get_artist_mb_releasegroups "$1")"
|
|
total=$(printf "%s" "$rg" | $JQ -r '."release-group-count"')
|
|
seen=$MB_BROWSE_STEPS
|
|
while [ "$total" -gt "$seen" ]; do
|
|
# Fetch remaning release groups, and append to cache
|
|
sleep 1 # Make sure we don't get blocked (we prefer not to handle failed requests...)
|
|
__api_mb_browse_release_groups "$1" "$seen" | cache_append_artist_mb_releasegroups "$1"
|
|
seen=$((seen + MB_BROWSE_STEPS))
|
|
done
|
|
cache_get_artist_mb_releasegroups "$1"
|
|
fi
|
|
}
|
|
|
|
# Get MusicBrainz json for artist
|
|
# @argument $1: MusicBrainz Artist ID
|
|
mb_artist() {
|
|
__mb_artist_cache_or_fetch "$1"
|
|
}
|
|
|
|
# Get Wikidata json for artist
|
|
# @argument $1: MusicBrainz Artist ID
|
|
mb_artist_wikidata() {
|
|
if ! cache_get_artist_wikidata "$1"; then
|
|
wikidataid=$(mb_artist "$1" |
|
|
$JQ -r '.relations |
|
|
map(select(.type=="wikidata")) |
|
|
.[0].url.resource' |
|
|
awk -F "/" '{print $NF}')
|
|
[ ! "$wikidataid" ] || [ "$wikidataid" != "null" ] || return
|
|
__api_wikidata_get_sitelinks "$wikidataid" | cache_put_artist_wikidata "$1"
|
|
cache_get_artist_wikidata "$1"
|
|
fi
|
|
}
|
|
|
|
# Get Wikipedia (English) summary json for artist
|
|
# @argument $1: MusicBrainz Artist ID
|
|
mb_artist_enwikipedia() {
|
|
if ! cache_get_artist_enwikipedia "$1"; then
|
|
# To fetch the wikipedia data, we need the wikipedia URL
|
|
# There are two possibly ways to get the wikipedia URL
|
|
# 1. From the website relations in MB (MB artists donw have wiki rels)
|
|
# 2. MB website relations -> Wikidata -> Wikipedia
|
|
# Lately, Wikipedia pages are not stored in the MB artist url relations.
|
|
# For obvious reasons it is recommended to link to wikidata only. So, we
|
|
# take the second route.
|
|
wikidata=$(mb_artist_wikidata "$1" || true)
|
|
wikiid=$(printf "%s" "$wikidata" |
|
|
$JQ -r '.enwiki.url' |
|
|
awk -F "/" '{print $NF}')
|
|
[ ! "$wikiid" ] && return || [ "$wikiid" = "null" ] && return
|
|
__api_wikipedia_en_get_summary "$wikiid" | cache_put_artist_enwikipedia "$1"
|
|
cache_get_artist_enwikipedia "$1"
|
|
fi
|
|
}
|
|
|
|
# Get Discogs json for artist
|
|
# @argument $1: MusicBrainz Artist ID
|
|
mb_artist_discogs() {
|
|
if ! cache_get_artist_discogs "$1"; 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"
|
|
cache_get_artist_discogs "$1"
|
|
fi
|
|
}
|
|
|
|
# Get release-groups json for artist
|
|
# @argument $1: MusicBrainz Artist ID
|
|
mb_artist_releasegroups() {
|
|
__mb_artist_cache_or_fetch_releasegroups "$1"
|
|
}
|