refactor(routes, subtitle, track): improve code readability by formatting list structures

This commit is contained in:
imSp4rky
2026-05-04 22:21:03 -06:00
parent a7af898617
commit db313a8ee2
3 changed files with 81 additions and 67 deletions

View File

@@ -1327,24 +1327,26 @@ def setup_swagger(app: web.Application) -> None:
) )
# Add routes with OpenAPI documentation # Add routes with OpenAPI documentation
swagger.add_routes([ swagger.add_routes(
web.get("/api/health", health), [
web.get("/api/services", services), web.get("/api/health", health),
web.post("/api/search", search), web.get("/api/services", services),
web.post("/api/list-titles", list_titles), web.post("/api/search", search),
web.post("/api/list-tracks", list_tracks), web.post("/api/list-titles", list_titles),
web.post("/api/download", download), web.post("/api/list-tracks", list_tracks),
web.get("/api/download/jobs", download_jobs), web.post("/api/download", download),
web.get("/api/download/jobs/{job_id}", download_job_detail), web.get("/api/download/jobs", download_jobs),
web.delete("/api/download/jobs/{job_id}", cancel_download_job), web.get("/api/download/jobs/{job_id}", download_job_detail),
# Remote-DL session endpoints web.delete("/api/download/jobs/{job_id}", cancel_download_job),
web.post("/api/session/create", session_create), # Remote-DL session endpoints
web.get("/api/session/{session_id}/titles", session_titles), web.post("/api/session/create", session_create),
web.post("/api/session/{session_id}/tracks", session_tracks), web.get("/api/session/{session_id}/titles", session_titles),
web.post("/api/session/{session_id}/segments", session_segments), web.post("/api/session/{session_id}/tracks", session_tracks),
web.post("/api/session/{session_id}/license", session_license), web.post("/api/session/{session_id}/segments", session_segments),
web.get("/api/session/{session_id}/prompt", session_prompt_get), web.post("/api/session/{session_id}/license", session_license),
web.post("/api/session/{session_id}/prompt", session_prompt_submit), web.get("/api/session/{session_id}/prompt", session_prompt_get),
web.get("/api/session/{session_id}", session_info), web.post("/api/session/{session_id}/prompt", session_prompt_submit),
web.delete("/api/session/{session_id}", session_delete), web.get("/api/session/{session_id}", session_info),
]) web.delete("/api/session/{session_id}", session_delete),
]
)

View File

@@ -461,15 +461,17 @@ class Subtitle(Track):
content_lines.append(lines[i]) content_lines.append(lines[i])
i += 1 i += 1
cues.append({ cues.append(
"start_ms": Subtitle._parse_vtt_time(start_str), {
"end_ms": Subtitle._parse_vtt_time(end_str), "start_ms": Subtitle._parse_vtt_time(start_str),
"start_str": start_str, "end_ms": Subtitle._parse_vtt_time(end_str),
"end_str": end_str, "start_str": start_str,
"line_pos": line_pos, "end_str": end_str,
"content": "\n".join(content_lines), "line_pos": line_pos,
"settings": settings, "content": "\n".join(content_lines),
}) "settings": settings,
}
)
else: else:
i += 1 i += 1
@@ -494,19 +496,23 @@ class Subtitle(Track):
group.sort(key=lambda x: x["line_pos"]) group.sort(key=lambda x: x["line_pos"])
# Use the earliest start time from the group # Use the earliest start time from the group
earliest = min(group, key=lambda x: x["start_ms"]) earliest = min(group, key=lambda x: x["start_ms"])
merged_cues.append({ merged_cues.append(
"start_str": earliest["start_str"], {
"end_str": group[0]["end_str"], "start_str": earliest["start_str"],
"content": "\n".join(c["content"] for c in group), "end_str": group[0]["end_str"],
"settings": "", "content": "\n".join(c["content"] for c in group),
}) "settings": "",
}
)
else: else:
merged_cues.append({ merged_cues.append(
"start_str": current["start_str"], {
"end_str": current["end_str"], "start_str": current["start_str"],
"content": current["content"], "end_str": current["end_str"],
"settings": current["settings"], "content": current["content"],
}) "settings": current["settings"],
}
)
i = j if len(group) > 1 else i + 1 i = j if len(group) > 1 else i + 1
@@ -1145,14 +1151,16 @@ class Subtitle(Track):
if cue_box.type == b"sttg": if cue_box.type == b"sttg":
layout = Layout(webvtt_positioning=cue_box.settings) layout = Layout(webvtt_positioning=cue_box.settings)
elif cue_box.type == b"payl": elif cue_box.type == b"payl":
nodes.extend([ nodes.extend(
node [
for line in cue_box.cue_text.split("\n") node
for node in [ for line in cue_box.cue_text.split("\n")
CaptionNode.create_text(WebVTTReader()._decode(line)), for node in [
CaptionNode.create_break(), CaptionNode.create_text(WebVTTReader()._decode(line)),
CaptionNode.create_break(),
]
] ]
]) )
nodes.pop() nodes.pop()
if nodes: if nodes:

View File

@@ -687,25 +687,29 @@ class Track:
if hasattr(self, "data") and self.data.get("audio_language"): if hasattr(self, "data") and self.data.get("audio_language"):
audio_lang = self.data["audio_language"] audio_lang = self.data["audio_language"]
audio_name = self.data.get("audio_language_name", audio_lang) audio_name = self.data.get("audio_language_name", audio_lang)
args.extend([ args.extend(
"-metadata:s:a:0", [
f"language={audio_lang}", "-metadata:s:a:0",
"-metadata:s:a:0", f"language={audio_lang}",
f"title={audio_name}", "-metadata:s:a:0",
"-metadata:s:a:0", f"title={audio_name}",
f"handler_name={audio_name}", "-metadata:s:a:0",
]) f"handler_name={audio_name}",
]
)
args.extend([ args.extend(
# Following are very important! [
"-map_metadata", # Following are very important!
"-1", # don't transfer metadata to output file "-map_metadata",
"-fflags", "-1", # don't transfer metadata to output file
"bitexact", # only have minimal tag data, reproducible mux "-fflags",
"-codec", "bitexact", # only have minimal tag data, reproducible mux
"copy", "-codec",
str(output_path), "copy",
]) str(output_path),
]
)
subprocess.run( subprocess.run(
args, args,