feat(podcasts): add YouTube podcast source with transcript-first discovery

New "podcasts" source that discovers podcast content by scanning transcripts
from LLM-resolved YouTube channels. Finds content invisible to title-based
search — Acquired's "The NFL" episode mentions Taylor Swift 18x, ESPN 117x,
Netflix 102x, none in the title.

Architecture:
- LLM resolves 6-12 podcast channel @handles per topic
- Engine fetches recent episodes via yt-dlp (no video download)
- Downloads auto-captions and greps for topic keywords
- Episodes with 5+ mentions become podcast results with highlights
- Runs in parallel, ~15-20s latency, invisible in 3-min research run

Pipeline integration:
- New source module: scripts/lib/podcast_yt.py
- Registered in pipeline, normalizer, signals, planner, render
- CLI flag: --podcast-channels=AcquiredFM,lexfridman,...
- SOURCE_QUALITY: 0.88 (above YouTube's 0.85)
- Opt-in via INCLUDE_SOURCES=podcasts or --search=podcasts

Zero new API keys. Zero new dependencies. Reuses yt-dlp + transcript pipeline.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Matt Van Horn
2026-04-10 18:18:56 -04:00
parent 86e1d77ad7
commit 49d45c2b42
8 changed files with 731 additions and 0 deletions
+1
View File
@@ -53,6 +53,7 @@ def normalize_source_items(
"xiaohongshu": _normalize_grounding,
"github": _normalize_github,
"perplexity": _normalize_grounding,
"podcasts": lambda s, i, idx, fd, td: _normalize_youtube(s, i, idx, fd, td),
}
normalizer = normalizers.get(source)
if normalizer is None:
+15
View File
@@ -40,6 +40,7 @@ from . import (
xai_x,
xiaohongshu_api,
xquik,
podcast_yt,
youtube_yt,
)
from .cluster import cluster_candidates
@@ -77,6 +78,7 @@ MOCK_AVAILABLE_SOURCES = [
"github",
"perplexity",
"xquik",
"podcasts",
]
@@ -122,6 +124,8 @@ def available_sources(config: dict[str, Any], requested_sources: list[str] | Non
available.append("pinterest")
if env.is_xquik_available(config):
available.append("xquik")
if podcast_yt.is_available() and ("podcasts" in include_sources or (requested_sources and "podcasts" in requested_sources)):
available.append("podcasts")
return available
@@ -177,6 +181,7 @@ def run(
lookback_days: int = 30,
github_user: str | None = None,
github_repos: list[str] | None = None,
podcast_channels: list[str] | None = None,
) -> schema.Report:
settings = DEPTH_SETTINGS[depth]
requested_sources = normalize_requested_sources(requested_sources)
@@ -318,6 +323,7 @@ def run(
tiktok_hashtags=tiktok_hashtags,
tiktok_creators=tiktok_creators,
ig_creators=ig_creators,
podcast_channels=podcast_channels,
)
] = (subquery, source)
@@ -348,6 +354,7 @@ def run(
tiktok_hashtags=tiktok_hashtags,
tiktok_creators=tiktok_creators,
ig_creators=ig_creators,
podcast_channels=podcast_channels,
)
except Exception as retry_exc:
bundle.errors_by_source[source] = f"{exc} (retried once, still failed: {retry_exc})"
@@ -787,6 +794,7 @@ def _retrieve_stream(
tiktok_hashtags: list[str] | None = None,
tiktok_creators: list[str] | None = None,
ig_creators: list[str] | None = None,
podcast_channels: list[str] | None = None,
) -> tuple[list[dict], dict]:
# Early exit if source was rate-limited by a sibling future
if rate_limited_sources is not None and source in rate_limited_sources:
@@ -874,6 +882,13 @@ def _retrieve_stream(
sc_token = config.get("SCRAPECREATORS_API_KEY", "")
youtube_yt.enrich_with_comments(items, token=sc_token)
return items, {}
if source == "podcasts":
podcast_query = raw_topic or subquery.search_query
result = podcast_yt.search_podcast_youtube(
podcast_query, from_date, to_date,
depth=depth, channels=podcast_channels,
)
return result.get("items", []), {}
if source == "tiktok":
# Use raw_topic so expand_tiktok_queries() generates diverse variants
# from the original user topic, not the planner's narrowed search_query.
+1
View File
@@ -71,6 +71,7 @@ SOURCE_CAPABILITIES = {
"github": {"discussion", "link"},
"grounding": {"web", "reference", "link"},
"perplexity": {"web", "reference", "analysis"},
"podcasts": {"discussion", "video_longform", "expert"},
}
DEFAULT_INTENT_CAPABILITIES = {
"comparison": {"discussion", "video", "web", "reference", "social", "link", "market"},
+390
View File
@@ -0,0 +1,390 @@
"""YouTube podcast discovery via transcript scanning.
Discovers podcast content by fetching auto-captions from LLM-resolved
YouTube podcast channels and grepping for the search topic. Finds content
invisible to title-based search — e.g., Acquired's "The NFL" episode
mentions Taylor Swift 18 times, ESPN 117 times, Netflix 102 times.
Uses yt-dlp for channel playlist fetch + caption download. No API keys.
Reuses transcript highlight extraction from youtube_yt.
"""
import math
import os
import re
import shutil
import signal
import subprocess
import sys
import tempfile
from concurrent.futures import ThreadPoolExecutor, as_completed
from typing import Any, Dict, List, Optional
from . import log
# How many recent episodes to scan per channel, by depth
EPISODES_PER_CHANNEL = {
"quick": 2,
"default": 3,
"deep": 4,
}
# Minimum topic mentions in captions to count as a hit
MENTION_THRESHOLD = 5
# Max total results to return
RESULTS_CAP = {
"quick": 4,
"default": 8,
"deep": 20,
}
# Min duration in seconds to qualify as a podcast episode
MIN_DURATION = 1200 # 20 minutes
def _log(msg: str):
log.source_log("Podcasts", msg, tty_only=False)
def is_available() -> bool:
"""Podcast source is available when yt-dlp is installed."""
return shutil.which("yt-dlp") is not None
def resolve_channel(handle: str) -> Optional[str]:
"""Resolve a YouTube @handle to a channel URL.
Tries the @handle directly first (fast, ~92% success rate).
Falls back to ytsearch1 if the handle doesn't resolve.
Returns the channel URL (https://www.youtube.com/channel/...) or None.
"""
# Try @handle directly - use the channel/videos URL format
# yt-dlp can fetch from @handle URLs directly for playlist operations
direct_url = f"https://www.youtube.com/@{handle}/videos"
try:
result = subprocess.run(
["yt-dlp", "--playlist-end", "1",
"--print", "%(channel_url)s",
"--no-download", "--no-warnings", "--ignore-config", "--no-cookies-from-browser",
direct_url],
capture_output=True, text=True, timeout=20,
)
channel_url = result.stdout.strip().split("\n")[0].strip()
if channel_url and channel_url.startswith("http"):
_log(f"Resolved @{handle} -> {channel_url}")
return channel_url
except (subprocess.TimeoutExpired, FileNotFoundError):
pass
# Fallback: search for the podcast
_log(f"@{handle} not found, trying search fallback")
try:
result = subprocess.run(
["yt-dlp", "--flat-playlist", "--playlist-end", "1",
"--print", "%(channel_url)s",
f'ytsearch1:"{handle}" podcast full episode'],
capture_output=True, text=True, timeout=20,
)
channel_url = result.stdout.strip()
if channel_url and channel_url.startswith("http"):
_log(f"Search fallback resolved {handle} -> {channel_url}")
return channel_url
except (subprocess.TimeoutExpired, FileNotFoundError):
pass
_log(f"Could not resolve channel: {handle}")
return None
def _fetch_recent_episodes(
channel_url: str,
limit: int,
from_date: str,
to_date: str,
) -> List[Dict[str, Any]]:
"""Fetch recent long-form episodes from a channel.
Returns list of dicts with video_id, title, channel, duration, date, views, likes.
Filters to episodes with duration >= MIN_DURATION.
"""
import json as _json
try:
result = subprocess.run(
["yt-dlp", f"--playlist-end={limit + 2}",
"--dump-json", "--no-download", "--no-warnings", "--ignore-config", "--no-cookies-from-browser",
f"{channel_url}/videos"],
capture_output=True, text=True, timeout=60,
)
except (subprocess.TimeoutExpired, FileNotFoundError):
return []
episodes = []
for line in result.stdout.strip().split("\n"):
line = line.strip()
if not line:
continue
try:
video = _json.loads(line)
except _json.JSONDecodeError:
continue
video_id = video.get("id", "")
title = video.get("title", "")
channel = video.get("channel", video.get("uploader", ""))
duration = video.get("duration") or 0
upload_date_raw = video.get("upload_date", "")
views = video.get("view_count") or 0
likes = video.get("like_count") or 0
# Convert YYYYMMDD to YYYY-MM-DD
date_str = None
if upload_date_raw and len(upload_date_raw) >= 8:
date_str = f"{upload_date_raw[:4]}-{upload_date_raw[4:6]}-{upload_date_raw[6:8]}"
# Filter: duration >= MIN_DURATION
if duration < MIN_DURATION:
continue
# Filter: within date range (soft - keep if no date available)
if date_str and (date_str < from_date or date_str > to_date):
continue
episodes.append({
"video_id": video_id,
"title": title,
"channel_name": channel,
"duration": duration,
"date": date_str,
"views": views,
"likes": likes,
"url": f"https://www.youtube.com/watch?v={video_id}",
})
return episodes[:limit]
def _fetch_captions(video_id: str, temp_dir: str) -> Optional[str]:
"""Fetch auto-captions for a video. Returns caption text or None."""
out_template = os.path.join(temp_dir, f"cap_{video_id}")
try:
subprocess.run(
["yt-dlp", "--write-auto-sub", "--sub-lang", "en",
"--skip-download", "--sub-format", "vtt",
"-o", out_template,
f"https://www.youtube.com/watch?v={video_id}"],
capture_output=True, text=True, timeout=30,
)
except (subprocess.TimeoutExpired, FileNotFoundError):
return None
vtt_path = f"{out_template}.en.vtt"
if not os.path.exists(vtt_path):
return None
try:
with open(vtt_path, "r", encoding="utf-8") as f:
text = f.read()
os.remove(vtt_path)
# Strip VTT formatting: timestamps, alignment, tags, duplicate lines
# VTT auto-captions repeat lines as they scroll, so deduplicate
lines = []
prev_line = ""
for line in text.split("\n"):
line = line.strip()
if not line:
continue
if line.startswith("WEBVTT") or line.startswith("Kind:") or line.startswith("Language:"):
continue
if re.match(r"^\d{2}:\d{2}:", line):
continue
if re.match(r"^NOTE\b", line):
continue
if "align:" in line or "position:" in line:
continue
# Strip inline VTT tags like <c>, </c>, timestamps
cleaned = re.sub(r"<[^>]+>", "", line)
cleaned = cleaned.strip()
if cleaned and not re.match(r"^\d+$", cleaned) and cleaned != prev_line:
lines.append(cleaned)
prev_line = cleaned
return " ".join(lines)
except Exception:
return None
def _count_mentions(text: str, topic: str) -> int:
"""Count case-insensitive topic mentions in text."""
# Build a regex pattern from the topic words
# For multi-word topics like "Taylor Swift", search for the full phrase
pattern = re.escape(topic.strip())
return len(re.findall(pattern, text, re.IGNORECASE))
def _extract_mention_context(text: str, topic: str, max_excerpts: int = 3) -> List[str]:
"""Extract text snippets around topic mentions for highlights."""
words = text.split()
topic_lower = topic.lower()
excerpts = []
for i, word in enumerate(words):
# Check if we're near a mention
window = " ".join(words[max(0, i - 5):i + 15]).lower()
if topic_lower in window and len(excerpts) < max_excerpts:
start = max(0, i - 10)
end = min(len(words), i + 30)
excerpt = " ".join(words[start:end])
# Avoid duplicate excerpts
if not any(excerpt[:50] in e for e in excerpts):
excerpts.append(excerpt)
return excerpts
def _scan_channel(
handle: str,
topic: str,
from_date: str,
to_date: str,
episodes_limit: int,
) -> List[Dict[str, Any]]:
"""Scan a single channel's recent episodes for topic mentions.
Returns list of hit items with mention_count and transcript data.
"""
# Step 1: Resolve channel handle to URL
channel_url = resolve_channel(handle)
if not channel_url:
return []
# Step 2: Fetch recent long-form episodes
episodes = _fetch_recent_episodes(channel_url, episodes_limit, from_date, to_date)
if not episodes:
_log(f"No recent long-form episodes from {handle}")
return []
_log(f"Scanning {len(episodes)} episodes from {handle}")
# Step 3: Fetch captions and grep for topic
hits = []
with tempfile.TemporaryDirectory() as temp_dir:
for ep in episodes:
caption_text = _fetch_captions(ep["video_id"], temp_dir)
if not caption_text:
continue
mention_count = _count_mentions(caption_text, topic)
if mention_count < MENTION_THRESHOLD:
continue
# Extract highlights around the mentions
from .youtube_yt import extract_transcript_highlights
highlights = extract_transcript_highlights(caption_text, topic, limit=5)
mention_excerpts = _extract_mention_context(caption_text, topic)
# Cap transcript for storage
words = caption_text.split()
transcript_snippet = " ".join(words[:5000]) if len(words) > 5000 else caption_text
hits.append({
"video_id": ep["video_id"],
"title": ep["title"],
"channel_name": ep["channel_name"],
"url": ep["url"],
"date": ep["date"],
"duration": ep["duration"],
"engagement": {
"views": ep["views"],
"likes": ep["likes"],
},
"mention_count": mention_count,
"transcript_snippet": transcript_snippet,
"transcript_highlights": highlights,
"mention_excerpts": mention_excerpts,
"relevance": min(1.0, mention_count / 50),
"why_relevant": f"Podcast: {ep['channel_name']} - {ep['title'][:60]} ({mention_count} mentions)",
})
_log(f" HIT: {ep['title'][:60]} ({mention_count} mentions)")
return hits
def search_podcast_youtube(
topic: str,
from_date: str,
to_date: str,
depth: str = "default",
channels: Optional[List[str]] = None,
) -> Dict[str, Any]:
"""Discover podcast content by scanning transcripts of resolved channels.
Args:
topic: Search topic
from_date: Start date (YYYY-MM-DD)
to_date: End date (YYYY-MM-DD)
depth: 'quick', 'default', or 'deep'
channels: List of YouTube @handles to scan
Returns:
Dict with 'items' list. Each item has transcript and mention data.
"""
if not is_available():
_log("yt-dlp not installed")
return {"items": [], "error": "yt-dlp not installed"}
if not channels:
_log("No podcast channels provided")
return {"items": []}
episodes_limit = EPISODES_PER_CHANNEL.get(depth, EPISODES_PER_CHANNEL["default"])
results_cap = RESULTS_CAP.get(depth, RESULTS_CAP["default"])
_log(f"Scanning {len(channels)} podcast channels for '{topic}' (depth={depth}, {episodes_limit} eps/channel)")
# Scan channels in parallel
all_hits: List[Dict[str, Any]] = []
max_workers = min(4, len(channels))
with ThreadPoolExecutor(max_workers=max_workers) as executor:
futures = {
executor.submit(
_scan_channel, handle, topic, from_date, to_date, episodes_limit,
): handle
for handle in channels
}
for future in as_completed(futures):
handle = futures[future]
try:
hits = future.result()
all_hits.extend(hits)
except Exception as exc:
_log(f"Error scanning {handle}: {type(exc).__name__}: {exc}")
# Deduplicate by video_id
seen = set()
unique_hits = []
for hit in all_hits:
vid = hit["video_id"]
if vid not in seen:
seen.add(vid)
unique_hits.append(hit)
# Score: mention_count * log(views + 1)
for hit in unique_hits:
views = hit["engagement"].get("views", 0)
hit["_score"] = hit["mention_count"] * math.log(views + 1)
# Sort by score descending
unique_hits.sort(key=lambda x: x["_score"], reverse=True)
# Cap results
results = unique_hits[:results_cap]
# Clean up internal scoring field
for hit in results:
hit.pop("_score", None)
_log(f"Found {len(results)} podcast hits across {len(channels)} channels")
return {"items": results}
+1
View File
@@ -14,6 +14,7 @@ SOURCE_LABELS = {
"x": "X",
"github": "GitHub",
"perplexity": "Perplexity",
"podcasts": "Podcasts",
}
+1
View File
@@ -19,6 +19,7 @@ SOURCE_QUALITY = {
"polymarket": 0.5,
"instagram": 0.58,
"tiktok": 0.58,
"podcasts": 0.88,
}