From f69ebb4fbae4f936f7d72bdd16ac2b031d6ac454 Mon Sep 17 00:00:00 2001 From: imSp4rky Date: Wed, 1 Jul 2026 18:46:41 -0600 Subject: [PATCH] feat(api): report per-episode and per-track progress on download jobs Adds title, current_title, incremental output_files, and track_progress to job status so clients can render live multi-episode progress. --- unshackle/commands/dl.py | 14 ++++++++++++++ unshackle/core/api/download_manager.py | 14 ++++++++++++++ unshackle/core/api/download_worker.py | 6 +++++- unshackle/core/api/progress.py | 5 +++++ 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/unshackle/commands/dl.py b/unshackle/commands/dl.py index efae7526..024e5a5e 100644 --- a/unshackle/commands/dl.py +++ b/unshackle/commands/dl.py @@ -1987,6 +1987,17 @@ class dl: elif isinstance(title, Episode) and wanted and f"{title.season}x{title.number}" not in wanted: continue + if progress_sink: + if isinstance(title, Episode): + progress_sink( + { + "title": title.title, + "current_title": f"S{title.season or 0:02}E{title.number or 0:02}", + } + ) + else: + progress_sink({"title": getattr(title, "name", None) or str(title)}) + title_rule = ( f"Track {title.track:02}: {title.name}" if music_mode and isinstance(title, Song) else str(title) ) @@ -3440,6 +3451,9 @@ class dl: ) ) + if progress_sink: + progress_sink({"output_files": [str(p) for p in self.completed_files]}) + if not hasattr(service, "close"): cookie_file = self.get_cookie_path(self.service, self.profile) if cookie_file: diff --git a/unshackle/core/api/download_manager.py b/unshackle/core/api/download_manager.py index 0bcc416b..f94740a0 100644 --- a/unshackle/core/api/download_manager.py +++ b/unshackle/core/api/download_manager.py @@ -101,9 +101,12 @@ class DownloadJob: # Current phase, track counts, and labels of the tracks downloading now. phase: Optional[str] = None + title: Optional[str] = None + current_title: Optional[str] = None completed_tracks: int = 0 total_tracks: int = 0 active_tracks: List[str] = field(default_factory=list) + track_progress: List[Dict[str, Any]] = field(default_factory=list) # Segment counts and transfer speed of the track downloading now (granular display) segments_done: float = 0.0 segments_total: float = 0.0 @@ -124,11 +127,14 @@ class DownloadJob: "created_time": self.created_time.isoformat(), "service": self.service, "title_id": self.title_id, + "title": self.title, "progress": self.progress, "phase": self.phase, + "current_title": self.current_title, "completed_tracks": self.completed_tracks, "total_tracks": self.total_tracks, "active_tracks": self.active_tracks, + "track_progress": self.track_progress, "segments_done": self.segments_done, "segments_total": self.segments_total, "speed": self.speed, @@ -734,12 +740,20 @@ class DownloadQueueManager: progress_data = json.load(handle) if progress_data.get("phase") and progress_data["phase"] != job.phase: job.phase = progress_data["phase"] + if progress_data.get("current_title"): + job.current_title = str(progress_data["current_title"]) + if progress_data.get("title"): + job.title = str(progress_data["title"]) + if progress_data.get("output_files"): + job.output_files = [str(f) for f in progress_data["output_files"]] if progress_data.get("total_tracks"): job.total_tracks = int(progress_data["total_tracks"]) if progress_data.get("completed_tracks") is not None: job.completed_tracks = int(progress_data["completed_tracks"]) if "active_tracks" in progress_data: job.active_tracks = list(progress_data["active_tracks"]) + if "track_progress" in progress_data: + job.track_progress = list(progress_data["track_progress"]) for _sk in ("segments_done", "segments_total"): if _sk in progress_data: try: diff --git a/unshackle/core/api/download_worker.py b/unshackle/core/api/download_worker.py index 8d533f09..148483f5 100644 --- a/unshackle/core/api/download_worker.py +++ b/unshackle/core/api/download_worker.py @@ -49,12 +49,16 @@ def main(argv: list[str]) -> int: log.info(f"Worker starting job {job_id} ({service}:{title_id})") + # Merged so sparse keys (current_title, output_files) survive later writes. + progress_state: Dict[str, Any] = {} + def progress_callback(progress_data: Dict[str, Any]) -> None: """Write progress updates to file for main process to read.""" if progress_path: try: + progress_state.update(progress_data) log.info(f"Writing progress update: {progress_data}") - _write_result(progress_path, progress_data) + _write_result(progress_path, progress_state) log.info(f"Progress update written to {progress_path}") except Exception as e: log.error(f"Failed to write progress update: {e}") diff --git a/unshackle/core/api/progress.py b/unshackle/core/api/progress.py index a9ff6c43..eab489b9 100644 --- a/unshackle/core/api/progress.py +++ b/unshackle/core/api/progress.py @@ -94,6 +94,11 @@ def build_job_progress_callables( "completed_tracks": completed, "total_tracks": total, "active_tracks": active, + "track_progress": [ + {"label": labels[i], "progress": round(fractions[i] * 100, 1), "speed": speeds[i]} + for i in range(total) + if started[i] and not done[i] + ], "segments_done": seg_done[active_i] if active_i is not None else 0.0, "segments_total": seg_total[active_i] if active_i is not None else 0.0, "speed": speeds[active_i] if active_i is not None else None,