From 712373e582134051ecaf470f06abbe4ea80e3b62 Mon Sep 17 00:00:00 2001 From: Gabe Farrell Date: Sat, 10 May 2025 09:17:21 +0000 Subject: [PATCH] add music import script --- .import_music.sh.swp | Bin 12288 -> 0 bytes import_music.sh | 127 ++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 118 insertions(+), 9 deletions(-) delete mode 100644 .import_music.sh.swp diff --git a/.import_music.sh.swp b/.import_music.sh.swp deleted file mode 100644 index c237ff73a94c0570102b1a483ada86773acfbe6a..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 12288 zcmeI&F>ljA6ae5W3+>QS!N!1INrzSeDX*yhu)e#OgG_RL<96HX`E_(aP>VdC;uHQpskH@5YsdhArbE;S?5Nfg1{} ziYJeE2VryX;Rf8hdve1%yp07|fCX591z3OuSbzmsU=am;vm`!JAg4=8?6;sr%Yu|ly-5o;`<0k_NoEWiRRzyd750xZA+EWiT)l|T>rklSVoTebyl zKS-D;UqQbgK5v7sB*ZCg@D?`Hlh?!Nr-#F%G+`Q)I^##;1X#aJ)*Vm5{)DpJ?r ze0a|!G&Sb3fUzlL8S1DctJdV1aLN5lrq8{I4gGM?D z*yV&Ow7m}+eO~>$JKhb#nXwZCHT4X(gv-m%6>^UNO%W??*~Tktu!czzIKrrU)}k`! z+c2gpr(xk)Tz~>S2B6St=(1F`bg@dv)Y4-~0wDv70^&VRVMMj!5*b}?D3u9N-GDs# zboi9HIZR)@JUR~gRQb444!UXjXuU_Hz0?+t=vSB /dev/null +} + +# Function to find best match directory in LIBRARY_DIR containing a given substring +find_matching_dir() { + local search_root="$1" + local pattern="$2" + match=$(find "$search_root" -mindepth 1 -maxdepth 1 -type d -print | grep -iF "$pattern" | head -n 1) + + if [[ -n "$match" ]]; then + basename "$match" + else + echo "$pattern" + fi +} + +# Choose copy or move command +COPY_CMD="cp" +$MOVE_FILES && COPY_CMD="mv" + +# Process FLAC files +find "$IMPORT_DIR" -type f -name "*.flac" | while read -r file; do + artist=$(metaflac --show-tag=ALBUMARTIST "$file" | sed 's/^ALBUMARTIST=//') + album=$(metaflac --show-tag=ALBUM "$file" | sed 's/^ALBUM=//') + + resolved_artist="$artist" + resolved_album="$album" + + if contains_non_latin "$artist"; then + resolved_artist=$(find_matching_dir "$LIBRARY_DIR" "$artist") + fi + + artist_dir="$LIBRARY_DIR/$resolved_artist" + + if contains_non_latin "$album"; then + resolved_album=$(find_matching_dir "$artist_dir" "$album") + fi + + target_dir="$LIBRARY_DIR/$resolved_artist/$resolved_album" + mkdir -p "$target_dir" + $COPY_CMD "$file" "$target_dir/" + echo "$COPY_CMD $file to $target_dir" +done + +# Also move/copy associated image files (cover art & artist images) +find "$IMPORT_DIR" -type f \( -iname "*.jpg" -o -iname "*.png" \) | while read -r img_file; do + dir_path=$(dirname "$img_file") + artist=$(metaflac --show-tag=ALBUMARTIST "$dir_path"/*.flac 2>/dev/null | head -n1 | sed 's/^ALBUMARTIST=//') + album=$(metaflac --show-tag=ALBUM "$dir_path"/*.flac 2>/dev/null | head -n1 | sed 's/^ALBUM=//') + + resolved_artist="$artist" + resolved_album="$album" + + if contains_non_latin "$artist"; then + resolved_artist=$(find_matching_dir "$LIBRARY_DIR" "$artist") + fi + + artist_dir="$LIBRARY_DIR/$resolved_artist" + + if contains_non_latin "$album"; then + resolved_album=$(find_matching_dir "$artist_dir" "$album") + fi + + target_dir="$LIBRARY_DIR/$resolved_artist/$resolved_album" + mkdir -p "$target_dir" + $COPY_CMD "$img_file" "$target_dir/" + echo "$COPY_CMD $img_file to $target_dir" +done + +echo "Import completed."