cacheing and mb wrapper

This commit is contained in:
2025-07-23 10:22:11 +02:00
parent 20152a368e
commit 81e49c0984
5 changed files with 148 additions and 43 deletions

56
src/sh/mb.sh Normal file
View File

@@ -0,0 +1,56 @@
# The only IDs uses here are MusicBrainz IDs
__mb_artist_cache_or_fetch() {
mbartist="$(cache_get_artist_mb "$1" || true)"
if [ ! "$mbartist" ]; then
__api_mb_get_artist "$1" | cache_put_artist_mb "$1"
mbartist="$(cache_get_artist_mb "$1")"
fi
echo "$mbartist"
}
# 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() {
wikidata=$(cache_get_artist_wikidata "$1" || true)
if [ ! "$wikidata" ] || [ "$wikidata" = "null" ]; 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"
wikidata=$(cache_get_artist_wikidata "$1")
fi
echo "$wikidata"
}
# Get Wikipedia (English) summary json for artist
# @argument $1: MusicBrainz Artist ID
mb_artist_enwikipedia() {
enwiki="$(cache_get_artist_enwikipedia "$1" || true)"
if [ ! "$enwiki" ] || [ "$enwiki" = "null" ]; 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=$(echo "$wikidata" |
$JQ -r '.enwiki.url' |
awk -F "/" '{print $NF}')
[ ! "$wikiid" ] || [ "$wikiid" != "null" ] || return
__api_wikipedia_en_get_summary "$wikiid" | cache_put_artist_enwikipedia "$1"
enwiki="$(cache_get_artist_enwikipedia "$1")"
fi
echo "$enwiki"
}