rg caching, improved display

This commit is contained in:
2025-07-23 20:51:22 +02:00
parent 06e07f0f13
commit f3c58ca859
6 changed files with 98 additions and 53 deletions

View File

@@ -1,12 +1,28 @@
# 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() {
mbartist="$(cache_get_artist_mb "$1" || true)"
if [ ! "$mbartist" ]; then
if ! cache_get_artist_mb "$1"; then
__api_mb_get_artist "$1" | cache_put_artist_mb "$1"
mbartist="$(cache_get_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
printf "%s" "$mbartist"
}
# Get MusicBrainz json for artist
@@ -18,8 +34,7 @@ mb_artist() {
# 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
if ! cache_get_artist_wikidata "$1"; then
wikidataid=$(mb_artist "$1" |
$JQ -r '.relations |
map(select(.type=="wikidata")) |
@@ -27,17 +42,14 @@ mb_artist_wikidata() {
awk -F "/" '{print $NF}')
[ ! "$wikidataid" ] || [ "$wikidataid" != "null" ] || return
__api_wikidata_get_sitelinks "$wikidataid" | cache_put_artist_wikidata "$1"
wikidata=$(cache_get_artist_wikidata "$1")
cache_get_artist_wikidata "$1"
fi
[ ! "$wikidata" ] || [ "$wikidata" != "null" ] || return
printf "%s" "$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
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)
@@ -51,17 +63,14 @@ mb_artist_enwikipedia() {
awk -F "/" '{print $NF}')
[ ! "$wikiid" ] && return || [ "$wikiid" = "null" ] && return
__api_wikipedia_en_get_summary "$wikiid" | cache_put_artist_enwikipedia "$1"
enwiki="$(cache_get_artist_enwikipedia "$1")"
cache_get_artist_enwikipedia "$1"
fi
[ ! "$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
if ! cache_get_artist_discogs "$1"; then
discogsid=$(mb_artist "$1" |
$JQ -r '.relations |
map(select(.type=="discogs")) |
@@ -69,8 +78,12 @@ mb_artist_discogs() {
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)"
cache_get_artist_discogs "$1"
fi
[ ! "$discogs" ] && return || [ "$discogs" = "null" ] && return
printf "%s" "$discogs"
}
# Get release-groups json for artist
# @argument $1: MusicBrainz Artist ID
mb_artist_releasegroups() {
__mb_artist_cache_or_fetch_releasegroups "$1"
}