commented sh files
This commit is contained in:
167
src/sh/mb.sh
167
src/sh/mb.sh
@@ -1,7 +1,13 @@
|
||||
# The only IDs uses here are MusicBrainz IDs
|
||||
# This files provides a high-level access to the MusicBrainz databse. The only
|
||||
# IDs used here are MusicBrainz IDs
|
||||
|
||||
# Helper methods to retrieve from cache, if it exists, and otherwise populate
|
||||
# cache and retrieve
|
||||
# The following methods are local methods that combines the MusicBrainz API
|
||||
# with the caching methods.
|
||||
|
||||
# Retrieve MusicBrainz data for artist from cache (if it exists), and otherwise
|
||||
# download it using the MusicBrainz API.
|
||||
#
|
||||
# @argument $1: MusicBrainz artist ID
|
||||
__mb_artist_cache_or_fetch() {
|
||||
if ! cache_get_artist "$1"; then
|
||||
api_mb_artist "$1" | cache_put_artist "$1"
|
||||
@@ -9,6 +15,10 @@ __mb_artist_cache_or_fetch() {
|
||||
fi
|
||||
}
|
||||
|
||||
# Retrieve MusicBrainz data for release group from cache (if it exists), and
|
||||
# otherwise download it using the MusicBrainz API.
|
||||
#
|
||||
# @argument $1: MusicBrainz release-group ID
|
||||
__mb_releasegroup_cache_or_fetch() {
|
||||
if ! cache_get_releasegroup "$1"; then
|
||||
api_mb_releasegroup "$1" | cache_put_releasegroup "$1"
|
||||
@@ -16,6 +26,10 @@ __mb_releasegroup_cache_or_fetch() {
|
||||
fi
|
||||
}
|
||||
|
||||
# Retrieve MusicBrainz data for release from cache (if it exists), and
|
||||
# otherwise download it using the MusicBrainz API.
|
||||
#
|
||||
# @argument $1: MusicBrainz release ID
|
||||
__mb_release_cache_or_fetch() {
|
||||
if ! cache_get_release "$1"; then
|
||||
api_mb_release "$1" | cache_put_release "$1"
|
||||
@@ -23,6 +37,10 @@ __mb_release_cache_or_fetch() {
|
||||
fi
|
||||
}
|
||||
|
||||
# Retrieve MusicBrainz data for release groups of given artist from cache (if
|
||||
# it exists), and otherwise download it using the MusicBrainz API.
|
||||
#
|
||||
# @argument $1: MusicBrainz artist ID
|
||||
__mb_artist_cache_or_fetch_releasegroups() {
|
||||
if ! cache_get_artist_releasegroups "$1"; then
|
||||
api_mb_browse_artist_releasegroups "$1" | cache_put_artist_releasegroups "$1"
|
||||
@@ -39,6 +57,10 @@ __mb_artist_cache_or_fetch_releasegroups() {
|
||||
fi
|
||||
}
|
||||
|
||||
# Retrieve MusicBrainz data for releases of given release group from cache (if
|
||||
# it exists), and otherwise download it using the MusicBrainz API.
|
||||
#
|
||||
# @argument $1: MusicBrainz release-group ID
|
||||
__mb_releasegroup_cache_or_fetch_releases() {
|
||||
if ! cache_get_releasegroup_releases "$1"; then
|
||||
api_mb_browse_releasegroup_releases "$1" | cache_put_releasegroup_releases "$1"
|
||||
@@ -55,14 +77,18 @@ __mb_releasegroup_cache_or_fetch_releases() {
|
||||
fi
|
||||
}
|
||||
|
||||
# Get MusicBrainz json for artist
|
||||
# @argument $1: MusicBrainz Artist ID
|
||||
# The following methods provide the external interface
|
||||
|
||||
# Retrieve MusicBrainz data 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
|
||||
# Retrieve Wikidata data for artist
|
||||
#
|
||||
# @argument $1: MusicBrainz artist ID
|
||||
mb_artist_wikidata() {
|
||||
if ! cache_get_artist_wikidata "$1"; then
|
||||
wikidataid=$(mb_artist "$1" |
|
||||
@@ -76,8 +102,9 @@ mb_artist_wikidata() {
|
||||
fi
|
||||
}
|
||||
|
||||
# Get Wikipedia (English) summary json for artist
|
||||
# @argument $1: MusicBrainz Artist ID
|
||||
# Retrieve Wikipedia (English) summary json for artist
|
||||
#
|
||||
# @argument $1: MusicBrainz artist ID
|
||||
mb_artist_enwikipedia() {
|
||||
if ! cache_get_artist_enwikipedia "$1"; then
|
||||
# To fetch the wikipedia data, we need the wikipedia URL
|
||||
@@ -97,8 +124,9 @@ mb_artist_enwikipedia() {
|
||||
fi
|
||||
}
|
||||
|
||||
# Get Discogs json for artist
|
||||
# @argument $1: MusicBrainz Artist ID
|
||||
# Retrieve Discogs json for artist
|
||||
#
|
||||
# @argument $1: MusicBrainz artist ID
|
||||
mb_artist_discogs() {
|
||||
if ! cache_get_artist_discogs "$1"; then
|
||||
discogsid=$(mb_artist "$1" |
|
||||
@@ -112,22 +140,131 @@ mb_artist_discogs() {
|
||||
fi
|
||||
}
|
||||
|
||||
# Get release-groups json for artist
|
||||
# @argument $1: MusicBrainz Artist ID
|
||||
# Retrieve release groups for artist
|
||||
#
|
||||
# @argument $1: MusicBrainz artist ID
|
||||
mb_artist_releasegroups() {
|
||||
__mb_artist_cache_or_fetch_releasegroups "$1"
|
||||
}
|
||||
|
||||
# Get MusicBrainz json for release group
|
||||
# @argument $1: MusicBrainz Release-Group ID
|
||||
# Retrieve MusicBrainz release group
|
||||
#
|
||||
# @argument $1: MusicBrainz release-group ID
|
||||
mb_releasegroup() {
|
||||
__mb_releasegroup_cache_or_fetch "$1"
|
||||
}
|
||||
|
||||
# Retrieve MusicBrainz releases of release group
|
||||
#
|
||||
# @argument $1: MusicBrainz release-group ID
|
||||
mb_releasegroup_releases() {
|
||||
__mb_releasegroup_cache_or_fetch_releases "$1"
|
||||
}
|
||||
|
||||
# Retrieve MusicBrainz release
|
||||
#
|
||||
# @argument $1: MusicBrainz release ID
|
||||
mb_release() {
|
||||
__mb_release_cache_or_fetch "$1"
|
||||
}
|
||||
|
||||
# Reload hook that is used after a change in the query (when searching
|
||||
# MusicBrainz).
|
||||
#
|
||||
# This method waits for the search to complete, then it parses the search
|
||||
# results and prints them.
|
||||
mb_results_async() {
|
||||
# Wait for async. process to terminate
|
||||
sleep 1
|
||||
while [ -f "$LOCKFILE" ]; do
|
||||
sleep 1
|
||||
done
|
||||
# Show results
|
||||
column -t -s "$(printf '\t')" "$RESULTS" |
|
||||
sed 's| \+\([0-9a-f-]\+\) \+\([0-9a-f-]\+\)$|\t\1\t\2|'
|
||||
}
|
||||
|
||||
# Initiate search on MusicBrainz
|
||||
#
|
||||
# @argument $1: view
|
||||
#
|
||||
# This methods initiates an asynchronous search for both views
|
||||
# (VIEW_SEARCH_ARTIST and VIEW_SEARCH_ALBUM). If a running query is detected,
|
||||
# that one is killed first. The search results are then stored and become
|
||||
# retrievable using `mb_results_async`.
|
||||
mb_search_async() {
|
||||
view="$1"
|
||||
# Kill any running search
|
||||
if [ -f "$PIDFILE" ]; then
|
||||
pid=$(cat "$PIDFILE")
|
||||
rm -f "$PIDFILE"
|
||||
kill -9 "$pid" >/dev/null 2>&1 || true
|
||||
fi
|
||||
# Stop, if no search string is given
|
||||
[ "$FZF_QUERY" ] || exit 0
|
||||
# Store PID of current process
|
||||
echo "$$" >"$PIDFILE"
|
||||
touch "$LOCKFILE"
|
||||
sleep 1
|
||||
if [ "$view" = "$VIEW_SEARCH_ARTIST" ]; then
|
||||
api_mb_search_artist "$FZF_QUERY" |
|
||||
$JQ '.artists[] | [
|
||||
.id,
|
||||
.type,
|
||||
.name,
|
||||
.disambiguation,
|
||||
.["life-span"].begin,
|
||||
.["life-span"].end
|
||||
] | join("\t")' |
|
||||
awk \
|
||||
-F "\t" \
|
||||
-v file_local_artists="${LOCALDATA_ARTISTS:-}" \
|
||||
-v format_person="$AV_PERSON" \
|
||||
-v format_group="$AV_GROUP" \
|
||||
-v format_disambiguation="$AV_DISAMBIGUATION" \
|
||||
-v format_local="$FORMAT_LOCAL" \
|
||||
"$AWK_ARTISTS" >"$RESULTS" ||
|
||||
true
|
||||
else
|
||||
api_mb_search_releasegroup "$FZF_QUERY" |
|
||||
$JQ '."release-groups"[] | [
|
||||
.id,
|
||||
."primary-type",
|
||||
(."secondary-types" // []|join(";")),
|
||||
."first-release-date",
|
||||
.title,
|
||||
(."artist-credit" | map(([.name, .joinphrase]|join(""))) | join(""))
|
||||
] | join("\t")' |
|
||||
awk \
|
||||
-F "\t" \
|
||||
-v file_local_releasegroups="${LOCALDATA_RELEASEGROUPS:-}" \
|
||||
-v format_release="$RGV_RELEASE" \
|
||||
-v format_release_w_artist="$RGV_RELEASE_W_ARTIST" \
|
||||
-v format_year="$RGV_YEAR" \
|
||||
-v format_album="$FORMAT_TYPE_ALBUM" \
|
||||
-v format_single="$FORMAT_TYPE_SINGLE" \
|
||||
-v format_ep="$FORMAT_TYPE_EP" \
|
||||
-v format_broadcast="$FORMAT_TYPE_BROADCAST" \
|
||||
-v format_other="$FORMAT_TYPE_OTHER" \
|
||||
-v format_has_secondary="$FORMAT_TYPE_HAS_SECONDARY" \
|
||||
-v format_secondary="$FORMAT_TYPE_SECONDARY" \
|
||||
-v format_compilation="$FORMAT_TYPE_SECONDARY_COMPILATION" \
|
||||
-v format_soundtrack="$FORMAT_TYPE_SECONDARY_SOUNDTRACK" \
|
||||
-v format_spokenword="$FORMAT_TYPE_SECONDARY_SPOKENWORD" \
|
||||
-v format_interview="$FORMAT_TYPE_SECONDARY_INTERVIEW" \
|
||||
-v format_audiobook="$FORMAT_TYPE_SECONDARY_AUDIOBOOK" \
|
||||
-v format_audiodrama="$FORMAT_TYPE_SECONDARY_AUDIODRAMA" \
|
||||
-v format_live="$FORMAT_TYPE_SECONDARY_LIVE" \
|
||||
-v format_remix="$FORMAT_TYPE_SECONDARY_REMIX" \
|
||||
-v format_djmix="$FORMAT_TYPE_SECONDARY_DJMIX" \
|
||||
-v format_mixtape="$FORMAT_TYPE_SECONDARY_MIXTAPE" \
|
||||
-v format_demo="$FORMAT_TYPE_SECONDARY_DEMO" \
|
||||
-v format_fieldrec="$FORMAT_TYPE_SECONDARY_FIELDREC" \
|
||||
-v format_local="$FORMAT_LOCAL" \
|
||||
"$AWK_RELEASEGROUPS" |
|
||||
cut -d "$(printf '\t')" -f 2- >"$RESULTS" ||
|
||||
true
|
||||
fi
|
||||
# Process ends now: Display and quit
|
||||
rm -f "$LOCKFILE" "$PIDFILE"
|
||||
}
|
||||
|
Reference in New Issue
Block a user