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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
16 changes: 16 additions & 0 deletions app/db/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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")

Expand Down
45 changes: 45 additions & 0 deletions app/services/sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@
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
from app.services import channels, exchange, importer
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
Expand Down Expand Up @@ -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(
Expand All @@ -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)
)
Expand Down Expand Up @@ -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:
Expand Down
19 changes: 19 additions & 0 deletions app/web/explore_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion app/web/static/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
19 changes: 19 additions & 0 deletions app/web/templates/explore/_entry_status.html
Original file line number Diff line number Diff line change
@@ -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 %}
<td class="status-cell">
{% if check_engine_down %}
<span class="status status-error" role="alert">Ace Stream no responde</span>
{% elif entry.check_status %}
<span class="status status-{{ entry.check_status.value }}">{{ status_labels[entry.check_status] }}</span>
{%- if entry.check_peers is not none %} <span class="hint">{{ entry.check_peers }} {{ "peer" if entry.check_peers == 1 else "peers" }}</span>{% endif %}
<time class="hint" datetime="{{ entry.checked_at.isoformat() }}" title="{{ entry.checked_at | localtime }}">{{ entry.checked_at | ago }}</time>
{% else %}
<span class="status status-unchecked">Sin verificar</span>
{% endif %}
{# Not a form: the rows are inside the add form. The cell replaces itself. #}
<button type="button" class="icon-button recheck-button" hx-post="{{ url_for('explore_check', entry_id=entry.id) }}"
hx-params="none" hx-target="closest td" hx-swap="outerHTML" hx-indicator="closest td" hx-disabled-elt="this"
aria-label="Verificar {{ entry.title or entry.content_id }}" title="Verificar sin agregarlo">{{ icon("recheck") }}</button>
<span class="busy" aria-live="polite">Verificando…</span>
</td>
2 changes: 2 additions & 0 deletions app/web/templates/explore/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ <h2 id="summary-title">Resultado</h2>
<th scope="col">Grupo</th>
<th scope="col">Hash</th>
<th scope="col">Fuente</th>
<th scope="col">Estado</th>
<th scope="col"><span class="visually-hidden">Acciones</span></th>
</tr>
</thead>
Expand All @@ -126,6 +127,7 @@ <h2 id="summary-title">Resultado</h2>
<td>{{ entry.group or "—" }}</td>
<td class="hash"><code title="{{ entry.content_id }}">{{ entry.content_id | short_hash }}</code>{{ copy_button(entry.content_id, "Copiar el hash de " ~ (entry.title or entry.content_id)) }}</td>
<td>{{ row.source_name }}</td>
{% include "explore/_entry_status.html" %}
<td class="row-actions">
{# Not a form: these rows are inside the add form. No parameters are sent. #}
<button type="button" class="icon-button" hx-post="{{ url_for('explore_play', entry_id=entry.id) }}"
Expand Down
35 changes: 35 additions & 0 deletions migrations/versions/0008_source_entry_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""checks of source entries run from Explorar

Revision ID: 0008
Revises: 0007
Create Date: 2026-10-11 12:00:00
"""

import sqlalchemy as sa
from alembic import op

revision = "0008"
down_revision = "0007"
branch_labels = None
depends_on = None

_STATUSES = "'alive', 'no_peers', 'not_found', 'error'"


def upgrade() -> None:
# Batch mode: SQLite cannot add a CHECK constraint to an existing table.
with op.batch_alter_table("source_entry") as batch:
batch.add_column(sa.Column("check_status", sa.String(length=16), nullable=True))
batch.add_column(sa.Column("check_peers", sa.Integer(), nullable=True))
batch.add_column(sa.Column("checked_at", sa.DateTime(), nullable=True))
batch.create_check_constraint(
op.f("ck_source_entry_check_status_valid"), f"check_status IN ({_STATUSES})"
)


def downgrade() -> None:
with op.batch_alter_table("source_entry") as batch:
batch.drop_constraint(op.f("ck_source_entry_check_status_valid"), type_="check")
batch.drop_column("checked_at")
batch.drop_column("check_peers")
batch.drop_column("check_status")
73 changes: 73 additions & 0 deletions tests/test_sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,3 +550,76 @@ def test_deleting_a_source_forgets_what_was_dismissed(db, explored):
sources.delete_source(db, explored["lista"].id)

assert sources.dismissed_counts(db) == {}


@pytest.fixture
def verified(monkeypatch):
"""Replaces the engine check: records each call and answers `outcome`."""
from app.db.models import CheckStatus
from app.services.verification import VerificationResult

class Verified:
calls = []
outcome = VerificationResult(CheckStatus.ALIVE, peers=3)

def __call__(self, client, content_id, settings=None, *, kind=None):
self.calls.append((content_id, kind))
if isinstance(self.outcome, Exception):
raise self.outcome
return self.outcome

fake = Verified()
monkeypatch.setattr(sources, "verify", fake)
return fake


def test_check_entry_keeps_the_outcome_without_adding_the_channel(db, explored, verified):
from app.db.models import Channel, CheckStatus

entry_id = entry_ids(db, "Uno HD")[0]

entry = sources.check_entry(db, None, entry_id)

assert (entry.check_status, entry.check_peers) == (CheckStatus.ALIVE, 3)
assert entry.checked_at is not None
assert verified.calls == [(HASH_A, None)]
assert [c.title for c in db.query(Channel)] == ["Mío"]


def test_check_entry_asks_the_way_the_catalog_knows(db, explored, verified):
explored["mine"].id_kind = "infohash"
db.commit()

sources.check_entry(db, None, entry_ids(db, "Dos")[0])

assert verified.calls == [(HASH_B, "infohash")]


def test_check_entry_with_the_engine_down_stores_nothing(db, explored, verified):
from app.acestream.errors import EngineUnavailableError

verified.outcome = EngineUnavailableError("down")
entry_id = entry_ids(db, "Uno HD")[0]

with pytest.raises(EngineUnavailableError):
sources.check_entry(db, None, entry_id)

assert db.get(SourceEntry, entry_id).checked_at is None
with pytest.raises(NotFoundError):
sources.check_entry(db, None, 999)


def test_checks_outlive_a_refresh(db, client, explored, verified):
from app.db.models import CheckStatus

sources.check_entry(db, None, entry_ids(db, "Uno HD")[0])

sources.refresh_sources(db, client)

entries = {e.title: e for e in db.query(SourceEntry)}
assert (entries["Uno HD"].check_status, entries["Uno HD"].check_peers) == (
CheckStatus.ALIVE,
3,
)
assert entries["Dos"].checked_at is None
assert entries["Uno otra vez"].checked_at is None # same channel, other source
49 changes: 49 additions & 0 deletions tests/test_web_explore.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from app.db.models import Channel, Source, SourceEntry
from app.services import categories, channels
from app.services import sources as sources_service

HASH_A = "78266c15035d0ad8cbc58f821733931e1de434ab"
HASH_B = "ecc85e5d2b6088d2d307fd0b5de4f09f089e762f"
Expand Down Expand Up @@ -363,3 +364,51 @@ def test_explore_rows_have_a_play_button(web, db, listed):

assert f'hx-post="http://127.0.0.1/explore/entries/{entry_id(db, "Uno HD")}/play"' in page
assert 'hx-params="none"' in page


@pytest.fixture
def checked_entries(monkeypatch):
"""Replaces the engine check of Explorar with a fixed answer (or an exception)."""
from app.db.models import CheckStatus
from app.services.verification import VerificationResult

state = {"outcome": VerificationResult(CheckStatus.NO_PEERS, peers=0)}

def fake(client, content_id, settings=None, *, kind=None):
if isinstance(state["outcome"], Exception):
raise state["outcome"]
return state["outcome"]

monkeypatch.setattr(sources_service, "verify", fake)
return state


def test_checking_a_channel_of_a_source_answers_with_its_status_cell(
web, db, listed, checked_entries
):
response = web.post(f"/explore/entries/{entry_id(db, 'Uno HD')}/check")

assert response.status_code == 200
cell = response.text
assert '<td class="status-cell">' in cell
assert "Sin peers" in cell and "0 peers" in cell and "<html" not in cell
assert "Sin peers" in web.get("/explore").text
assert db.query(Channel).count() == 1


def test_checking_with_the_engine_down_says_so(web, db, listed, checked_entries):
from app.acestream.errors import EngineUnavailableError

checked_entries["outcome"] = EngineUnavailableError("down")

cell = web.post(f"/explore/entries/{entry_id(db, 'Uno HD')}/check").text

assert "Ace Stream no responde" in cell
assert web.post("/explore/entries/999/check").status_code == 404


def test_explore_rows_have_a_status_and_a_check_button(web, db, listed):
page = web.get("/explore").text

assert page.count("Sin verificar") == 2
assert f'hx-post="http://127.0.0.1/explore/entries/{entry_id(db, "Dos")}/check"' in page
Loading