Files
karaoke-gen/backend/services/youtube_description.py
T
Andrew Beveridge ☄️andClaude Opus 4.8 a344783376 gen: unify YouTube descriptions + automated back-catalogue rewrite (#978)
## Summary

Unifies the descriptions across all ~1.6k videos on
youtube.com/@nomadkaraoke to one consistent, conversion-focused
template, makes new uploads use it automatically, and adds a
**reusable** automated system to re-apply any future template change
across the whole channel.

Old descriptions were inconsistent: pre-2024 ones linked a dead $10
Fiverr gig; newer ones led with "AI-powered" (bad rep among viewers) and
linked nowhere useful.

## New template
Single source of truth: `Settings.default_youtube_description` rendered
by `backend/services/youtube_description.py` (placeholders `{title}
{artist} {artist_hashtag} {brand_code}`). Drops all "AI" framing; leads
with the craft; CTA to the requests board then the referral link
`nomadkaraoke.com/r/youtube` (50% off, tracked via referrals); Nomad
Discord + decide + KaraokeNerds; fair-use line; preserves the brand
code.

## Live pipeline (new uploads)
Both upload sites (`video_worker_orchestrator.py`,
`youtube_queue_processor.py`) now render via the shared module +
enriched tags — new uploads use the new template automatically.

## Bulk rewrite of the back catalogue
- Shared logic `backend/services/youtube_backfill.py` (classify / target
/ render / update-body / enumeration). **High-confidence-only**
eligibility (title ends in `(Karaoke)`) + curated `skip_ids.txt` /
`include_ids.txt` lists shipped in the package.
- **Manual CLI** `scripts/youtube-descriptions/` (`analyze` / `apply` /
`status`) — read-only report + full old→new diffs before any write.
- **Automated daily drain**
`backend/workers/youtube_description_backfill_worker.py` + `POST
/api/internal/youtube-backfill/run` + Cloud Scheduler (Pulumi). Progress
in Firestore keyed by a **template fingerprint**, so any future template
change auto-re-drains the whole channel with no manual reset.
Quota-coordinated with uploads (reserves headroom via
`YouTubeQuotaService`); Postmark progress + completion emails.

## Verified
- Analyzed all 1,631 videos → **1,614 to rewrite** (1,610 clean + 4
rescued truncated titles), **17 skipped** (lyrics-with-vocals, demos,
tutorials), zero false positives.
- One video (`PmwRXIZoCpg`) updated live and verified on YouTube (new
description + enriched tags; title/category preserved).

## Tests
42 new unit tests (renderer, classifier/targeting/update-body, worker:
full drain, budget cap, quota reserve, dry-run, completion/idempotency,
template-change cycle, quotaExceeded stop). Local review done via the
local code-review fallback (CodeRabbit CLI was rate-limited) — 3
findings fixed, 2 documented trade-offs.

## Deploy notes
- Backend auto-deploys on merge (worker + endpoint + config + data
files).
- **`pulumi up` required** to create the
`youtube-description-backfill-daily` Cloud Scheduler job.
- New config env (all defaulted): `YOUTUBE_BACKFILL_ENABLED`,
`..._DAILY_MAX_UPDATES`, `..._QUOTA_RESERVE`, `..._ENRICH_TAGS`,
`..._REPORT_EMAIL`.

Plan/architecture:
`docs/archive/2026-09-03-youtube-descriptions-plan.md`

🤖 Generated with [Claude Code](https://claude.com/claude-code)

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **New Features**
- YouTube uploads now use standardized descriptions with artist, title,
brand-code, and hashtag details.
- Tags are generated consistently from karaoke, artist, and title
information.
- Existing channel videos can be analyzed and updated in quota-limited
batches, with dry-run support and progress reporting.
- Added scheduled daily backfills and administrative controls for update
limits, exclusions, and overrides.
- Added safeguards for low-confidence titles and explicitly skipped
videos.

- **Documentation**
- Added guidance for automated and manual YouTube description management
workflows.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-09-03 02:20:19 -04:00

144 lines
4.9 KiB
Python

"""
YouTube description + tags rendering (single source of truth).
Both the live upload pipeline (video_worker_orchestrator, youtube_queue_processor)
and the bulk-rewrite maintenance tool (scripts/youtube-descriptions) render
descriptions through this module so on-channel descriptions stay consistent with
newly published ones.
The canonical template lives in ``Settings.default_youtube_description``
(``backend/config.py``, overridable via the ``DEFAULT_YOUTUBE_DESCRIPTION`` env
var). It may contain these placeholders, all optional:
{title} - song title
{artist} - artist name
{artist_hashtag} - artist name reduced to a valid hashtag (e.g. "#Coldplay")
{brand_code} - the NOMAD-#### brand code line
Templates without placeholders still work: legacy/tenant-custom templates that
predate placeholders keep the historical behaviour of appending
``\n\nBrand Code: <code>`` when a brand code is available.
"""
import re
from typing import List, Optional
# Total length budget for the YouTube ``tags`` field (API rejects > 500 chars,
# counted as the sum of tag lengths plus separators). Stay comfortably under.
_MAX_TAGS_TOTAL_CHARS = 460
def hashtagify(text: Optional[str]) -> str:
"""Reduce a string to the characters that are valid inside a hashtag.
Keeps unicode word characters (letters, digits, underscore) and drops
everything else, so ``"P!nk"`` -> ``"Pnk"`` and ``"Twenty One Pilots"`` ->
``"TwentyOnePilots"``. Returns ``""`` when nothing usable remains.
"""
if not text:
return ""
return re.sub(r"[^\w]", "", text, flags=re.UNICODE)
def _collapse_blank_lines(text: str) -> str:
"""Collapse runs of 3+ newlines down to a blank-line separator."""
return re.sub(r"\n{3,}", "\n\n", text)
def render_youtube_description(
artist: Optional[str] = None,
title: Optional[str] = None,
brand_code: Optional[str] = None,
template: Optional[str] = None,
) -> str:
"""Render a YouTube description from the template + per-video values.
Args:
artist: Artist name (may be empty/unknown).
title: Song title (may be empty/unknown).
brand_code: NOMAD-#### style code, or falsy to omit the brand-code line.
template: Override template. Defaults to
``Settings.default_youtube_description``.
Returns:
The fully rendered description string, stripped of trailing whitespace.
"""
if template is None:
# Lazy import to avoid any import-time coupling with config.
from backend.config import get_settings
template = get_settings().default_youtube_description or ""
result = template
# Simple token substitution (avoid str.format so stray braces in custom
# templates never raise).
result = result.replace("{artist}", artist or "")
result = result.replace("{title}", title or "")
artist_tag = hashtagify(artist)
if artist_tag:
result = result.replace("{artist_hashtag}", artist_tag)
else:
# Drop the artist hashtag entirely, including a leading separator space.
result = result.replace(" #{artist_hashtag}", "")
result = result.replace("#{artist_hashtag}", "")
result = result.replace("{artist_hashtag}", "")
if "{brand_code}" in result:
if brand_code:
result = result.replace("{brand_code}", brand_code)
else:
# Remove any line carrying the placeholder so we don't leave a
# dangling "Brand Code:" label.
result = "\n".join(
line for line in result.split("\n") if "{brand_code}" not in line
)
elif brand_code:
# Legacy template without a placeholder: preserve historical append.
result = f"{result}\n\nBrand Code: {brand_code}"
return _collapse_blank_lines(result).strip()
def build_youtube_tags(artist: Optional[str], title: Optional[str]) -> List[str]:
"""Build a de-duplicated, length-capped YouTube tags list.
Richer than the historical ``["karaoke", artist, title]`` for discoverability,
while respecting YouTube's ~500 char total tags budget.
"""
artist = (artist or "").strip()
title = (title or "").strip()
candidates = [
"karaoke",
"instrumental",
"lyrics",
"karaoke version",
"sing along",
"backing track",
artist,
title,
f"{artist} karaoke" if artist else "",
f"{artist} {title} karaoke" if artist and title else "",
]
tags: List[str] = []
seen = set()
total = 0
for tag in candidates:
tag = tag.strip()
if not tag:
continue
key = tag.lower()
if key in seen:
continue
# +1 approximates the separator cost the API counts between tags.
cost = len(tag) + 1
if total + cost > _MAX_TAGS_TOTAL_CHARS:
continue
seen.add(key)
tags.append(tag)
total += cost
return tags