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,7 +1327,8 @@ 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/health", health),
web.get("/api/services", services), web.get("/api/services", services),
web.post("/api/search", search), web.post("/api/search", search),
@@ -1347,4 +1348,5 @@ def setup_swagger(app: web.Application) -> None:
web.post("/api/session/{session_id}/prompt", session_prompt_submit), web.post("/api/session/{session_id}/prompt", session_prompt_submit),
web.get("/api/session/{session_id}", session_info), web.get("/api/session/{session_id}", session_info),
web.delete("/api/session/{session_id}", session_delete), web.delete("/api/session/{session_id}", session_delete),
]) ]
)

View File

@@ -461,7 +461,8 @@ 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), "start_ms": Subtitle._parse_vtt_time(start_str),
"end_ms": Subtitle._parse_vtt_time(end_str), "end_ms": Subtitle._parse_vtt_time(end_str),
"start_str": start_str, "start_str": start_str,
@@ -469,7 +470,8 @@ class Subtitle(Track):
"line_pos": line_pos, "line_pos": line_pos,
"content": "\n".join(content_lines), "content": "\n".join(content_lines),
"settings": settings, "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"], "start_str": earliest["start_str"],
"end_str": group[0]["end_str"], "end_str": group[0]["end_str"],
"content": "\n".join(c["content"] for c in group), "content": "\n".join(c["content"] for c in group),
"settings": "", "settings": "",
}) }
)
else: else:
merged_cues.append({ merged_cues.append(
{
"start_str": current["start_str"], "start_str": current["start_str"],
"end_str": current["end_str"], "end_str": current["end_str"],
"content": current["content"], "content": current["content"],
"settings": current["settings"], "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 node
for line in cue_box.cue_text.split("\n") for line in cue_box.cue_text.split("\n")
for node in [ for node in [
CaptionNode.create_text(WebVTTReader()._decode(line)), CaptionNode.create_text(WebVTTReader()._decode(line)),
CaptionNode.create_break(), CaptionNode.create_break(),
] ]
]) ]
)
nodes.pop() nodes.pop()
if nodes: if nodes:

View File

@@ -687,16 +687,19 @@ 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", "-metadata:s:a:0",
f"language={audio_lang}", f"language={audio_lang}",
"-metadata:s:a:0", "-metadata:s:a:0",
f"title={audio_name}", f"title={audio_name}",
"-metadata:s:a:0", "-metadata:s:a:0",
f"handler_name={audio_name}", f"handler_name={audio_name}",
]) ]
)
args.extend([ args.extend(
[
# Following are very important! # Following are very important!
"-map_metadata", "-map_metadata",
"-1", # don't transfer metadata to output file "-1", # don't transfer metadata to output file
@@ -705,7 +708,8 @@ class Track:
"-codec", "-codec",
"copy", "copy",
str(output_path), str(output_path),
]) ]
)
subprocess.run( subprocess.run(
args, args,