要在 YouTube 接口中监听来自特定频道的新上传,您可以使用 YouTube Data API v3。具体步骤如下:
以下是一个示例 Python 脚本,展示了如何实现这一过程:
import requests
import time
# 替换为您的 API 密钥
API_KEY = 'YOUR_API_KEY'
# 替换为您要监听的频道 ID
CHANNEL_ID = 'YOUR_CHANNEL_ID'
# 获取频道的上传播放列表 ID
def get_upload_playlist_id(channel_id):
url = f'https://www.googleapis.com/youtube/v3/channels?part=contentDetails&id={channel_id}&key={API_KEY}'
response = requests.get(url)
data = response.json()
if 'items' in data and len(data['items']) > 0:
return data['items'][0]['contentDetails']['relatedPlaylists']['uploads']
else:
raise Exception('无法获取上传播放列表 ID')
# 获取播放列表中的视频
def get_playlist_videos(playlist_id):
url = f'https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId={playlist_id}&maxResults=5&key={API_KEY}'
response = requests.get(url)
data = response.json()
videos = []
if 'items' in data:
for item in data['items']:
video_id = item['snippet']['resourceId']['videoId']
title = item['snippet']['title']
videos.append((video_id, title))
return videos
# 主函数
def main():
upload_playlist_id = get_upload_playlist_id(CHANNEL_ID)
print(f'上传播放列表 ID: {upload_playlist_id}')
# 存储已处理的视频 ID
processed_videos = set()
while True:
videos = get_playlist_videos(upload_playlist_id)
for video_id, title in videos:
if video_id not in processed_videos:
print(f'新视频上传: {title} (https://www.youtube.com/watch?v={video_id})')
processed_videos.add(video_id)
# 每隔60秒检查一次
time.sleep(60)
if __name__ == '__main__':
main()
https://www.googleapis.com/youtube/v3/channels
端点获取频道的上传播放列表 ID。https://www.googleapis.com/youtube/v3/playlistItems
端点获取播放列表中的视频。领取专属 10元无门槛券
手把手带您无忧上云