Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ What changed in each version of WhatsOn, newest first.

## Unreleased

### Added
- **WhatsOn works under a path prefix.** The page now asks for its files and the API with relative addresses, so it
can sit behind a proxy that serves it from a sub-path, such as a Home Assistant ingress panel at
`/api/ingress/whatson/`. Opening it directly at its own address works as before.

## 0.2.8 - 2026-11-10

### Added
Expand Down
3 changes: 3 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,6 @@ A key from the environment only goes to the provider's own address, never to `AI

- WhatsOn listens on port 8430. The image sets this, not an environment variable.
- `compose.yaml` uses host networking so the container can reach TVs on ports 6466 and 6467.
- WhatsOn can be served under a path prefix, for example as a Home Assistant ingress panel at `/api/ingress/whatson/`.
The page uses relative addresses for its files and the API, so nothing needs setting for it. Open it with a trailing
slash. The proxy must keep WhatsOn's own address in the `Host` header, or that name must be in `ALLOWED_HOSTS`.
1 change: 1 addition & 0 deletions docs/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ scripts when Node.js is installed. CI also runs gitleaks over the whole history,
| `tests/test_trakt.py` | Trakt token refresh and its redirect URI |
| `tests/test_settings_layout.py` | Settings tabs and links into them |
| `tests/test_progress_ui.py` | Wiring for search and AI progress |
| `tests/test_subpath.py` | The page uses relative addresses, so it works under a path prefix |
| `tests/test_release.py` | Release notes and version checks |
| `tests/test_unraid.py` | The Unraid template matches how the app runs |
| `tests/test_version.py` | The status route reports the version |
Expand Down
2 changes: 1 addition & 1 deletion tests/test_sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def test_guide_channels_and_listings():
channels, airings = run(jf.load_guide(0, 1))
assert channels[0].vcn == "12" and channels[1].vcn == "13" and channels[1].thumb == ""
# Channel art is used as an address directly, so the browser must only ever be pointed at WhatsOn.
assert channels[0].thumb.startswith("/api/thumb?path=%2FItems%2F") and "jf.example.com" not in channels[0].thumb
assert channels[0].thumb.startswith("api/thumb?path=%2FItems%2F") and "jf.example.com" not in channels[0].thumb
assert [(a.kind, a.subtitle) for a in airings] == [("episode", "S01E22 The Pool"), ("movie", ""), ("sports", "")]
assert airings[0].ends - airings[0].begins == 1800 and airings[1].year == 1995

Expand Down
25 changes: 25 additions & 0 deletions tests/test_subpath.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""WhatsOn can be served under a path prefix, such as a Home Assistant ingress panel at /api/ingress/whatson/.
That only works while the page asks for its files and the API with relative URLs."""
from __future__ import annotations

import re
from pathlib import Path

STATIC = Path(__file__).resolve().parent.parent / "whatson" / "static"
JS = STATIC / "js"


def test_the_page_loads_its_files_relatively():
html = (STATIC / "index.html").read_text(encoding="utf-8")
assert not re.search(r'(?:src|href)="/(?!/)', html)


def test_direct_fetches_use_relative_urls():
for f in JS.glob("*.js"):
assert not re.search(r"fetch\(\s*[\"'`]/(?!/)", f.read_text(encoding="utf-8")), f.name


def test_api_calls_and_artwork_resolve_under_a_prefix():
core = (JS / "core.js").read_text(encoding="utf-8")
assert "fetch(rel(path)" in core
assert "`api/thumb?path=" in core
2 changes: 1 addition & 1 deletion whatson/sources/jellyfin.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ async def load_guide(self, start: int, end: int) -> tuple[list[Channel], list[Ai
art = _poster(c)
channels.append(Channel(id=c["Id"], vcn=str(c.get("ChannelNumber") or c.get("Number") or c["Id"]),
title=c.get("Name", ""),
thumb=f"/api/thumb?path={quote(art, safe='')}&width=120" if art else ""))
thumb=f"api/thumb?path={quote(art, safe='')}&width=120" if art else ""))
programs = await self._pages("/LiveTv/Programs", MinEndDate=_iso(start), MaxStartDate=_iso(end),
Fields="Overview,Genres", EnableImages="false", EnableUserData="false",
**self._user())
Expand Down
4 changes: 2 additions & 2 deletions whatson/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<meta name="theme-color" content="#0f1115" media="(prefers-color-scheme: dark)">
<link rel="icon" href="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 32 32'%3E%3Crect width='32' height='32' rx='7' fill='%2314161a'/%3E%3Cpath d='M11.5 5.5 16 10l4.5-4.5' fill='none' stroke='%23ff6a3d' stroke-width='2.4' stroke-linecap='round' stroke-linejoin='round'/%3E%3Crect x='5' y='10' width='22' height='17' rx='3.5' fill='%23ff6a3d'/%3E%3Crect x='8.5' y='13.5' width='15' height='10' rx='1.5' fill='%2314161a'/%3E%3C/svg%3E">
<script>try{var t=localStorage.getItem("theme");if(t==="light"||t==="dark")document.documentElement.dataset.theme=t}catch(e){}</script>
<link rel="stylesheet" href="/static/style.css">
<link rel="stylesheet" href="static/style.css">
</head>
<body>
<header class="top">
Expand Down Expand Up @@ -271,6 +271,6 @@ <h3>Your teams</h3>
</dialog>

<div id="toast" role="status" aria-live="polite"></div>
<script type="module" src="/static/js/main.js"></script>
<script type="module" src="static/js/main.js"></script>
</body>
</html>
7 changes: 5 additions & 2 deletions whatson/static/js/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ export function applyTheme(theme) {
}
}

/** Turns "/api/x" into "api/x", so the page works under a path prefix such as a Home Assistant ingress panel. */
export const rel = (url) => (url.startsWith("/") && !url.startsWith("//") ? url.slice(1) : url);

export async function api(path, opts = {}) {
const res = await fetch(path, {
const res = await fetch(rel(path), {
...opts,
headers: { "X-WhatsOn": "1", ...(opts.body ? { "Content-Type": "application/json" } : {}) },
body: opts.body ? JSON.stringify(opts.body) : undefined,
Expand Down Expand Up @@ -92,7 +95,7 @@ export const fmtMins = (ms) => {
return m >= 60 ? `${Math.floor(m / 60)} h ${String(m % 60).padStart(2, "0")}` : `${m} min`;
};
export const thumbUrl = (path, width = 300) =>
path ? `/api/thumb?path=${encodeURIComponent(path)}&width=${width}` : "";
path ? `api/thumb?path=${encodeURIComponent(path)}&width=${width}` : "";

export const isPhone = () => window.matchMedia("(max-width: 700px)").matches;

Expand Down
4 changes: 2 additions & 2 deletions whatson/static/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ document.addEventListener("click", (ev) => {

$("#open-settings").addEventListener("click", () => openSettings("tvs"));
$("#settings").addEventListener("close", async () => {
const st = await fetch("/api/status").then((r) => r.json()).catch(() => ({}));
const st = await fetch("api/status").then((r) => r.json()).catch(() => ({}));
const wasOff = !$("#connect-source").hidden;
$("#connect-source").hidden = st.connected !== false;
ai.on = Boolean(st.ai);
Expand Down Expand Up @@ -111,7 +111,7 @@ initSettings();
await loadDevices().catch(() => {});
refreshNow();
// Before the first screen, so it knows whether to show that AI is working.
const st = await fetch("/api/status").then((r) => r.json()).catch(() => ({}));
const st = await fetch("api/status").then((r) => r.json()).catch(() => ({}));
ai.on = Boolean(st.ai);
route();
// First run: nothing works until a server is connected.
Expand Down