关于 youtube
youtube 是通过 curl 查询 YouTube Data API v3 的技能。它帮助用户使用 API 密钥获取视频、频道、播放列表、评论和热门内容数据。
主要功能
- 01按条件搜索视频
- 02获取视频详情与统计数据
- 03获取频道信息
- 04列出播放列表视频
- 05获取评论与热门视频
适用场景
- 01按主题或类别查找视频
- 02查看视频播放量和时长
- 03查看频道信息或播放列表内容
- 04获取评论或地区热门视频
安装
复制下面的命令,在终端中运行。
npx skills add https:/ / github.com/ manojbajaj95/ claude-gtm-plugin --skill youtube安装后在 Claude Code 中输入/youtube确认已加载。
详细介绍
YouTube Data API
通过直接调用 curl 使用 YouTube Data API v3,搜索视频、获取视频详情、获取频道信息、列出播放列表项目以及获取评论。
官方文档:
https://developers.google.com/youtube/v3
使用场景
在需要以下操作时使用此技能:
- 按关键词或筛选条件搜索视频
- 获取视频详情(标题、说明、统计数据、时长)
- 获取频道信息(订阅者数量、视频数量、说明)
- 列出播放列表项目(播放列表中的视频)
- 获取视频的评论
- 按地区获取热门视频
前提条件
1. 创建 Google Cloud 项目
- 前往 Google Cloud Console
- 创建新项目或选择现有项目
- 前往“APIs & Services” > “Library”
- 搜索“YouTube Data API v3”并启用
2. 获取 API 密钥
- 前往“APIs & Services” > “Credentials”
- 点击“Create Credentials” > “API Key”
- 复制 API 密钥
export YOUTUBE_API_KEY="your-api-key-here"
3.(可选)限制 API 密钥
用于生产环境时,请限制密钥:
- 应用限制:HTTP 引用来源、IP 地址
- API 限制:仅限 YouTube Data API v3
**重要:**在将使用
$VAR的命令通过管道传给另一条命令时,请用bash -c '...'包裹包含$VAR的命令。由于 Claude Code 的一个错误,直接使用管道时环境变量会被悄悄清除。bash -c 'curl -s "https://api.example.com" -H "Authorization: Bearer $API_KEY" | jq .'
使用方法
基础 URL:https://www.googleapis.com/youtube/v3
1. 搜索视频
bash -c 'curl -s "https://www.googleapis.com/youtube/v3/search?part=snippet&q=kubernetes+tutorial&type=video&maxResults=5&key=${YOUTUBE_API_KEY}"' | jq '.items[] | {videoId: .id.videoId, title: .snippet.title, channel: .snippet.channelTitle}'
2. 使用筛选条件搜索
搜索今年上传的视频,并按观看次数排序:
bash -c 'curl -s "https://www.googleapis.com/youtube/v3/search?part=snippet&q=react+hooks&type=video&order=viewCount&publishedAfter=2024-01-01T00:00:00Z&maxResults=10&key=${YOUTUBE_API_KEY}"' | jq '.items[] | {videoId: .id.videoId, title: .snippet.title}'
3. 获取视频详情
将 <your-video-id> 替换为实际的视频 ID:
bash -c 'curl -s "https://www.googleapis.com/youtube/v3/videos?part=snippet,statistics,contentDetails&id=<your-video-id>&key=${YOUTUBE_API_KEY}"' | jq '.items[0] | {title: .snippet.title, views: .statistics.viewCount, likes: .statistics.likeCount, duration: .contentDetails.duration}'
4. 获取多个视频
将 <your-video-id-1>、<your-video-id-2>、<your-video-id-3> 替换为实际的视频 ID:
bash -c 'curl -s "https://www.googleapis.com/youtube/v3/videos?part=snippet,statistics&id=<your-video-id-1>,<your-video-id-2>,<your-video-id-3>&key=${YOUTUBE_API_KEY}"' | jq '.items[] | {id: .id, title: .snippet.title, views: .statistics.viewCount}'
5. 获取热门视频
bash -c 'curl -s "https://www.googleapis.com/youtube/v3/videos?part=snippet,statistics&chart=mostPopular®ionCode=US&maxResults=10&key=${YOUTUBE_API_KEY}"' | jq '.items[] | {title: .snippet.title, channel: .snippet.channelTitle, views: .statistics.viewCount}'
6. 按 ID 获取频道
将 <your-channel-id> 替换为实际的频道 ID:
bash -c 'curl -s "https://www.googleapis.com/youtube/v3/channels?part=snippet,statistics&id=<your-channel-id>&key=${YOUTUBE_API_KEY}"' | jq '.items[0] | {title: .snippet.title, subscribers: .statistics.subscriberCount, videos: .statistics.videoCount}'
7. 按句柄获取频道
bash -c 'curl -s "https://www.googleapis.com/youtube/v3/channels?part=snippet,statistics&forHandle=@GoogleDevelopers&key=${YOUTUBE_API_KEY}"' | jq '.items[0] | {id: .id, title: .snippet.title, subscribers: .statistics.subscriberCount}'
8. 按用户名获取频道
bash -c 'curl -s "https://www.googleapis.com/youtube/v3/channels?part=snippet,statistics&forUsername=GoogleDevelopers&key=${YOUTUBE_API_KEY}"' | jq '.items[0] | {id: .id, title: .snippet.title, description: .snippet.description}'
9. 列出播放列表项目
将 <your-playlist-id> 替换为实际的播放列表 ID:
bash -c 'curl -s "https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId=<your-playlist-id>&maxResults=20&key=${YOUTUBE_API_KEY}"' | jq '.items[] | {position: .snippet.position, title: .snippet.title, videoId: .snippet.resourceId.videoId}'
10. 获取频道上传内容的播放列表
先获取频道的上传内容播放列表 ID,然后列出视频。将 <your-channel-id> 替换为实际的频道 ID:
bash -c 'curl -s "https://www.googleapis.com/youtube/v3/channels?part=contentDetails&id=<your-channel-id>&key=${YOUTUBE_API_KEY}"' | jq -r '.items[0].contentDetails.relatedPlaylists.uploads'
11. 获取视频评论
将 <your-video-id> 替换为实际的视频 ID:
bash -c 'curl -s "https://www.googleapis.com/youtube/v3/commentThreads?part=snippet&videoId=<your-video-id>&maxResults=20&order=relevance&key=${YOUTUBE_API_KEY}"' | jq '.items[] | {author: .snippet.topLevelComment.snippet.authorDisplayName, text: .snippet.topLevelComment.snippet.textDisplay, likes: .snippet.topLevelComment.snippet.likeCount}'
12. 搜索评论
将 <your-video-id> 替换为实际的视频 ID:
bash -c 'curl -s "https://www.googleapis.com/youtube/v3/commentThreads?part=snippet&videoId=<your-video-id>&searchTerms=great+video&maxResults=10&key=${YOUTUBE_API_KEY}"' | jq '.items[] | {author: .snippet.topLevelComment.snippet.authorDisplayName, text: .snippet.topLevelComment.snippet.textDisplay}'
13. 获取视频类别
bash -c 'curl -s "https://www.googleapis.com/youtube/v3/videoCategories?part=snippet®ionCode=US&key=${YOUTUBE_API_KEY}"' | jq '.items[] | {id: .id, title: .snippet.title}'
14. 按类别搜索视频
bash -c 'curl -s "https://www.googleapis.com/youtube/v3/search?part=snippet&type=video&videoCategoryId=28&maxResults=10&key=${YOUTUBE_API_KEY}"' | jq '.items[] | {videoId: .id.videoId, title: .snippet.title}'
注意:类别 28 = 科学与技术
15. 获取频道中的播放列表
将 <your-channel-id> 替换为实际的频道 ID:
bash -c 'curl -s "https://www.googleapis.com/youtube/v3/playlists?part=snippet&channelId=<your-channel-id>&maxResults=20&key=${YOUTUBE_API_KEY}"' | jq '.items[] | {id: .id, title: .snippet.title, description: .snippet.description}'
常见视频类别
| ID | 类别 |
|---|---|
| 1 | 电影与动画 |
| 10 | 音乐 |
| 17 | 体育 |
| 20 | 游戏 |
| 22 | 人物与博客 |
| 24 | 娱乐 |
| 25 | 新闻与政治 |
| 26 | 技巧与风格 |
| 27 | 教育 |
| 28 | 科学与技术 |
Part 参数选项
视频
snippet- 标题、说明、缩略图、频道statistics- 观看次数、点赞数、评论数contentDetails- 时长、清晰度、字幕status- 上传状态、隐私设置、许可player- 可嵌入播放器
频道
snippet- 标题、说明、缩略图statistics- 订阅者、视频、观看次数contentDetails- 相关播放列表(上传内容、点赞内容)brandingSettings- 频道自定义设置
分页
使用响应中的 nextPageToken 获取更多结果。将 <your-next-page-token> 替换为上一条响应中的实际令牌:
bash -c 'curl -s "https://www.googleapis.com/youtube/v3/search?part=snippet&q=python&type=video&maxResults=50&pageToken=<your-next-page-token>&key=${YOUTUBE_API_KEY}"' | jq '.items[] | {title: .snippet.title}'
指南
- 配额限制:API 每日配额为 10,000 单位。搜索消耗 100 单位,其他大多数操作消耗 1 单位
- 速率限制:遇到 403/429 错误时实施指数退避
- API 密钥安全:切勿在客户端代码中暴露 API 密钥
- 缓存:缓存响应以减少配额用量
- 视频 ID:从
youtube.com/watch?v=VIDEO_ID或youtu.be/VIDEO_ID这类 URL 中提取