From 7f94ab9646f484808f3f86543d57ccb04181a99c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=84min=20Baumeler?= Date: Mon, 4 Aug 2025 15:56:47 +0200 Subject: [PATCH] --decorate --- src/main.sh | 41 ++++++++++++++++++++++++++++++----------- src/sh/config.sh | 7 +++++++ src/sh/local.sh | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 11 deletions(-) create mode 100644 src/sh/config.sh create mode 100644 src/sh/local.sh diff --git a/src/main.sh b/src/main.sh index 49643d3..2f53510 100755 --- a/src/main.sh +++ b/src/main.sh @@ -8,6 +8,9 @@ set -eu # Load helper methods . "sh/helper.sh" +# Load configuration +. "sh/config.sh" + # Load theme . "sh/theme.sh" @@ -29,6 +32,22 @@ set -eu # Load MusicBrainz wrappers . "sh/mb.sh" +# Load local file handling +. "sh/local.sh" + +# Support of local music files +if [ "${MUSICDIR:-}" ]; then + if [ "${1:-}" = "--decorate" ]; then + [ ! "${2:-}" ] && err "You did not specify a directory." && exit 1 + [ ! -d "$2" ] && err "Path $2 does not point to a directory." && exit 1 + if ! decorate "$2"; then + err "Something went wrong. Are you're files tagged correctly?" + exit 1 + fi + exit 0 + fi +fi + if [ "${1:-}" = "--internal-preview-artist" ]; then __preview_artist "$2" exit 0 @@ -177,17 +196,17 @@ if [ "${1:-}" = "--internal-list-releases" ]; then $JQ -r '."artist-credit" | map(([.name, .joinphrase]|join(""))) | join("")')" mb_releasegroup_releases "$2" | $JQ -r '."releases"[] | [ - .id, - .status, - .date, - ."cover-art-archive".count, - (."label-info" | map(.label.name) | unique | join(", ")), - (.media | map(."track-count") | add), - (.media | map(.format) | unique | join(", ")), - .country, - .title, - (."artist-credit" | map(([.name, .joinphrase]|join(""))) | join("")) - ] | join("\t")' | + .id, + .status, + .date, + ."cover-art-archive".count, + (."label-info" | map(.label.name) | unique | join(", ")), + (.media | map(."track-count") | add), + (.media | map(.format) | unique | join(", ")), + .country, + .title, + (."artist-credit" | map(([.name, .joinphrase]|join(""))) | join("")) + ] | join("\t")' | awk \ -F "\t" \ -v release_official="$FORMAT_STATUS_OFFICIAL" \ diff --git a/src/sh/config.sh b/src/sh/config.sh new file mode 100644 index 0000000..045aac7 --- /dev/null +++ b/src/sh/config.sh @@ -0,0 +1,7 @@ +if [ "${CONFIGFILE:-}" ]; then + [ ! -f "$CONFIGFILE" ] && err "Configuration $CONFIGFILE not found." && exit 1 + . "$CONFIGFLIE" + [ ! "${MUSICDIR:-}" ] && err "MUSICDIR configuration not set." && exit 1 +fi +[ "${MUSICDIR:-}" ] && [ ! -d "${MUSICDIR:-}" ] && err "The specified path ${MUSICDIR:-} is not a directory." && exit 1 +DECORATION_FILENAME=${DECORATION_FILENAME:-"mbid.json"} diff --git a/src/sh/local.sh b/src/sh/local.sh new file mode 100644 index 0000000..7cf9a7f --- /dev/null +++ b/src/sh/local.sh @@ -0,0 +1,42 @@ +gettags() { + ffprobe -v error -show_entries format_tags -print_format json "$1" | + $JQ -r --compact-output '.format.tags | { + trackid: (."MusicBrainz Release Track Id" // ."MUSICBRAINZ_RELEASETRACKID" // ."MusicBrainz/Release Track Id" // ""), + releaseid: (."MusicBrainz Album Id" // ."MUSICBRAINZ_ALBUMID" // ."MusicBrainz/Album Id" // "") + }' +} + +# Read music files in specified directory and create json file that points to +# all relevant MusicBrainz IDs. +# @input $1: Path to directory with album +decorate() { + decoration=$($JQ -n '.tracks = {}') + tmpf=$(mktemp) + (cd "$1" && find . -type f -iname '*.mp3' -o -iname '*.mp4' -o -iname '*.flac' -o -iname '*.m4a') >"$tmpf" + while IFS= read -r f; do + mbid=$(gettags "$1/$f") + rid=$(echo "$mbid" | $JQ -r '.releaseid') + tid=$(echo "$mbid" | $JQ -r '.trackid') + if [ ! "$rid" ] || [ ! "$tid" ]; then + err "File $f: Seems not tagged" + releaseid="" + break + fi + if [ "${releaseid:-}" ]; then + if [ "$releaseid" != "$rid" ]; then + err "Directory $1 contains files of different releases" + releaseid="" + break + fi + else + releaseid="$rid" + fi + decoration=$(echo "$decoration" | $JQ ".tracks += {\"$tid\": \"$f\"}") + done <"$tmpf" + rm -f "$tmpf" + if [ "$releaseid" ]; then + echo "$decoration" | $JQ --compact-output ".releaseid = \"$releaseid\"" >"$1/$DECORATION_FILENAME" + else + return 1 + fi +}