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
|
||||
|
@@ -4,7 +4,6 @@ from urllib.parse import urlparse
|
||||
import hashlib
|
||||
import logging
|
||||
import os
|
||||
import asyncio
|
||||
import aiohttp
|
||||
from streamrip.client import TidalClient # type: ignore
|
||||
from streamrip.config import Config as StreamripConfig # type: ignore
|
||||
@@ -135,7 +134,8 @@ class SRUtil:
|
||||
logging.debug("Retrieved albums: %s", albums_out)
|
||||
return albums_out
|
||||
|
||||
async def get_tracks_by_album_id(self, album_id: int) -> Optional[list | dict]:
|
||||
async def get_tracks_by_album_id(self, album_id: int,
|
||||
quality: str = "FLAC") -> Optional[list | dict]:
|
||||
"""Get tracks by album ID
|
||||
Args:
|
||||
album_id (int): The ID of the album.
|
||||
@@ -177,7 +177,7 @@ class SRUtil:
|
||||
return []
|
||||
|
||||
async def get_stream_url_by_track_id(
|
||||
self, track_id: int, quality: str = "LOSSLESS"
|
||||
self, track_id: int, quality: str = "FLAC"
|
||||
) -> Optional[str]:
|
||||
"""Get stream URL by track ID
|
||||
Args:
|
||||
@@ -186,19 +186,21 @@ class SRUtil:
|
||||
Returns:
|
||||
Optional[str]: The stream URL or None if not found.
|
||||
"""
|
||||
if quality not in ["LOSSLESS", "HIGH", "LOW"]:
|
||||
if quality not in ["FLAC", "Lossy"]:
|
||||
logging.error("Invalid quality requested: %s", quality)
|
||||
return None
|
||||
quality_int: int = int(self.streamrip_config.session.tidal.quality)
|
||||
match quality:
|
||||
case "HIGH":
|
||||
case "FLAC":
|
||||
quality_int = 2
|
||||
case "Lossy":
|
||||
quality_int = 1
|
||||
case "LOW":
|
||||
quality_int = 0
|
||||
track_id_str: str = str(track_id)
|
||||
|
||||
await self.streamrip_client.login()
|
||||
|
||||
try:
|
||||
logging.critical("Using quality_int: %s", quality_int)
|
||||
track = await self.streamrip_client.get_downloadable(
|
||||
track_id=track_id_str, quality=quality_int
|
||||
)
|
||||
|
Reference in New Issue
Block a user