imprv: faster playlist handling and a few bugs fixed

This commit is contained in:
2025-10-13 22:07:26 +02:00
parent 3b66faed6f
commit c0463c0d5e
7 changed files with 183 additions and 82 deletions

View File

@@ -238,6 +238,18 @@ cache_get_file_batch() {
awk -v dir="$CACHEDIR/$1/" -v f="/$fn" '{ print dir $0 f }'
}
# Detect missing cache files
#
# @argument $1: type
# @argument $2: path to list with MusicBrainz IDs
#
# This method returns a nonzero value if some MusicBrainz objects listed in $2
# are not cached.
cached() {
cache_get_file_batch "$1" <"$2" |
xargs -d '\n' ls >/dev/null 2>&1 || return 1
}
# Print MusicBrainz ID associated to the file paths
#
# This reads from stdin any number of paths (one per line)
@@ -295,3 +307,41 @@ cache_rm_release() {
info "Removing $d from cache"
rm -rf "$d"
}
# Load missing cache entries (batch mode)
#
# argument $1: type
#
# This method reads one MusicBrainz IDs of the specified type from stdin (one
# per line), and fetches the missing items.
batch_load_missing() {
tmpf=$(mktemp)
cat |
cache_get_file_batch "$1" |
xargs -d '\n' \
sh -c 'for f; do [ -e "$f" ] || echo "$f"; done' _ |
cache_mbid_from_path_batch >"$tmpf"
lines=$(wc -l "$tmpf" | cut -d ' ' -f 1)
if [ "$lines" -gt 0 ]; then
case "$1" in
"$TYPE_ARTIST") tt="artists" ;;
"$TYPE_RELEASEGROUP") tt="release groups" ;;
"$TYPE_RELEASE") tt="releases" ;;
esac
info "Fetching missing $tt"
cnt=0
while IFS= read -r mbid; do
case "$1" in
"$TYPE_ARTIST")
name=$(mb_artist "$mbid" | $JQ '.name')
;;
"$TYPE_RELEASEGROUP") name=$(mb_releasegroup "$mbid" | $JQ '.title') ;;
"$TYPE_RELEASE") name=$(mb_release "$mbid" | $JQ '.title') ;;
esac
cnt=$((cnt + 1))
info "$(printf "%d/%d (%s: %s)" "$cnt" "$lines" "$mbid" "$name")"
sleep 1
done <"$tmpf"
fi
rm -f "$tmpf"
}