another commit without a list of specific changes! (misc)
This commit is contained in:
@@ -39,6 +39,23 @@ sr = SRUtil()
|
||||
|
||||
|
||||
# ---------- Helpers ----------
|
||||
|
||||
def cleanup_empty_dirs(root: Path):
|
||||
"""
|
||||
Recursively remove any directories under root that contain no files
|
||||
(empty or only empty subdirectories).
|
||||
"""
|
||||
for dirpath, dirnames, filenames in os.walk(root, topdown=False):
|
||||
p = Path(dirpath)
|
||||
# Check if there are any files in this directory or subdirectories
|
||||
has_file = any(f.is_file() for f in p.rglob("*"))
|
||||
if not has_file:
|
||||
try:
|
||||
p.rmdir() # safe to remove empty dirs
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
def sanitize_filename(name: str) -> str:
|
||||
"""Make a string safe for file/dir names."""
|
||||
if not name:
|
||||
@@ -64,7 +81,7 @@ def ensure_unique_path(p: Path) -> Path:
|
||||
return parent / f"{stem}_{short_id}{suffix}"
|
||||
|
||||
# ---------- Job ----------
|
||||
def bulk_download(track_list: list):
|
||||
def bulk_download(track_list: list, quality: str = "FLAC"):
|
||||
"""
|
||||
RQ job:
|
||||
- fetches stream URLs
|
||||
@@ -118,7 +135,7 @@ def bulk_download(track_list: list):
|
||||
|
||||
try:
|
||||
# 1) Stream URL
|
||||
url = await sr.get_stream_url_by_track_id(track_id)
|
||||
url = await sr.get_stream_url_by_track_id(track_id, quality)
|
||||
if not url:
|
||||
raise RuntimeError("No stream URL")
|
||||
|
||||
@@ -228,7 +245,7 @@ def bulk_download(track_list: list):
|
||||
|
||||
# Stage tarball in ROOT_DIR first
|
||||
staged_tarball = ROOT_DIR / f"{combined_artist}_{short_id}.tar.gz"
|
||||
final_tarball = ROOT_DIR / "completed" / staged_tarball.name
|
||||
final_tarball = ROOT_DIR / "completed" / quality / staged_tarball.name
|
||||
final_tarball.parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
if job:
|
||||
@@ -276,6 +293,7 @@ def bulk_download(track_list: list):
|
||||
shutil.move(str(staged_tarball), str(final_tarball))
|
||||
|
||||
logging.critical("Tarball finalized: %s", final_tarball)
|
||||
await asyncio.to_thread(cleanup_empty_dirs, ROOT_DIR)
|
||||
if job:
|
||||
job.meta["tarball"] = str(final_tarball)
|
||||
job.meta["progress"] = 100
|
||||
|
Reference in New Issue
Block a user