feat(search): live YouTube search backend

Add a live YouTube search that materialises results into the shared catalog so
they render with the normal feed cards + in-app player and gain per-user state.

- YouTubeClient.search_videos(): search.list (100 units), embeddable-only, returns
  flat stubs + nextPageToken; surfaces liveBroadcastContent for live filtering.
- routes/search.py GET /api/search/youtube: require_human + per-user daily cap
  (search_daily_limit_per_user, default 70) + can_spend pre-check (429 on either);
  drops live/upcoming, upserts channel stubs (channels.list) + video stubs, enriches
  (videos.list), runs the youtube.com/shorts probe, then excludes Shorts/live and
  returns feed cards in relevance order with the YouTube pageToken as the cursor.
- Provenance: videos.via_search / channels.from_search (migration 0028) flag
  search-discovered rows; the feed hides them from the Library (scope=all) by default
  via exclude_search_discovered, leaving the Mine feed untouched.
- quota.actions_today() counts a user's per-action events today for the cap; only the
  search.list call is attributed VIDEOS_SEARCH so the counter is exactly 1 per search.
This commit is contained in:
npeter83 2026-06-29 02:01:31 +02:00
parent b1ed706cab
commit 9b1bdb6b42
9 changed files with 341 additions and 0 deletions

View file

@ -124,6 +124,7 @@ def _filtered_query(
include_live: bool,
show: str,
scope: str = "my",
exclude_search_discovered: bool = True,
exclude_tag_category: str | None = None,
) -> tuple[Select, object]:
"""Build the feed query (joins + all WHERE filters), shared by /feed and /feed/count.
@ -171,6 +172,12 @@ def _filtered_query(
# either way they shouldn't show in any feed view meanwhile.
query = query.where(Video.unavailable_since.is_(None))
# Hide videos that only entered the catalog via a live YouTube search (search-discovered
# "noise") from the Library by default. Only meaningful in "all" scope: "my" already
# restricts to subscribed channels, where a once-searched video is legitimately the user's.
if scope == "all" and exclude_search_discovered:
query = query.where(Video.via_search.is_(False))
if channel_id:
query = query.where(Video.channel_id == channel_id)
if min_duration is not None:
@ -271,6 +278,7 @@ def _feed_params(
include_live: bool = False,
show: str = "unwatched",
scope: str = "my",
exclude_search_discovered: bool = True,
) -> dict:
return {
"tags": tags,
@ -287,6 +295,7 @@ def _feed_params(
"include_live": include_live,
"show": show,
"scope": scope,
"exclude_search_discovered": exclude_search_discovered,
}