From e8cf98708b53c58af572214475b2531b11210127 Mon Sep 17 00:00:00 2001 From: "Roncero Blanco, Edgar" Date: Mon, 26 May 2025 13:35:48 +0200 Subject: [PATCH] Add run_date variable, prompt for source path, auto-generate destination folder with date-based name --- src/app.py | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/src/app.py b/src/app.py index b023c01..8c29238 100644 --- a/src/app.py +++ b/src/app.py @@ -2,13 +2,28 @@ import os import shutil from pathlib import Path from mutagen.id3 import ID3 +from datetime import datetime +from collections import defaultdict -CD_SIZE = 700 * 1024 * 1024 # 700 MB +CD_SIZE = 695 * 1024 * 1024 # 695 MB GROUP_SIZE = 5 -dir_a = Path(r"C:\Users\Edgar\Desktop\Peak Time BOF (ed1337x)\Already where in MP3") -dir_b = Path(r"C:\Users\Edgar\Desktop\Peak Time BOF (ed1337x)\CD") -dir_b.mkdir(parents=True, exist_ok=True) +# Store the current date +run_date = datetime.now().strftime("%Y-%m-%d") + +# Ask for source path +source_input = input("Enter the full path to your source MP3 folder: ").strip() +source_media_path = Path(source_input) + +if not source_media_path.exists() or not source_media_path.is_dir(): + print(f"Error: {source_media_path} is not a valid directory.") + exit(1) + +# Build destination path +parent = source_media_path.parent +folder_name = source_media_path.name +dest_media_path = parent / f"[CDs-{run_date}]{folder_name}" +dest_media_path.mkdir(parents=True, exist_ok=True) def get_bpm(file_path): try: @@ -22,7 +37,7 @@ def get_bpm(file_path): # Collect all tracks all_tracks = [] -for file in dir_a.glob("*.mp3"): +for file in source_media_path.glob("*.mp3"): bpm = get_bpm(file) if bpm is None: continue @@ -32,23 +47,21 @@ for file in dir_a.glob("*.mp3"): # Calculate total size and number of CDs needed total_size = sum(t["size"] for t in all_tracks) -num_cds = max(1, (total_size + CD_SIZE - 1) // CD_SIZE) # ceiling division +num_cds = max(1, (total_size + CD_SIZE - 1) // CD_SIZE) print(f"Total size: {total_size / 1024**2:.2f} MB, splitting into {num_cds} CDs") # Group tracks by BPM range -from collections import defaultdict bpm_groups = defaultdict(list) for track in all_tracks: bpm_groups[track["bpm_range"]].append(track) -# Sort each bpm group by filename (or size if you want) +# Sort each bpm group for bpm_range in bpm_groups: bpm_groups[bpm_range].sort(key=lambda x: x["file"].name) # Split each BPM group evenly into num_cds parts def split_evenly(lst, n): - """Split list lst into n chunks as evenly as possible by count.""" k, m = divmod(len(lst), n) return [lst[i*k + min(i, m):(i+1)*k + min(i+1, m)] for i in range(n)] @@ -59,14 +72,14 @@ for bpm_range, tracks in bpm_groups.items(): # Prepare CDs cd_contents = [[] for _ in range(num_cds)] -# Fill CDs with chunks from each BPM range +# Fill CDs with balanced BPM chunks for bpm_range in bpm_chunks: for i, chunk in enumerate(bpm_chunks[bpm_range]): cd_contents[i].extend(chunk) # Write CDs with BPM subfolders for i, tracks in enumerate(cd_contents, start=1): - cd_folder = dir_b / f"CD-{i:02}" + cd_folder = dest_media_path / f"CD-{i:02}" cd_folder.mkdir(parents=True, exist_ok=True) size_accum = 0 for track in tracks: