reorganized code
This commit is contained in:
145
src/sh/cache.sh
145
src/sh/cache.sh
@@ -1,103 +1,140 @@
|
||||
# Caching structure
|
||||
#
|
||||
# ./artist/radix(uuid)/musicbrainz.json # Artist information
|
||||
# ./artist/radix(uuid)/releasegroups.json # List of all release groups
|
||||
# ./artist/radix(uuid)/... # Any other artist information
|
||||
# ./releasegroup/radix(uuid)/musicbrainz.json # Release group information
|
||||
# ./releasegroup/radix(uuid)/releases.json # List of all releases in release group
|
||||
# ./release/radix(uuid)/musicbrainz.json # Release information with tracklist etc.
|
||||
CACHEDIR="$HOME/.cache/$APP_NAME"
|
||||
[ -d "$CACHEDIR" ] || mkdir "$CACHEDIR"
|
||||
TYPE_ARTIST="artist"
|
||||
TYPE_RELEASEGROUP="releasegroup"
|
||||
TYPE_RELEASE="release"
|
||||
|
||||
artist_mb_filename="musicbrainz.json"
|
||||
artist_mb_releasegroups_filename="releasegroups.json"
|
||||
artist_filename="musicbrainz.json"
|
||||
artist_releasegroups_filename="releasegroups.json"
|
||||
artist_discogs_filename="discogs.json"
|
||||
artist_wikidata_filename="wikidata.json"
|
||||
artist_enwikipedia_filename="enwikipedia.json"
|
||||
releasegroup_filename="musicbrainz.json"
|
||||
releasegroup_releases_filename="releases.json"
|
||||
release_filename="musicbrainz.json"
|
||||
|
||||
# Radix transform directory name
|
||||
__radix() {
|
||||
echo "$1" | awk -F "" '{ print $1$2$3$4"/"$5$6$7$8"/"$0 }'
|
||||
}
|
||||
|
||||
# Super wrapper
|
||||
# argument $1: MusicBrainz Artist ID
|
||||
# argument $2: Filename of json file
|
||||
__get_artist_json() {
|
||||
f="$CACHEDIR/$1/$2"
|
||||
# argument $1: type
|
||||
# argument $2: MusicBrainz ID
|
||||
# argument $3: Filename of json file
|
||||
__get_json() {
|
||||
f="$CACHEDIR/$1/$(__radix "$2")/$3"
|
||||
[ -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"
|
||||
# argument $1: type
|
||||
# argument $2: MusicBrainz ID
|
||||
# argument $3: Filename of json file
|
||||
__put_json() {
|
||||
dir="$CACHEDIR/$1/$(__radix "$2")"
|
||||
[ -d "$dir" ] || mkdir -p "$dir"
|
||||
f="$dir/$3"
|
||||
tmpf=$(mktemp)
|
||||
cat >"$tmpf"
|
||||
[ -s "$tmpf" ] && mv "$tmpf" "$f" || printf "{}" >"$f"
|
||||
}
|
||||
|
||||
# Delete all cache associated to the given artist
|
||||
# argument $1: MusicBrainz Artist ID
|
||||
cache_delete_artist() {
|
||||
artistdir="$CACHEDIR/$1"
|
||||
rm -rf "$artistdir"
|
||||
## Artist
|
||||
cache_get_artist() {
|
||||
__get_json "$TYPE_ARTIST" "$1" "$artist_filename"
|
||||
}
|
||||
|
||||
# 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"
|
||||
cache_get_artist_releasegroups() {
|
||||
__get_json "$TYPE_ARTIST" "$1" "$artist_releasegroups_filename"
|
||||
}
|
||||
|
||||
# Returns the cached MusicBrainz release-group data for the artist given by the
|
||||
# MusicBrainz Artist ID in $1
|
||||
cache_get_artist_mb_releasegroups() {
|
||||
__get_artist_json "$1" "$artist_mb_releasegroups_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"
|
||||
__get_json "$TYPE_ARTIST" "$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"
|
||||
__get_json "$TYPE_ARTIST" "$1" "$artist_enwikipedia_filename"
|
||||
}
|
||||
|
||||
cache_get_artist_wikidata() {
|
||||
__get_artist_json "$1" "$artist_wikidata_filename"
|
||||
__get_json "$TYPE_ARTIST" "$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"
|
||||
cache_put_artist() {
|
||||
cat | __put_json "$TYPE_ARTIST" "$1" "$artist_filename"
|
||||
}
|
||||
|
||||
# Read from stdin the MusicBrainz release-group data for the artist given by
|
||||
# the MusicBrainz Artist ID in $1, and cache it.
|
||||
cache_put_artist_mb_releasegroups() {
|
||||
cat | __put_artist_json "$1" "$artist_mb_releasegroups_filename"
|
||||
cache_put_artist_releasegroups() {
|
||||
cat | __put_json "$TYPE_ARTIST" "$1" "$artist_releasegroups_filename"
|
||||
}
|
||||
|
||||
# Read from stdin the MusicBrainz release-group data for the artist given by
|
||||
# the MusicBrainz Artist ID in $1, and append it to the current cache.
|
||||
cache_append_artist_mb_releasegroups() {
|
||||
cache_append_artist_releasegroups() {
|
||||
tmpf=$(mktemp)
|
||||
cat >"$tmpf"
|
||||
updated=$(mktemp)
|
||||
f="$CACHEDIR/$1/$artist_mb_releasegroups_filename"
|
||||
f="$CACHEDIR/$TYPE_ARTIST/$(__radix "$1")/$artist_releasegroups_filename"
|
||||
$JQ -r --slurpfile n "$tmpf" '."release-groups" += ($n[0]|."release-groups")' "$f" >"$updated" && mv "$updated" "$f"
|
||||
rm -f "$tmpf"
|
||||
}
|
||||
|
||||
# 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"
|
||||
cat | __put_json "$TYPE_ARTIST" "$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"
|
||||
cat | __put_json "$TYPE_ARTIST" "$1" "$artist_enwikipedia_filename"
|
||||
}
|
||||
|
||||
cache_put_artist_wikidata() {
|
||||
cat | __put_artist_json "$1" "$artist_wikidata_filename"
|
||||
cat | __put_json "$TYPE_ARTIST" "$1" "$artist_wikidata_filename"
|
||||
}
|
||||
|
||||
## Release group
|
||||
cache_get_releasegroup() {
|
||||
__get_json "$TYPE_RELEASEGROUP" "$1" "$releasegroup_filename"
|
||||
}
|
||||
|
||||
cache_get_releasegroup_releases() {
|
||||
__get_json "$TYPE_RELEASEGROUP" "$1" "$releasegroup_releases_filename"
|
||||
}
|
||||
|
||||
cache_put_releasegroup() {
|
||||
cat | __put_json "$TYPE_RELEASEGROUP" "$1" "$releasegroup_filename"
|
||||
}
|
||||
|
||||
cache_put_releasegroup_releases() {
|
||||
cat | __put_json "$TYPE_RELEASEGROUP" "$1" "$releasegroup_releases_filename"
|
||||
}
|
||||
|
||||
cache_append_releasegroup_releases() {
|
||||
tmpf=$(mktemp)
|
||||
cat >"$tmpf"
|
||||
updated=$(mktemp)
|
||||
f="$CACHEDIR/$TYPE_RELEASEGROUP/$(__radix "$1")/$releasegroup_releases_filename"
|
||||
$JQ -r --slurpfile n "$tmpf" '."releases" += ($n[0]|."releases")' "$f" >"$updated" && mv "$updated" "$f"
|
||||
rm -f "$tmpf"
|
||||
}
|
||||
|
||||
## Release
|
||||
cache_get_release() {
|
||||
__get_json "$TYPE_RELEASE" "$1" "$release_filename"
|
||||
}
|
||||
|
||||
cache_put_release() {
|
||||
cat | __put_json "$TYPE_RELEASE" "$1" "$release_filename"
|
||||
}
|
||||
|
||||
## Cache deletion
|
||||
cache_delete_artist() {
|
||||
# Get release groups
|
||||
echo "NOT IMPLEMENTED" >/dev/stderr
|
||||
}
|
||||
|
Reference in New Issue
Block a user