From fe68810e0f7ee79506e57f540f284f8900440f57 Mon Sep 17 00:00:00 2001 From: Lucas Serverhost Date: Fri, 31 Jul 2026 13:46:39 +0200 Subject: [PATCH] metadata script metadata remove genre: script to remove keywords ie audiobook from genre rank from list: change pattern and separate author and title, rank is str now --- quiethost/metadata_rank_from_list.py | 18 ++--- quiethost/metadata_remove_genre.sh | 103 +++++++++++++++++++++++++++ 2 files changed, 113 insertions(+), 8 deletions(-) create mode 100644 quiethost/metadata_remove_genre.sh diff --git a/quiethost/metadata_rank_from_list.py b/quiethost/metadata_rank_from_list.py index d04d06a..58173c9 100644 --- a/quiethost/metadata_rank_from_list.py +++ b/quiethost/metadata_rank_from_list.py @@ -70,9 +70,11 @@ def tag_file(path: Path, rank: int, year: str, dry_run: bool): else: tag = "grouping" existing = "" - pattern = r"Top 100 SciFi Books #\d+" -# pattern = r"Enderverse" - replace = r"Top 100 SciFi Books" + #pattern = r"Top 100 SciFi Books #\d+(?:\.\d+)?" + pattern = r"Enderverse Publication #\d+(?:\.\d+)?" + #replace = r"Top 100 SciFi Books" + replace = r"Enderverse Publication" + if existing: if re.search(pattern, existing): new_group = re.sub( @@ -142,9 +144,9 @@ def main(): parts = line.split(" - ") if len(parts) == 4: - rank, author, title, year = parts + rank, title, author, year = parts elif len(parts) == 3: - rank, author, title = parts + rank, title, author = parts year = None else: raise ValueError(f"Invalid line: {line}") @@ -186,7 +188,7 @@ def main(): found.add(rank) - print(f"{GREEN}FOUND{RESET} #{int(rank):3} {title} in {book_dir}") + print(f"{GREEN}FOUND{RESET} #{rank:3} {title} in {book_dir}") if args.find_missing: continue @@ -195,10 +197,10 @@ def main(): if file.suffix.lower() not in AUDIO_EXTENSIONS: continue - ret=tag_file(file, int(rank), year, args.dry_run) + ret=tag_file(file, rank, year, args.dry_run) if ret == "skip": break - # print("next") + #print("next") if __name__ == "__main__": main() diff --git a/quiethost/metadata_remove_genre.sh b/quiethost/metadata_remove_genre.sh new file mode 100644 index 0000000..4c30f87 --- /dev/null +++ b/quiethost/metadata_remove_genre.sh @@ -0,0 +1,103 @@ +#!/usr/bin/env bash + +LIBRARY="$1" +MODE="${2:-}" +REPORT="${3:-/home/lucas/audiobook_genre_cleanup.txt}" + +if [[ -z "$LIBRARY" ]]; then + echo "Usage: $0 [--delete] [report_file]" + exit 1 +fi + +mapfile -t FILES < <( + find "$LIBRARY" -type f \( \ + -iname "*.mp3" -o \ + -iname "*.m4b" -o \ + -iname "*.m4a" \ + \) +) + +TOTAL=${#FILES[@]} +PROCESSED=0 +MATCHES=0 +MODIFIED=0 + +{ + echo "Audiobook Genre Cleanup Report" + echo "Generated: $(date)" + echo "Library: $LIBRARY" + echo "Total files: $TOTAL" + echo +} > "$REPORT" + +for file in "${FILES[@]}"; do + ((PROCESSED++)) + + if (( PROCESSED % 100 == 0 )); then + echo "Progress: $PROCESSED/$TOTAL files processed" + fi + genre=$(kid3-cli -c "get genre" "$file" 2>/dev/null) + + [[ -z "$genre" ]] && continue + cleaned=$(printf '%s\n' "$genre" | sed -E ' + s/[Aa]udio[[:space:]]*[Bb]ooks?//g + + # Normalize genre names + s/\b(Sci[ -]?Fi|Science Fiction|SF)\b/SciFi/Ig + s/\bnonfiction\b/NonFiction/Ig + + # Normalize separators (&, :, / -> ;) + s/[[:space:]]*[,&:\/][[:space:]]*/;/g + + # Normalize commas + s/[[:space:]]*;[[:space:]]*/; /g + + # Trim leading/trailing punctuation and whitespace + s/^[[:space:],;:-]+// + s/[[:space:],;-]+$// + ') +# s/.*//; + + if [[ "$genre" != "$cleaned" ]]; then + ((MATCHES++)) + + status="MATCH" + + if [[ "$MODE" == "--delete" ]]; then + printf "D " + kid3-cli \ + -c "select-tag 1" \ + -c "set genre \"$cleaned\"" \ + -c "select-tag 2" \ + -c "set genre \"$cleaned\"" \ + -c save \ + "$file" >/dev/null + + ((MODIFIED++)) + status="UPDATED" + fi + + printf '[%d/%d] [%s] %s | OLD="%s" | NEW="%s"\n' \ + "$PROCESSED" "$TOTAL" "$status" "$file" "$genre" "$cleaned" + + printf '[%d/%d] [%s] %s | OLD="%s" | NEW="%s"\n' \ + "$PROCESSED" "$TOTAL" "$status" "$file" "$genre" "$cleaned" >> "$REPORT" + fi + +done + +{ + echo + echo "========== SUMMARY ==========" + echo "Files scanned : $TOTAL" + echo "Matches found : $MATCHES" + echo "Files updated : $MODIFIED" +} >> "$REPORT" + +echo +echo "========== SUMMARY ==========" +echo "Files scanned : $TOTAL" +echo "Matches found : $MATCHES" +echo "Files updated : $MODIFIED" +echo "Report written to: $REPORT" +