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

72
src/sh/cache.sh Normal file
View File

@@ -0,0 +1,72 @@
CACHEDIR="$HOME/.cache/$APP_NAME"
[ -d "$CACHEDIR" ] || mkdir "$CACHEDIR"
artist_mb_filename="musicbrainz.json"
artist_discogs_filename="discogs.json"
artist_wikidata_filename="wikidata.json"
artist_enwikipedia_filename="enwikipedia.json"
# Super wrapper
# argument $1: MusicBrainz Artist ID
# argument $2: Filename of json file
__get_artist_json() {
f="$CACHEDIR/$1/$2"
[ -f "$f" ] || return
cat "$f"
}
# Super wrapper
# argument $1: MusicBrainz Artist ID
# argument $2: Filename of json file
__put_artist_json() {
artistdir="$CACHEDIR/$1"
[ -d "$artistdir" ] || mkdir "$artistdir"
f="$artistdir/$2"
tmpf=$(mktemp)
cat >"$tmpf"
[ -s "$tmpf" ] && mv "$tmpf" "$f" || echo "{}" >"$f"
}
# Returns the cached MusicBrainz data for the artist given by the MusicBrainz
# Artist ID in $1
cache_get_artist_mb() {
__get_artist_json "$1" "$artist_mb_filename"
}
# Returns the cached Discogs data for the artist given by the MusicBrainz
# Artist ID in $1
cache_get_artist_discogs() {
__get_artist_json "$1" "$artist_discogs_filename"
}
# Returns the cached Wikipedia (English) data for the artist given by the
# MusicBrainz Artist ID in $1
cache_get_artist_enwikipedia() {
__get_artist_json "$1" "$artist_enwikipedia_filename"
}
cache_get_artist_wikidata() {
__get_artist_json "$1" "$artist_wikidata_filename"
}
# Read from stdin the MusicBrainz data for the artist given by the MusicBrainz
# Artist ID in $1, and cache it.
cache_put_artist_mb() {
cat | __put_artist_json "$1" "$artist_mb_filename"
}
# Read from stdin the Discogs data for the artist given by the MusicBrainz
# Artist ID in $1, and cache it.
cache_put_artist_discogs() {
cat | __put_artist_json "$1" "$artist_discogs_filename"
}
# Read from stdin the Wikipedia data (English) for the artist given by the
# MusicBrainz Artist ID in $1, and cache it.
cache_put_artist_enwikipedia() {
cat | __put_artist_json "$1" "$artist_enwikipedia_filename"
}
cache_put_artist_wikidata() {
cat | __put_artist_json "$1" "$artist_wikidata_filename"
}