96 lines
2.4 KiB
Python
96 lines
2.4 KiB
Python
"""Simple web app to display translated episodes with speaker colors."""
|
|
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
|
|
from flask import Flask, render_template, abort
|
|
|
|
app = Flask(__name__)
|
|
|
|
# Configuration
|
|
TRANSLATED_DIR = Path(__file__).parent.parent / "_translated"
|
|
COLORS_FILE = Path(__file__).parent.parent / "_colors.json"
|
|
|
|
|
|
def load_colors():
|
|
"""Load speaker color mapping."""
|
|
if COLORS_FILE.exists():
|
|
with open(COLORS_FILE, "r", encoding="utf-8") as f:
|
|
return json.load(f)
|
|
return {}
|
|
|
|
|
|
def get_episodes():
|
|
"""Get list of all episodes from _translated folder."""
|
|
episodes = []
|
|
if TRANSLATED_DIR.exists():
|
|
for json_file in sorted(TRANSLATED_DIR.glob("*_translated.json")):
|
|
# Extract episode name from filename (e.g., "S02E01_translated.json" -> "S02E01")
|
|
episode_id = json_file.stem.replace("_translated", "")
|
|
episodes.append({
|
|
"id": episode_id,
|
|
"filename": json_file.name,
|
|
"title": episode_id
|
|
})
|
|
return episodes
|
|
|
|
|
|
def load_episode(episode_id):
|
|
"""Load a specific episode's data."""
|
|
json_file = TRANSLATED_DIR / f"{episode_id}_translated.json"
|
|
if not json_file.exists():
|
|
return None
|
|
|
|
with open(json_file, "r", encoding="utf-8") as f:
|
|
return json.load(f)
|
|
|
|
|
|
@app.route("/")
|
|
def index():
|
|
"""Redirect to first episode or show episode list."""
|
|
episodes = get_episodes()
|
|
if not episodes:
|
|
return "No episodes found", 404
|
|
|
|
# Get first episode data
|
|
first_episode = episodes[0]
|
|
lines = load_episode(first_episode["id"])
|
|
colors = load_colors()
|
|
|
|
return render_template(
|
|
"episode.html",
|
|
episodes=episodes,
|
|
current_episode=first_episode,
|
|
lines=lines,
|
|
colors=colors
|
|
)
|
|
|
|
|
|
@app.route("/episode/<episode_id>")
|
|
def episode(episode_id):
|
|
"""Display a specific episode."""
|
|
lines = load_episode(episode_id)
|
|
if lines is None:
|
|
abort(404)
|
|
|
|
episodes = get_episodes()
|
|
current_episode = {
|
|
"id": episode_id,
|
|
"filename": f"{episode_id}_translated.json",
|
|
"title": episode_id
|
|
}
|
|
colors = load_colors()
|
|
|
|
return render_template(
|
|
"episode.html",
|
|
episodes=episodes,
|
|
current_episode=current_episode,
|
|
lines=lines,
|
|
colors=colors
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
app.run(debug=True, host="0.0.0.0", port=5000)
|