reorganized code

This commit is contained in:
2025-07-29 12:04:14 +02:00
parent 5bfb76fadb
commit 8bbc57ffb2
4 changed files with 213 additions and 104 deletions

View File

@@ -55,7 +55,7 @@ if [ "${1:-}" = "--internal-search" ]; then
echo "$$" >"$PIDFILE" echo "$$" >"$PIDFILE"
touch "$LOCKFILE" touch "$LOCKFILE"
sleep 1 sleep 1
__api_mb_search_artists "$2" | api_mb_search_artists "$2" |
$JQ -r '.artists[] | [ $JQ -r '.artists[] | [
.id, .id,
.type, .type,
@@ -76,15 +76,20 @@ if [ "${1:-}" = "--internal-search" ]; then
exit 0 exit 0
fi fi
if [ "${1:-}" = "--internal-list-releases-fresh" ]; then if [ "${1:-}" = "--internal-list-releases" ]; then
print "NOT IMPLEMENTED"
exit 0
fi
if [ "${1:-}" = "--internal-list-releasegroups-fresh" ]; then
artistid="$2" artistid="$2"
cache_delete_artist "$artistid" cache_delete_artist "$artistid"
shift 2 shift 2
set -- "--internal-list-releases" "$artistid" set -- "--internal-list-releasegroups" "$artistid"
mb_artist "$artistid" >/dev/null mb_artist "$artistid" >/dev/null
fi fi
if [ "${1:-}" = "--internal-list-releases" ]; then if [ "${1:-}" = "--internal-list-releasegroups" ]; then
name=$(mb_artist "$2" | $JQ -r '.name') name=$(mb_artist "$2" | $JQ -r '.name')
mb_artist_releasegroups "$2" | mb_artist_releasegroups "$2" |
$JQ -r '."release-groups"[] | [ $JQ -r '."release-groups"[] | [
@@ -158,10 +163,10 @@ fi
while true; do while true; do
case "${1:-}" in case "${1:-}" in
"--show-artist") "--show-artist")
name=$(mb_artist "$2" | $JQ -r '.name') name="$(mb_artist "$2" | $JQ -r '.name')"
secsymb=$(printf "$FORMAT_TYPE_HAS_SECONDARY" "") secsymb="$(printf "$FORMAT_TYPE_HAS_SECONDARY" "")"
sel=$( sel=$(
$0 --internal-list-releases "$2" | $0 --internal-list-releasegroups "$2" |
$FZF \ $FZF \
--ansi \ --ansi \
--reverse \ --reverse \
@@ -173,10 +178,20 @@ while true; do
--with-nth="{1}" \ --with-nth="{1}" \
--bind="alt-1:change-query(!$secsymb ),alt-2:change-query($secsymb )" \ --bind="alt-1:change-query(!$secsymb ),alt-2:change-query($secsymb )" \
--bind="ctrl-d:half-page-down,ctrl-u:half-page-up" \ --bind="ctrl-d:half-page-down,ctrl-u:half-page-up" \
--bind="ctrl-r:reload:$0 --internal-list-releases-fresh \"$2\"" --bind="ctrl-r:reload:$0 --internal-list-releasegroups-fresh \"$2\""
) )
[ "$sel" ] && set -- "--show-release" "$sel" [ "$sel" ] && set -- "--show-release" "$sel"
;; ;;
"--show-release-group")
title="$(mb_releasegroup "$2" |
$JQ -r '.title')"
artist="$(mb_releasegroup "$2" |
$JQ -r '."artist-credit" | map(([.name, .joinphrase]|join(""))) | join("")')"
echo "$title"
echo "$artist"
exit 0
set -- ""
;;
*) *)
sel=$( sel=$(
printf "" | printf "" |

View File

@@ -1,32 +1,58 @@
MB_BROWSE_STEPS=100 MB_BROWSE_STEPS=100
export MB_BROWSE_STEPS export MB_BROWSE_STEPS
# Argument: MB Artist ID api_mb_artist() {
__api_mb_browse_releases() {
$CURL \ $CURL \
--get \ --get \
--data fmt=json \ --data fmt=json \
--data inc=release-groups \ --data inc="url-rels+artist-rels+aliases" \
--data-urlencode artist="$1" \
-A "$APP_NAME/$APP_VERSION ($APP_WEBSITE)" \ -A "$APP_NAME/$APP_VERSION ($APP_WEBSITE)" \
"https://musicbrainz.org/ws/2/release" "https://musicbrainz.org/ws/2/artist/$1"
}
api_mb_releasegroup() {
$CURL \
--get \
--data fmt=json \
--data inc=artist-credits \
-A "$APP_NAME/$APP_VERSION ($APP_WEBSITE)" \
"https://musicbrainz.org/ws/2/release-group/$1"
} }
# Argument: MB Artist ID api_mb_release() {
__api_mb_browse_release_groups() { $CURL \
--get \
--data fmt=json \
--data inc="recordings+artist-credits" \
-A "$APP_NAME/$APP_VERSION ($APP_WEBSITE)" \
"https://musicbrainz.org/ws/2/release/$1"
}
api_mb_browse_artist_releasegroups() {
$CURL \ $CURL \
--get \ --get \
--data fmt=json \ --data fmt=json \
--data inc=artist-credits \ --data inc=artist-credits \
--data limit="$MB_BROWSE_STEPS" \ --data limit="$MB_BROWSE_STEPS" \
--data offset="${2:-0}" \ --data offset="${2:-0}" \
--data-urlencode artist="$1" \ --data artist="$1" \
-A "$APP_NAME/$APP_VERSION ($APP_WEBSITE)" \ -A "$APP_NAME/$APP_VERSION ($APP_WEBSITE)" \
"https://musicbrainz.org/ws/2/release-group" "https://musicbrainz.org/ws/2/release-group"
} }
api_mb_browse_releasegroup_releases() {
$CURL \
--get \
--data fmt=json \
--data inc=artist-credits \
--data limit="$MB_BROWSE_STEPS" \
--data offset="${2:-0}" \
--data release-group="$1" \
-A "$APP_NAME/$APP_VERSION ($APP_WEBSITE)" \
"https://musicbrainz.org/ws/2/release"
}
# Argument: Search string # Argument: Search string
__api_mb_search_artists() { api_mb_search_artists() {
$CURL \ $CURL \
--get \ --get \
--data fmt=json \ --data fmt=json \
@@ -35,34 +61,21 @@ __api_mb_search_artists() {
"https://musicbrainz.org/ws/2/artist" "https://musicbrainz.org/ws/2/artist"
} }
# Argument: MB Artist ID api_discogs_artist() {
__api_mb_get_artist() {
$CURL \
--get \
--data fmt=json \
--data inc="url-rels+artist-rels+aliases" \
-A "$APP_NAME/$APP_VERSION ($APP_WEBSITE)" \
"https://musicbrainz.org/ws/2/artist/$1"
}
# Argument: Discogs id
__api_discogs_get_artist() {
$CURL \ $CURL \
--get \ --get \
-A "$APP_NAME/$APP_VERSION ($APP_WEBSITE)" \ -A "$APP_NAME/$APP_VERSION ($APP_WEBSITE)" \
"https://api.discogs.com/artists/$1" "https://api.discogs.com/artists/$1"
} }
# Argument: wikidata id api_wikidata_sitelinks() {
__api_wikidata_get_sitelinks() {
$CURL \ $CURL \
--get \ --get \
-A "$APP_NAME/$APP_VERSION ($APP_WEBSITE)" \ -A "$APP_NAME/$APP_VERSION ($APP_WEBSITE)" \
"https://www.wikidata.org/w/rest.php/wikibase/v1/entities/items/$1/sitelinks" "https://www.wikidata.org/w/rest.php/wikibase/v1/entities/items/$1/sitelinks"
} }
# Argument: Wikipedia name (last part of URL) api_wikipedia_en_summary() {
__api_wikipedia_en_get_summary() {
$CURL \ $CURL \
--get \ --get \
-A "$APP_NAME/$APP_VERSION ($APP_WEBSITE)" \ -A "$APP_NAME/$APP_VERSION ($APP_WEBSITE)" \

View File

@@ -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" CACHEDIR="$HOME/.cache/$APP_NAME"
[ -d "$CACHEDIR" ] || mkdir "$CACHEDIR" TYPE_ARTIST="artist"
TYPE_RELEASEGROUP="releasegroup"
TYPE_RELEASE="release"
artist_mb_filename="musicbrainz.json" artist_filename="musicbrainz.json"
artist_mb_releasegroups_filename="releasegroups.json" artist_releasegroups_filename="releasegroups.json"
artist_discogs_filename="discogs.json" artist_discogs_filename="discogs.json"
artist_wikidata_filename="wikidata.json" artist_wikidata_filename="wikidata.json"
artist_enwikipedia_filename="enwikipedia.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 # Super wrapper
# argument $1: MusicBrainz Artist ID # argument $1: type
# argument $2: Filename of json file # argument $2: MusicBrainz ID
__get_artist_json() { # argument $3: Filename of json file
f="$CACHEDIR/$1/$2" __get_json() {
f="$CACHEDIR/$1/$(__radix "$2")/$3"
[ -f "$f" ] || return [ -f "$f" ] || return
cat "$f" cat "$f"
} }
# Super wrapper # Super wrapper
# argument $1: MusicBrainz Artist ID # argument $1: type
# argument $2: Filename of json file # argument $2: MusicBrainz ID
__put_artist_json() { # argument $3: Filename of json file
artistdir="$CACHEDIR/$1" __put_json() {
[ -d "$artistdir" ] || mkdir "$artistdir" dir="$CACHEDIR/$1/$(__radix "$2")"
f="$artistdir/$2" [ -d "$dir" ] || mkdir -p "$dir"
f="$dir/$3"
tmpf=$(mktemp) tmpf=$(mktemp)
cat >"$tmpf" cat >"$tmpf"
[ -s "$tmpf" ] && mv "$tmpf" "$f" || printf "{}" >"$f" [ -s "$tmpf" ] && mv "$tmpf" "$f" || printf "{}" >"$f"
} }
# Delete all cache associated to the given artist ## Artist
# argument $1: MusicBrainz Artist ID cache_get_artist() {
cache_delete_artist() { __get_json "$TYPE_ARTIST" "$1" "$artist_filename"
artistdir="$CACHEDIR/$1"
rm -rf "$artistdir"
} }
# Returns the cached MusicBrainz data for the artist given by the MusicBrainz cache_get_artist_releasegroups() {
# Artist ID in $1 __get_json "$TYPE_ARTIST" "$1" "$artist_releasegroups_filename"
cache_get_artist_mb() {
__get_artist_json "$1" "$artist_mb_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() { 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() { cache_get_artist_enwikipedia() {
__get_artist_json "$1" "$artist_enwikipedia_filename" __get_json "$TYPE_ARTIST" "$1" "$artist_enwikipedia_filename"
} }
cache_get_artist_wikidata() { 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 cache_put_artist() {
# Artist ID in $1, and cache it. cat | __put_json "$TYPE_ARTIST" "$1" "$artist_filename"
cache_put_artist_mb() {
cat | __put_artist_json "$1" "$artist_mb_filename"
} }
# Read from stdin the MusicBrainz release-group data for the artist given by cache_put_artist_releasegroups() {
# the MusicBrainz Artist ID in $1, and cache it. cat | __put_json "$TYPE_ARTIST" "$1" "$artist_releasegroups_filename"
cache_put_artist_mb_releasegroups() {
cat | __put_artist_json "$1" "$artist_mb_releasegroups_filename"
} }
# Read from stdin the MusicBrainz release-group data for the artist given by cache_append_artist_releasegroups() {
# the MusicBrainz Artist ID in $1, and append it to the current cache.
cache_append_artist_mb_releasegroups() {
tmpf=$(mktemp) tmpf=$(mktemp)
cat >"$tmpf" cat >"$tmpf"
updated=$(mktemp) 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" $JQ -r --slurpfile n "$tmpf" '."release-groups" += ($n[0]|."release-groups")' "$f" >"$updated" && mv "$updated" "$f"
rm -f "$tmpf" 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() { 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() { 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() { 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
} }

View File

@@ -3,25 +3,55 @@
# Helper methods to retrieve from cache, if it exists, and otherwise populate # Helper methods to retrieve from cache, if it exists, and otherwise populate
# cache and retrieve # cache and retrieve
__mb_artist_cache_or_fetch() { __mb_artist_cache_or_fetch() {
if ! cache_get_artist_mb "$1"; then if ! cache_get_artist "$1"; then
__api_mb_get_artist "$1" | cache_put_artist_mb "$1" api_mb_artist "$1" | cache_put_artist "$1"
cache_get_artist_mb "$1" cache_get_artist "$1"
fi
}
__mb_releasegroup_cache_or_fetch() {
if ! cache_get_releasegroup "$1"; then
api_mb_releasegroup "$1" | cache_put_releasegroup "$1"
cache_get_releasegroup "$1"
fi
}
__mb_release_cache_or_fetch() {
if ! cache_get_release "$1"; then
api_mb_release "$1" | cache_put_release "$1"
cache_get_release "$1"
fi fi
} }
__mb_artist_cache_or_fetch_releasegroups() { __mb_artist_cache_or_fetch_releasegroups() {
if ! cache_get_artist_mb_releasegroups "$1"; then if ! cache_get_artist_releasegroups "$1"; then
__api_mb_browse_release_groups "$1" | cache_put_artist_mb_releasegroups "$1" api_mb_browse_artist_releasegroups "$1" | cache_put_artist_releasegroups "$1"
rg="$(cache_get_artist_mb_releasegroups "$1")" rg="$(cache_get_artist_releasegroups "$1")"
total=$(printf "%s" "$rg" | $JQ -r '."release-group-count"') total=$(printf "%s" "$rg" | $JQ -r '."release-group-count"')
seen=$MB_BROWSE_STEPS seen=$MB_BROWSE_STEPS
while [ "$total" -gt "$seen" ]; do while [ "$total" -gt "$seen" ]; do
# Fetch remaning release groups, and append to cache # Fetch remaning release groups, and append to cache
sleep 1 # Make sure we don't get blocked (we prefer not to handle failed requests...) 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" api_mb_browse_artist_releasegroups "$1" "$seen" | cache_append_artist_releasegroups "$1"
seen=$((seen + MB_BROWSE_STEPS)) seen=$((seen + MB_BROWSE_STEPS))
done done
cache_get_artist_mb_releasegroups "$1" cache_get_artist_releasegroups "$1"
fi
}
__mb_releasegroup_cache_or_fetch_releases() {
if ! cache_get_releasegroup_releases "$1"; then
api_mb_browse_releasegroup_releases "$1" | cache_put_releasegroup_releases "$1"
releases="$(cache_get_releasegroup_releases "$1")"
total=$(printf "%s" "$releases" | $JQ -r '."release-count"')
seen=$MB_BROWSE_STEPS
while [ "$total" -gt "$seen" ]; do
# Fetch remaning releases, and append to cache
sleep 1 # Make sure we don't get blocked (we prefer not to handle failed requests...)
api_mb_browse_releasegroup_releases "$1" "$seen" | cache_append_releasegroup_releases "$1"
seen=$((seen + MB_BROWSE_STEPS))
done
cache_get_releasegroup_releases "$1"
fi fi
} }
@@ -38,10 +68,10 @@ mb_artist_wikidata() {
wikidataid=$(mb_artist "$1" | wikidataid=$(mb_artist "$1" |
$JQ -r '.relations | $JQ -r '.relations |
map(select(.type=="wikidata")) | map(select(.type=="wikidata")) |
.[0].url.resource' | .[0].url.resource // ""' |
awk -F "/" '{print $NF}') awk -F "/" '{print $NF}')
[ ! "$wikidataid" ] && return || [ "$wikidataid" = "null" ] && return [ ! "$wikidataid" ] && return
__api_wikidata_get_sitelinks "$wikidataid" | cache_put_artist_wikidata "$1" api_wikidata_sitelinks "$wikidataid" | cache_put_artist_wikidata "$1"
cache_get_artist_wikidata "$1" cache_get_artist_wikidata "$1"
fi fi
} }
@@ -59,10 +89,10 @@ mb_artist_enwikipedia() {
# take the second route. # take the second route.
wikidata=$(mb_artist_wikidata "$1" || true) wikidata=$(mb_artist_wikidata "$1" || true)
wikiid=$(printf "%s" "$wikidata" | wikiid=$(printf "%s" "$wikidata" |
$JQ -r '.enwiki.url' | $JQ -r '.enwiki.url // ""' |
awk -F "/" '{print $NF}') awk -F "/" '{print $NF}')
[ ! "$wikiid" ] && return || [ "$wikiid" = "null" ] && return [ ! "$wikiid" ] && return
__api_wikipedia_en_get_summary "$wikiid" | cache_put_artist_enwikipedia "$1" api_wikipedia_en_summary "$wikiid" | cache_put_artist_enwikipedia "$1"
cache_get_artist_enwikipedia "$1" cache_get_artist_enwikipedia "$1"
fi fi
} }
@@ -74,10 +104,10 @@ mb_artist_discogs() {
discogsid=$(mb_artist "$1" | discogsid=$(mb_artist "$1" |
$JQ -r '.relations | $JQ -r '.relations |
map(select(.type=="discogs")) | map(select(.type=="discogs")) |
.[0].url.resource' | .[0].url.resource // ""' |
awk -F "/" '{print $NF}') awk -F "/" '{print $NF}')
[ ! "$discogsid" ] && return || [ "$discogsid" = "null" ] && return [ ! "$discogsid" ] && return
__api_discogs_get_artist "$discogsid" | cache_put_artist_discogs "$1" api_discogs_artist "$discogsid" | cache_put_artist_discogs "$1"
cache_get_artist_discogs "$1" cache_get_artist_discogs "$1"
fi fi
} }
@@ -87,3 +117,17 @@ mb_artist_discogs() {
mb_artist_releasegroups() { mb_artist_releasegroups() {
__mb_artist_cache_or_fetch_releasegroups "$1" __mb_artist_cache_or_fetch_releasegroups "$1"
} }
# Get MusicBrainz json for release group
# @argument $1: MusicBrainz Release-Group ID
mb_releasegroup() {
__mb_releasegroup_cache_or_fetch "$1"
}
mb_releasegroup_releases() {
__mb_releasegroup_cache_or_fetch_releases "$1"
}
mb_release() {
__mb_release_cache_or_fetch "$1"
}