diff --git a/CHANGELOG.md b/CHANGELOG.md index 34618e3..fa2f6fc 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 +- Checking a channel of a source without adding it (#106): Explorar has a Estado column with a check button per row that runs the same check as the catalog and shows the outcome (status, peers, when) in the row. It is kept on the source entry, also across refreshes (migration 0008). With Ace Stream off it says so and stores nothing. - 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. diff --git a/app/db/models.py b/app/db/models.py index eb362f0..e39a079 100644 --- a/app/db/models.py +++ b/app/db/models.py @@ -184,6 +184,10 @@ class SourceEntry(Base): __table_args__ = ( UniqueConstraint("source_id", "content_id"), Index("ix_source_entry_content_id", "content_id"), + CheckConstraint( + "check_status IN (" + ", ".join(f"'{s.value}'" for s in CheckStatus) + ")", + name="check_status_valid", + ), ) id: Mapped[int] = mapped_column(primary_key=True) @@ -192,6 +196,18 @@ class SourceEntry(Base): title: Mapped[str | None] = mapped_column(String(200)) # None: the list gave no name group: Mapped[str | None] = mapped_column(String(100)) # the list's own grouping, if any position: Mapped[int] # order in the list + # Last check run from Explorar, without adding the channel; kept across refreshes. + check_status: Mapped[CheckStatus | None] = mapped_column( + Enum( + CheckStatus, + native_enum=False, + create_constraint=False, + length=16, + values_callable=lambda e: [m.value for m in e], + ) + ) + check_peers: Mapped[int | None] + checked_at: Mapped[datetime | None] = mapped_column(UTCDateTime) source: Mapped[Source] = relationship(back_populates="entries") diff --git a/app/services/sources.py b/app/services/sources.py index ee60150..fee0307 100644 --- a/app/services/sources.py +++ b/app/services/sources.py @@ -19,6 +19,7 @@ from sqlalchemy.orm import Session from app import __version__ +from app.acestream.client import EngineClient from app.acestream.content_id import InvalidContentIdError, parse_content_id from app.db.base import utcnow from app.db.models import Channel, Source, SourceDismissed, SourceEntry @@ -26,6 +27,7 @@ from app.services.errors import DuplicateError, NotFoundError, ValidationError from app.services.importer import ImportResult, Reason, Rejected from app.services.naming import clean_stream_name, placeholder_title +from app.services.verification import VerificationSettings, verify MAX_SOURCE_BYTES = 5 * 1024 * 1024 MAX_ENTRIES = 5000 # per source @@ -310,6 +312,18 @@ def refresh_source(session: Session, source: Source, client: httpx.Client) -> So def _store_entries(session: Session, source: Source, entries: list[ParsedEntry]) -> None: + # Checks run from Explorar outlive the refresh, matched by Content ID. + checked = { + content_id: (status, peers, at) + for content_id, status, peers, at in session.execute( + select( + SourceEntry.content_id, + SourceEntry.check_status, + SourceEntry.check_peers, + SourceEntry.checked_at, + ).where(SourceEntry.source_id == source.id, SourceEntry.checked_at.is_not(None)) + ) + } session.execute(delete(SourceEntry).where(SourceEntry.source_id == source.id)) session.add_all( SourceEntry( @@ -318,6 +332,13 @@ def _store_entries(session: Session, source: Source, entries: list[ParsedEntry]) title=entry.title, group=entry.group, position=position, + **dict( + zip( + ("check_status", "check_peers", "checked_at"), + checked.get(entry.content_id, (None, None, None)), + strict=True, + ) + ), ) for position, entry in enumerate(entries, start=1) ) @@ -421,6 +442,30 @@ def restore_dismissed(session: Session, source_id: int) -> None: session.commit() +def check_entry( + session: Session, + client: EngineClient, + entry_id: int, + settings: VerificationSettings | None = None, +) -> SourceEntry: + """Verifies a channel of a source, as the catalog would, and keeps the outcome on it. + + The channel is not added to the catalog. An engine that cannot be reached raises + `EngineUnavailableError` and nothing is stored, as with catalog checks. + Blocks while the engine is polled, so call it from a worker thread. + """ + entry = session.get(SourceEntry, entry_id) + if entry is None: + raise NotFoundError(f"source entry not found: {entry_id}") + known = channels.find_channel_by_content_id(session, entry.content_id) + result = verify(client, entry.content_id, settings, kind=known.id_kind if known else None) + entry.check_status = result.status + entry.check_peers = result.peers + entry.checked_at = utcnow() + session.commit() + return entry + + def add_entries( session: Session, entry_ids: Iterable[int], *, category_id: int | None = None ) -> ImportResult: diff --git a/app/web/explore_routes.py b/app/web/explore_routes.py index d1970ee..79335d1 100644 --- a/app/web/explore_routes.py +++ b/app/web/explore_routes.py @@ -5,6 +5,7 @@ from fastapi.responses import HTMLResponse, RedirectResponse from sqlalchemy.orm import Session +from app.acestream.errors import EngineUnavailableError from app.db.models import Category, SourceEntry from app.services import categories, channels, sources from app.services.catalog import Screenshots @@ -167,6 +168,24 @@ def explore_play( return response +@router.post("/entries/{entry_id}/check", response_class=HTMLResponse, name="explore_check") +def explore_check( + request: Request, session: SessionDep, client: EngineDep, settings: SettingsDep, entry_id: int +): + """Checks a channel of a source without adding it; answers with its status cell.""" + entry = _entry_or_404(session, entry_id) + engine_down = False + try: + sources.check_entry(session, client, entry.id, settings.verification()) + except EngineUnavailableError: + engine_down = True # says nothing about the channel: nothing is stored + return templates.TemplateResponse( + request, + "explore/_entry_status.html", + {"entry": entry, "check_engine_down": engine_down}, + ) + + def _entry_or_404(session: Session, entry_id: int) -> SourceEntry: entry = session.get(SourceEntry, entry_id) if entry is None: diff --git a/app/web/static/style.css b/app/web/static/style.css index 13c39d5..0c2aace 100644 --- a/app/web/static/style.css +++ b/app/web/static/style.css @@ -271,7 +271,7 @@ abbr { text-decoration: none; cursor: help; } .actions { display: flex; align-items: center; gap: 12px; margin-top: 8px; } .busy { display: none; color: var(--muted); } -form.htmx-request .busy { display: inline; } +form.htmx-request .busy, td.htmx-request .busy { display: inline; } .filters { display: flex; diff --git a/app/web/templates/explore/_entry_status.html b/app/web/templates/explore/_entry_status.html new file mode 100644 index 0000000..6435039 --- /dev/null +++ b/app/web/templates/explore/_entry_status.html @@ -0,0 +1,19 @@ +{# Status cell of an Explorar row: its last check from here, and a button to run one. + Expects `entry` and, after a check, `check_engine_down`. #} +{% from "_icons.html" import icon %} +
{{ entry.content_id | short_hash }}{{ copy_button(entry.content_id, "Copiar el hash de " ~ (entry.title or entry.content_id)) }}