Add live Dunnellon weather and clean up Roku guide titles

This commit is contained in:
Josh
2026-11-13 12:40:36 -05:00
parent 364d911fe4
commit 737c0f0c0c
12 changed files with 322 additions and 16 deletions

View File

@@ -14,6 +14,7 @@ from fastapi.responses import RedirectResponse, Response, StreamingResponse
from starlette.background import BackgroundTask
from .discovery import SERVICE, start_discovery
from .weather import NWSWeather
WEATHER_ID = "weather"
HOP_HEADERS = {
@@ -49,12 +50,35 @@ def lineup(data):
return sorted(channels, key=lambda channel: channel["number"])
def programs(channel_guide):
EPISODE_CODE = re.compile(r"\bS(\d{1,2})E(\d{1,3})\b", re.IGNORECASE)
RELEASE_SUFFIX = re.compile(
r"\b(?:\d{3,4}p|WEB(?:[ ._-]?(?:DL|Rip))?|BluRay|BRRip|HDTV|[xh][ .]?26[45]|HEVC)\b",
re.IGNORECASE,
)
def program_title(item, channel_name):
program = item.get("program") or {}
title = item.get("title") or program.get("title")
if not title:
return "Off air" if item["type"] == "flex" else "Programming"
if program.get("type") != "episode":
return title
words = re.sub(r"(?<=\w)\.(?=\w)", " ", title)
match = EPISODE_CODE.search(words)
if not match:
return words
season, episode = map(int, match.groups())
label = f"{channel_name} S{season:02d}E{episode:02d}"
detail = RELEASE_SUFFIX.split(words[match.end():], maxsplit=1)[0].strip(" ._-")
return f"{label} · {detail}" if detail else label
def programs(channel_guide, channel_name):
result = []
for item in sorted(channel_guide.get("programs", []), key=lambda p: p["start"]):
title = item.get("title") or (item.get("program") or {}).get("title")
result.append({
"title": title or ("Off air" if item["type"] == "flex" else "Programming"),
"title": program_title(item, channel_name),
"start": utc_string(datetime.fromtimestamp(item["start"] / 1000, UTC)),
"stop": utc_string(datetime.fromtimestamp(item["stop"] / 1000, UTC)),
})
@@ -90,6 +114,7 @@ def create_app(*, tunarr_url=None, transport=None, discover=True, id_file=None):
port = int(os.getenv("RETROTV_PORT", "4242"))
base_url = f"http://{ip}:{port}"
name = os.getenv("RETROTV_NAME", "retroLabs")
weather_provider = NWSWeather()
upstream = tunarr_url or os.getenv("RETROTV_TUNARR_URL", "http://127.0.0.1:14242")
identity = Path(id_file or os.getenv("RETROTV_ID_FILE", "data/server-id"))
@@ -171,7 +196,7 @@ def create_app(*, tunarr_url=None, transport=None, discover=True, id_file=None):
return {
"serverTime": utc_string(now),
"channels": [
{**channel, "programs": programs(schedules.get(channel["id"], {}))}
{**channel, "programs": programs(schedules.get(channel["id"], {}), channel["name"])}
for channel in lineup(channel_data)
],
}
@@ -185,9 +210,10 @@ def create_app(*, tunarr_url=None, transport=None, discover=True, id_file=None):
channel_data, schedules = await asyncio.gather(
upstream_json("/api/channels"), guide_data(now, 24)
)
if not any(channel["id"] == channel_id for channel in lineup(channel_data)):
channel = next((entry for entry in lineup(channel_data) if entry["id"] == channel_id), None)
if channel is None:
raise HTTPException(404, "Channel not found")
entries = programs(schedules.get(channel_id, {}))
entries = programs(schedules.get(channel_id, {}), channel["name"])
return {
"now": next((p for p in entries if p["start"] <= stamp < p["stop"]), None),
"next": next((p for p in entries if p["start"] > stamp), None),
@@ -196,7 +222,7 @@ def create_app(*, tunarr_url=None, transport=None, discover=True, id_file=None):
@app.get("/api/v1/weather")
async def weather():
return {"status": "placeholder"}
return await weather_provider.read()
async def proxy(request, path, *, playlist=False, params=None):
client = app.state.tunarr