From e27aa3cf22caa5002cb6c3bec76c597a5941743c Mon Sep 17 00:00:00 2001 From: Fabio Date: Sat, 10 Oct 2026 13:57:11 -0300 Subject: [PATCH] Play a channel from Explorar without adding it (#105) Each Explorar row gets a play button (hx-post with no parameters, as the rows sit inside the add form). The playing code of the channel detail moves to routes.play(), shared by both; a source entry uses the kind of the catalog channel with the same Content ID, if there is one. Co-Authored-By: Claude --- CHANGELOG.md | 1 + app/web/explore_routes.py | 31 +++++++++- app/web/routes.py | 42 ++++++++++---- app/web/templates/channels/_play_result.html | 2 +- app/web/templates/explore/index.html | 9 ++- tests/test_web_explore.py | 60 ++++++++++++++++++++ 6 files changed, 131 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 35e16bb..34618e3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ Todos los cambios relevantes del proyecto se registran aquí. Formato basado en ## [Sin publicar] ### Agregado +- Playing a channel of a source without adding it (#105): each Explorar row has a play button that opens it in VLC, the same way and with the same messages as the catalog (Content ID or infohash, #100). - Removing channels from a source (#104): "Quitar de la lista" in Explorar hides the chosen channels of their sources, and they stay hidden when the source is refreshed or its file uploaded again (kept by Content ID in the new `source_dismissed` table, migration 0007). Fuentes shows how many were removed from each source and "Restaurar quitados" brings them back. The catalog is not touched. - Pages in Explorar (#103): 100 channels per page with first, previous, next and last links and "página X de Y", instead of only the first 200. The page goes in the address (`page=`) with the search, and adding channels comes back to the same page; changing a filter starts again at the first one. - Choosing several channels in the list (#102): a check box per row and one to choose them all, and a bar with how many are chosen and three actions for them: Verificar, Regenerar capturas and Borrar (the last two ask first). Checks run in the background and every action comes back to the list with its filters. diff --git a/app/web/explore_routes.py b/app/web/explore_routes.py index 5f60e8e..d1970ee 100644 --- a/app/web/explore_routes.py +++ b/app/web/explore_routes.py @@ -5,14 +5,15 @@ from fastapi.responses import HTMLResponse, RedirectResponse from sqlalchemy.orm import Session -from app.db.models import Category -from app.services import categories, sources +from app.db.models import Category, SourceEntry +from app.services import categories, channels, sources from app.services.catalog import Screenshots from app.services.errors import DuplicateError, NotFoundError, ServiceError, ValidationError from app.services.sources import ExploreQuery, SourceFetchError from app.web.check_routes import start_checks from app.web.deps import ( CheckRunnerDep, + EngineDep, EngineFactoryDep, EngineProbeDep, SessionDep, @@ -20,6 +21,7 @@ SettingsDep, ) from app.web.forms import resolve_category +from app.web.routes import play from app.web.templating import templates router = APIRouter(prefix="/explore") @@ -147,6 +149,31 @@ def explore_add( return _render_explore(request, session, query, result=result) +@router.post("/entries/{entry_id}/play", response_class=HTMLResponse, name="explore_play") +def explore_play( + request: Request, session: SessionDep, client: EngineDep, settings: SettingsDep, entry_id: int +): + """Opens a channel of a source in VLC without adding it to the catalog, to try it.""" + entry = _entry_or_404(session, entry_id) + known = channels.find_channel_by_content_id(session, entry.content_id) + response, _ = play( + request, + client, + settings, + entry.content_id, + title=entry.title, + kind=known.id_kind if known else None, + ) + return response + + +def _entry_or_404(session: Session, entry_id: int) -> SourceEntry: + entry = session.get(SourceEntry, entry_id) + if entry is None: + raise HTTPException(status_code=404, detail="Canal no encontrado en las fuentes") + return entry + + @router.post("/remove", response_class=HTMLResponse, name="explore_remove") def explore_remove( request: Request, diff --git a/app/web/routes.py b/app/web/routes.py index 7042315..6eee6b4 100644 --- a/app/web/routes.py +++ b/app/web/routes.py @@ -7,6 +7,8 @@ from fastapi.responses import HTMLResponse, RedirectResponse, Response from sqlalchemy.orm import Session +from app.acestream.client import EngineClient, StreamSession +from app.acestream.content_id import IdKind from app.acestream.errors import ContentNotFoundError, EngineError from app.db.base import utcnow from app.db.models import Channel, Check @@ -16,6 +18,7 @@ from app.services.iso import country_name, language_name from app.services.listing import DEFAULT_DESCENDING, ChannelQuery, SortKey from app.services.screenshots import SCREENSHOTS_SUBDIR +from app.services.settings import AppSettings from app.web.check_routes import start_checks from app.web.deps import ( CheckRunnerDep, @@ -357,13 +360,34 @@ def channel_play( request: Request, session: SessionDep, client: EngineDep, settings: SettingsDep, channel_id: int ): channel = _get_channel_or_404(session, channel_id) + response, stream = play( + request, + client, + settings, + channel.content_id, + title=channel.title, + kind=channel.id_kind, + ) + if stream is not None and channel.id_kind != stream.kind: + channel.id_kind = stream.kind # next time, straight the right way + session.commit() + return response + + +def play( + request: Request, + client: EngineClient, + settings: AppSettings, + content_id: str, + *, + title: str | None, + kind: IdKind | None = None, +) -> tuple[HTMLResponse, StreamSession | None]: + """Opens `content_id` in VLC; the notice for #notices and the session, if it started.""" + stream = None try: stream = player.open_in_player( - client, - channel.content_id, - kind=channel.id_kind, - title=channel.title, - vlc_path=settings.vlc_path, + client, content_id, kind=kind, title=title, vlc_path=settings.vlc_path ) except player.VlcNotFoundError: error = "No se encontró VLC. Instálalo o indica la ruta de vlc.exe en Ajustes." @@ -378,16 +402,14 @@ def channel_play( error = f"No se pudo abrir VLC: {exc}" else: error = None - if channel.id_kind != stream.kind: - channel.id_kind = stream.kind # next time, straight the right way - session.commit() - return templates.TemplateResponse( + response = templates.TemplateResponse( request, "channels/_play_result.html", - {"channel": channel, "error": error}, + {"title": title or content_id, "error": error}, # 503: the player or the engine is not available; htmx shows it (see base.html). status_code=503 if error else 200, ) + return response, stream @router.get("/channels/{channel_id}/edit", response_class=HTMLResponse, name="channel_edit") diff --git a/app/web/templates/channels/_play_result.html b/app/web/templates/channels/_play_result.html index 1eefe74..8bd988b 100644 --- a/app/web/templates/channels/_play_result.html +++ b/app/web/templates/channels/_play_result.html @@ -1,5 +1,5 @@ {% if error %} {% else %} -

Abriendo «{{ channel.title }}» en VLC…

+

Abriendo «{{ title }}» en VLC…

{% endif %} diff --git a/app/web/templates/explore/index.html b/app/web/templates/explore/index.html index c138a8a..ee2b129 100644 --- a/app/web/templates/explore/index.html +++ b/app/web/templates/explore/index.html @@ -1,5 +1,5 @@ {% extends "base.html" %} -{% from "_icons.html" import copy_button %} +{% from "_icons.html" import copy_button, icon %} {# Reloading would lose the summary of what was added. #} {% set reload_on_checks = false %} @@ -105,6 +105,7 @@

Resultado

Grupo Hash Fuente + Acciones @@ -125,6 +126,12 @@

Resultado

{{ entry.group or "—" }} {{ entry.content_id | short_hash }}{{ copy_button(entry.content_id, "Copiar el hash de " ~ (entry.title or entry.content_id)) }} {{ row.source_name }} + + {# Not a form: these rows are inside the add form. No parameters are sent. #} + + {% endfor %} diff --git a/tests/test_web_explore.py b/tests/test_web_explore.py index bd2dae6..57e58bb 100644 --- a/tests/test_web_explore.py +++ b/tests/test_web_explore.py @@ -303,3 +303,63 @@ def test_restoring_from_the_sources_page(web, db, listed): assert "Restaurar quitados" not in page assert "Uno HD" in web.get("/explore").text assert web.post("/explore/sources/99/restore").status_code == 404 + + +@pytest.fixture +def opened(monkeypatch): + """Replaces the player: records what would be opened, or raises `error`.""" + from types import SimpleNamespace + + from app.acestream.content_id import IdKind + from app.web import routes + + class Opened: + calls = [] + error = None + + def __call__(self, client, content_id, *, kind=None, title=None, vlc_path=None): + self.calls.append((content_id, kind, title)) + if self.error: + raise self.error + return SimpleNamespace(kind=IdKind.CONTENT_ID) + + fake = Opened() + monkeypatch.setattr(routes.player, "open_in_player", fake) + return fake + + +def test_playing_a_channel_of_a_source_without_adding_it(web, db, listed, opened): + response = web.post(f"/explore/entries/{entry_id(db, 'Uno HD')}/play") + + assert response.status_code == 200 + assert "Abriendo «Uno HD» en VLC…" in response.text and "