From db313a8ee2bafd274f64e15dc8d27d235b75ff7d Mon Sep 17 00:00:00 2001 From: imSp4rky Date: Mon, 4 May 2026 22:21:03 -0600 Subject: [PATCH] refactor(routes, subtitle, track): improve code readability by formatting list structures --- unshackle/core/api/routes.py | 44 +++++++++++---------- unshackle/core/tracks/subtitle.py | 64 +++++++++++++++++-------------- unshackle/core/tracks/track.py | 40 ++++++++++--------- 3 files changed, 81 insertions(+), 67 deletions(-) diff --git a/unshackle/core/api/routes.py b/unshackle/core/api/routes.py index 5c10938..d466b40 100644 --- a/unshackle/core/api/routes.py +++ b/unshackle/core/api/routes.py @@ -1327,24 +1327,26 @@ def setup_swagger(app: web.Application) -> None: ) # Add routes with OpenAPI documentation - swagger.add_routes([ - web.get("/api/health", health), - web.get("/api/services", services), - web.post("/api/search", search), - web.post("/api/list-titles", list_titles), - web.post("/api/list-tracks", list_tracks), - web.post("/api/download", download), - web.get("/api/download/jobs", download_jobs), - web.get("/api/download/jobs/{job_id}", download_job_detail), - web.delete("/api/download/jobs/{job_id}", cancel_download_job), - # Remote-DL session endpoints - web.post("/api/session/create", session_create), - web.get("/api/session/{session_id}/titles", session_titles), - web.post("/api/session/{session_id}/tracks", session_tracks), - web.post("/api/session/{session_id}/segments", session_segments), - web.post("/api/session/{session_id}/license", session_license), - web.get("/api/session/{session_id}/prompt", session_prompt_get), - web.post("/api/session/{session_id}/prompt", session_prompt_submit), - web.get("/api/session/{session_id}", session_info), - web.delete("/api/session/{session_id}", session_delete), - ]) + swagger.add_routes( + [ + web.get("/api/health", health), + web.get("/api/services", services), + web.post("/api/search", search), + web.post("/api/list-titles", list_titles), + web.post("/api/list-tracks", list_tracks), + web.post("/api/download", download), + web.get("/api/download/jobs", download_jobs), + web.get("/api/download/jobs/{job_id}", download_job_detail), + web.delete("/api/download/jobs/{job_id}", cancel_download_job), + # Remote-DL session endpoints + web.post("/api/session/create", session_create), + web.get("/api/session/{session_id}/titles", session_titles), + web.post("/api/session/{session_id}/tracks", session_tracks), + web.post("/api/session/{session_id}/segments", session_segments), + web.post("/api/session/{session_id}/license", session_license), + web.get("/api/session/{session_id}/prompt", session_prompt_get), + web.post("/api/session/{session_id}/prompt", session_prompt_submit), + web.get("/api/session/{session_id}", session_info), + web.delete("/api/session/{session_id}", session_delete), + ] + ) diff --git a/unshackle/core/tracks/subtitle.py b/unshackle/core/tracks/subtitle.py index 280e34d..4ed84b4 100644 --- a/unshackle/core/tracks/subtitle.py +++ b/unshackle/core/tracks/subtitle.py @@ -461,15 +461,17 @@ class Subtitle(Track): content_lines.append(lines[i]) i += 1 - cues.append({ - "start_ms": Subtitle._parse_vtt_time(start_str), - "end_ms": Subtitle._parse_vtt_time(end_str), - "start_str": start_str, - "end_str": end_str, - "line_pos": line_pos, - "content": "\n".join(content_lines), - "settings": settings, - }) + cues.append( + { + "start_ms": Subtitle._parse_vtt_time(start_str), + "end_ms": Subtitle._parse_vtt_time(end_str), + "start_str": start_str, + "end_str": end_str, + "line_pos": line_pos, + "content": "\n".join(content_lines), + "settings": settings, + } + ) else: i += 1 @@ -494,19 +496,23 @@ class Subtitle(Track): group.sort(key=lambda x: x["line_pos"]) # Use the earliest start time from the group earliest = min(group, key=lambda x: x["start_ms"]) - merged_cues.append({ - "start_str": earliest["start_str"], - "end_str": group[0]["end_str"], - "content": "\n".join(c["content"] for c in group), - "settings": "", - }) + merged_cues.append( + { + "start_str": earliest["start_str"], + "end_str": group[0]["end_str"], + "content": "\n".join(c["content"] for c in group), + "settings": "", + } + ) else: - merged_cues.append({ - "start_str": current["start_str"], - "end_str": current["end_str"], - "content": current["content"], - "settings": current["settings"], - }) + merged_cues.append( + { + "start_str": current["start_str"], + "end_str": current["end_str"], + "content": current["content"], + "settings": current["settings"], + } + ) i = j if len(group) > 1 else i + 1 @@ -1145,14 +1151,16 @@ class Subtitle(Track): if cue_box.type == b"sttg": layout = Layout(webvtt_positioning=cue_box.settings) elif cue_box.type == b"payl": - nodes.extend([ - node - for line in cue_box.cue_text.split("\n") - for node in [ - CaptionNode.create_text(WebVTTReader()._decode(line)), - CaptionNode.create_break(), + nodes.extend( + [ + node + for line in cue_box.cue_text.split("\n") + for node in [ + CaptionNode.create_text(WebVTTReader()._decode(line)), + CaptionNode.create_break(), + ] ] - ]) + ) nodes.pop() if nodes: diff --git a/unshackle/core/tracks/track.py b/unshackle/core/tracks/track.py index 4198c9b..4a43e22 100644 --- a/unshackle/core/tracks/track.py +++ b/unshackle/core/tracks/track.py @@ -687,25 +687,29 @@ class Track: if hasattr(self, "data") and self.data.get("audio_language"): audio_lang = self.data["audio_language"] audio_name = self.data.get("audio_language_name", audio_lang) - args.extend([ - "-metadata:s:a:0", - f"language={audio_lang}", - "-metadata:s:a:0", - f"title={audio_name}", - "-metadata:s:a:0", - f"handler_name={audio_name}", - ]) + args.extend( + [ + "-metadata:s:a:0", + f"language={audio_lang}", + "-metadata:s:a:0", + f"title={audio_name}", + "-metadata:s:a:0", + f"handler_name={audio_name}", + ] + ) - args.extend([ - # Following are very important! - "-map_metadata", - "-1", # don't transfer metadata to output file - "-fflags", - "bitexact", # only have minimal tag data, reproducible mux - "-codec", - "copy", - str(output_path), - ]) + args.extend( + [ + # Following are very important! + "-map_metadata", + "-1", # don't transfer metadata to output file + "-fflags", + "bitexact", # only have minimal tag data, reproducible mux + "-codec", + "copy", + str(output_path), + ] + ) subprocess.run( args,