Skip to content

chenyuxiaojin/xiaochen-skills / plugins/cyxj-youtube-topics/skills/cyxj-youtube-topics

cyxj-youtube-topics

Verified

YouTube 选题发现 + 判断。搜索 "Claude Code" 相关最近 48 小时新视频, 去重、按话题聚类、做硬信号 + 字幕内容分析,输出带 verdict(值得做/观望/跟风/跳过)+ 理由 + 差异化切口建议的选题报告。写入 Obsidian 选题库。 触发方式:「YouTube 选题」「找 YouTube 选题」「YouTube 最近有什么」「帮我找找最近的新选题」「跑一下选题发现」「有什么新视频」 边界:只做 YouTube 新视频的选题发现与判断;泛选题、短视频 IP 人群选题、文案式出选题不归本 skill。

FileKindSize
SKILL.mdskill md12.0 KB
backfill_channel_ids.pyscript5.1 KB
chinese_reference.pyscript9.2 KB
paths.pyscript6.9 KB
references/setup.mdmarkdown3.3 KB
requirements.txtmanifest9 B
subtitle_fetcher.pyscript5.8 KB
topic_judge.pyscript8.4 KB
write_topics.pyscript39.5 KB
youtube_search.pyscript29.3 KB

subtitle_fetcher.py

View on GitHub
#!/usr/bin/env python3
"""拉 YouTube 视频字幕前 N 秒的纯文本。

主路径:Apify Actor `scrape-creators/best-youtube-transcripts-scraper`。
Fallback:Supadata(独立服务商,独立 IP 池)。
karamelo 与新主路径同属 Apify、共用同一额度,保留它做兜底没有意义,因此删除。

设计约束:
- 入口签名 fetch_subtitle(video_url_or_id, max_seconds=180) 不变
- max_seconds=None 时返回全文
- 失败返回 None 不抛异常
- 返回约前 max_seconds*60 字符的纯文本(粗估,无时间戳按字符数截断;足够判断角度)
- stderr 打印耗时和失败原因
"""

import html
import json
import re
import sys
import time
import urllib.request

from paths import load_apify_token, load_supadata_key

DEFAULT_MAX_SECONDS = 180

VIDEO_ID_PATTERN = re.compile(r"([0-9A-Za-z_-]{11})")


def _normalize(video_url_or_id: str):
    """归一化为 (video_id, url)。无法解析返回 (None, None)。"""
    if re.fullmatch(r"[0-9A-Za-z_-]{11}", video_url_or_id):
        return video_url_or_id, f"https://www.youtube.com/watch?v={video_url_or_id}"
    m = VIDEO_ID_PATTERN.search(video_url_or_id)
    if not m:
        return None, None
    return m.group(1), video_url_or_id


def _truncate(text: str, max_seconds: int | None) -> str:
    if max_seconds is None:
        return text
    return text[: max_seconds * 60]


def _fetch_via_scrape_creators(vid: str, max_seconds: int | None):
    """主路径:Apify scrape-creators Actor。失败返回 None。"""
    try:
        token = load_apify_token()
        api_url = (
            "https://api.apify.com/v2/acts/"
            "scrape-creators~best-youtube-transcripts-scraper"
            "/run-sync-get-dataset-items"
        )
        body = json.dumps(
            {"videoUrls": [f"https://www.youtube.com/watch?v={vid}"]}
        ).encode("utf-8")
        req = urllib.request.Request(
            api_url,
            data=body,
            method="POST",
            headers={
                "Authorization": f"Bearer {token}",
                "Content-Type": "application/json",
                "User-Agent": "curl/8.4.0",
            },
        )
        with urllib.request.urlopen(req, timeout=60) as r:
            if not (200 <= r.status < 300):
                print(
                    f"warn scrape-creators {vid}: HTTP {r.status}",
                    file=sys.stderr,
                )
                return None
            items = json.loads(r.read())
    except Exception as e:
        print(
            f"warn scrape-creators {vid}: {type(e).__name__} {str(e)[:120]}",
            file=sys.stderr,
        )
        return None

    if not items:
        return None
    item = items[0] or {}

    text = item.get("transcript_only_text")
    if not text:
        transcript = item.get("transcript") or []
        text = " ".join(
            str(part.get("text") or "")
            for part in transcript
            if isinstance(part, dict)
        )
    if isinstance(text, str):
        text = re.sub(r"\s+", " ", html.unescape(text)).strip()
        if text:
            return _truncate(text, max_seconds)
    return None


def _fetch_via_supadata(vid: str, max_seconds: int | None):
    """Fallback:Supadata。文本模式无时间戳,按字符数粗估截断。失败返回 None。"""
    try:
        key = load_supadata_key()
    except Exception as e:
        print(
            f"warn supadata {vid}: {type(e).__name__} {str(e)[:120]}",
            file=sys.stderr,
        )
        return None
    if not key:
        return None
    api_url = (
        f"https://api.supadata.ai/v1/youtube/transcript?videoId={vid}&text=true"
    )
    req = urllib.request.Request(
        api_url,
        headers={
            "x-api-key": key,
            "User-Agent": "curl/8.4.0",
        },
    )
    try:
        with urllib.request.urlopen(req, timeout=30) as r:
            if r.status != 200:
                print(f"warn supadata {vid}: HTTP {r.status}", file=sys.stderr)
                return None
            d = json.loads(r.read())
    except Exception as e:
        print(
            f"warn supadata {vid}: {type(e).__name__} {str(e)[:120]}",
            file=sys.stderr,
        )
        return None

    text = d.get("content", "")
    if not text:
        return None
    return _truncate(text, max_seconds)


def fetch_subtitle(
    video_url_or_id: str, max_seconds: int | None = DEFAULT_MAX_SECONDS
):
    """拉字幕纯文本;max_seconds=None 返回全文。失败返回 None。
    主路径 Apify scrape-creators,失败 fallback 到 Supadata。"""
    vid, _ = _normalize(video_url_or_id)
    if not vid:
        return None

    t0 = time.monotonic()
    text = _fetch_via_scrape_creators(vid, max_seconds)
    dt = (time.monotonic() - t0) * 1000
    if text:
        print(
            f"info subtitle {vid}: scrape-creators 成功({dt:.0f}ms)",
            file=sys.stderr,
        )
        return text

    print(
        f"info subtitle {vid}: scrape-creators 未取到({dt:.0f}ms),"
        "fallback supadata",
        file=sys.stderr,
    )
    t1 = time.monotonic()
    text = _fetch_via_supadata(vid, max_seconds)
    dt2 = (time.monotonic() - t1) * 1000
    if text:
        print(f"info subtitle {vid}: supadata 成功({dt2:.0f}ms)", file=sys.stderr)
    else:
        print(f"info subtitle {vid}: supadata 也失败({dt2:.0f}ms)", file=sys.stderr)
    return text


def main():
    if len(sys.argv) < 2:
        print(
            "用法: python3 subtitle_fetcher.py <video_url_or_id> [max_seconds]",
            file=sys.stderr,
        )
        sys.exit(1)
    max_s = int(sys.argv[2]) if len(sys.argv) > 2 else DEFAULT_MAX_SECONDS
    text = fetch_subtitle(sys.argv[1], max_s)
    if text is None:
        sys.exit(2)
    print(text)


if __name__ == "__main__":
    main()
reverse-engineer-anythingmorluto/reaReverse engineer native, managed, Electron/JavaScript, packaged, firmware, and browser targets with REA, and build agent environments (AgentEnv bundles) of web applications from a live instance or a capture, and of Linux desktop apps with a task from a request. Use shipped-artifact or requested runtime evidence to explain features, compare versions, decompile code, or guide a reconstruction. Skip REA for ordinary source-repository architecture analysis.Stars: 472kUpdated Claude CodeCodexVerifiedInstall
diagnosing-superpowersobra/superpowersUse when a superpowers session went wrong and your human partner wants to know why — repeated work, ignored plans, stumbles, poor results, a skill that didn't fire, "it took too long", "why is it so expensive", "what is it doing" — or wants to build a bug report for the superpowers maintainers, for the current session or a past one identified by id or path, on any harness.Stars: 323kUpdated Claude CodeCodexVerifiedInstall
dispatching-parallel-agentsobra/superpowersUse when facing 2+ independent tasks that can be worked on without shared state or sequential dependenciesStars: 323kUpdated Claude CodeCodexVerifiedInstall
executing-plansobra/superpowersUse when executing an implementation plan in the current session as the implementer yourself — your human partner chose inline execution, or no subagent tool is availableStars: 323kUpdated Claude CodeCodexVerifiedInstall