goon/app/models/scene.py
jtrzupek 8c7fa36437
Some checks are pending
Backend tests / test (push) Waiting to run
fix(favorites): exclude bulk-backfill scenes from "+N new" (tube fake dates)
Browse scrapers backfilling old catalogs stamp the tube's import/post date as
release_date, so old content (e.g. 83 MissaX classics via perverzija, ~3600/3 days
across eporner/youporn/etc.) fake-ranked as newest and flooded the favorites "+N".
NULLing the dates was a non-starter — the stub filter would hide 251k performer-less
scenes. Instead: a Scene.backfill flag marks bulk catalog imports; they stay visible but
never count as "new".

- scenes.backfill column (+ index, migration 0026); resolve_scene/_process_scene thread it.
- deep_crawl tags scenes from pages beyond the "latest" threshold (>2) as backfill;
  latest pages + TPDB/StashDB delta stay genuine. Cursor reset re-sweeps page 1 so real
  new content is always caught fresh.
- favorites +N (performers + studios) excludes backfill within the top-200 window.
- SceneOut exposes `backfill`; mobile NEW badge + NEW-first re-sort skip it (badge==count).
- Retroactive: tagged 439k existing scenes in bulk (>10 non-canonical / studio / day)
  clusters. Device check: favorite-studios +N 11626 (naive) -> 236.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-01 16:04:01 +02:00

118 lines
4.4 KiB
Python

import enum
import uuid
from datetime import date, datetime
from sqlalchemy import (
Boolean,
Date,
DateTime,
Enum,
Float,
ForeignKey,
Integer,
String,
Text,
UniqueConstraint,
func,
)
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.orm import Mapped, mapped_column
from app.models.base import Base, TimestampMixin, UUIDPKMixin
class FingerprintKind(str, enum.Enum):
phash = "phash"
oshash = "oshash"
md5 = "md5"
class Scene(UUIDPKMixin, TimestampMixin, Base):
__tablename__ = "scenes"
title: Mapped[str] = mapped_column(String, nullable=False)
title_normalized: Mapped[str] = mapped_column(String, nullable=False, index=True)
slug: Mapped[str | None] = mapped_column(String, index=True)
release_date: Mapped[date | None] = mapped_column(Date, index=True)
studio_id: Mapped[uuid.UUID | None] = mapped_column(
UUID(as_uuid=True), ForeignKey("studios.id", ondelete="SET NULL"), index=True
)
duration_sec: Mapped[int | None] = mapped_column(Integer)
description: Mapped[str | None] = mapped_column(Text)
code: Mapped[str | None] = mapped_column(String(128), index=True)
director: Mapped[str | None] = mapped_column(String(256))
# True = scena wpadła masowym backfillem katalogu (deep-crawl, głębokie strony), NIE
# świeży ingest. Tube podają datę importu jako release_date, więc stary backfill
# udaje "nowość" (licznik +N w ulubionych). Ten flag pozwala go wykluczyć z "nowych"
# bez chowania sceny. Ustawiany przy TWORZENIU (deep_crawl page > próg); świeży
# latest-crawl + delta TPDB/StashDB → False. Patrz app/scheduler/deep_crawl.py.
backfill: Mapped[bool] = mapped_column(
Boolean, nullable=False, server_default="false", index=True
)
class SceneExternalRef(Base):
__tablename__ = "scene_external_refs"
source_id: Mapped[uuid.UUID] = mapped_column(
UUID(as_uuid=True), ForeignKey("sources.id", ondelete="CASCADE"), primary_key=True
)
external_id: Mapped[str] = mapped_column(String, primary_key=True)
scene_id: Mapped[uuid.UUID] = mapped_column(
UUID(as_uuid=True), ForeignKey("scenes.id", ondelete="CASCADE"), nullable=False, index=True
)
confidence: Mapped[float] = mapped_column(Float, nullable=False, default=1.0, server_default="1.0")
url: Mapped[str | None] = mapped_column(String(1024))
first_seen: Mapped[datetime] = mapped_column(
DateTime(timezone=True), server_default=func.now(), nullable=False
)
last_seen: Mapped[datetime] = mapped_column(
DateTime(timezone=True), server_default=func.now(), nullable=False
)
class SceneFingerprint(Base):
"""Fingerprinty zaciągnięte ze źródeł (TPDB/StashDB) — nie liczone lokalnie."""
__tablename__ = "scene_fingerprints"
__table_args__ = (UniqueConstraint("scene_id", "kind", "value"),)
id: Mapped[uuid.UUID] = mapped_column(
UUID(as_uuid=True), primary_key=True, server_default=func.gen_random_uuid()
)
scene_id: Mapped[uuid.UUID] = mapped_column(
UUID(as_uuid=True), ForeignKey("scenes.id", ondelete="CASCADE"), nullable=False, index=True
)
kind: Mapped[FingerprintKind] = mapped_column(Enum(FingerprintKind, name="fingerprint_kind"))
value: Mapped[str] = mapped_column(String(128), nullable=False, index=True)
source_id: Mapped[uuid.UUID | None] = mapped_column(
UUID(as_uuid=True), ForeignKey("sources.id", ondelete="SET NULL")
)
class ScenePerformer(Base):
__tablename__ = "scene_performers"
scene_id: Mapped[uuid.UUID] = mapped_column(
UUID(as_uuid=True), ForeignKey("scenes.id", ondelete="CASCADE"), primary_key=True
)
performer_id: Mapped[uuid.UUID] = mapped_column(
UUID(as_uuid=True), ForeignKey("performers.id", ondelete="CASCADE"), primary_key=True
)
role: Mapped[str | None] = mapped_column(String(64))
position: Mapped[int | None] = mapped_column(Integer)
as_alias: Mapped[str | None] = mapped_column(String(256))
class SceneTag(Base):
__tablename__ = "scene_tags"
scene_id: Mapped[uuid.UUID] = mapped_column(
UUID(as_uuid=True), ForeignKey("scenes.id", ondelete="CASCADE"), primary_key=True
)
tag_id: Mapped[uuid.UUID] = mapped_column(
UUID(as_uuid=True), ForeignKey("tags.id", ondelete="CASCADE"), primary_key=True
)
source_id: Mapped[uuid.UUID | None] = mapped_column(
UUID(as_uuid=True), ForeignKey("sources.id", ondelete="SET NULL")
)