使用YouTube API v3从多个频道检索最近上传的最有效方法(Most efficient way to retrieve recent uploads from multiple channe

编程入门 行业动态 更新时间:2024-10-28 03:24:41
使用YouTube API v3从多个频道检索最近上传的最有效方法(Most efficient way to retrieve recent uploads from multiple channels with YouTube API v3)

使用频道ID列表,例如:

channel_ids = ['UC6nSFpj9HTCZ5t-N3Ra3-HB','UC6nSFpjSEBUCZ5t-N3Ra3-HB','UC6nrst90rsd3Z5t-N3Ra3-HC','UC6nSFpjd329th0rt-tuTH3-HA']

我想使用YouTube Data API v3检索所有这些频道的50个最新视频上传,尽可能少地使用http请求并尽可能缩短时间。

我目前的做法是:

from apiclient.discovery import build youtube = build('youtube', 'v3', developerKey=key) channel_ids = ['UC6nSFpj9HTCZ5t-N3Ra3-HB', 'UC6nSFpjSEBUCZ5t-N3Ra3-HB', 'UC6nrst90rsd3Z5t-N3Ra3-HC', 'UC6nSFpjd329th0rt-tuTH3-HA'] videos_by_channel = {} for channel_id in channel_ids: search_response = youtube.search().list(part="id", type='video', order='date', channelId=channel_id, maxResults=50).execute() videoIds = [] for search_result in search_response.get("items", []): if search_result["id"]["kind"] == "youtube#video": videoIds.append(search_result['id']['videoId']) search_response = youtube.videos().list( id=",".join(videoIds), part="id,snippet" ).execute() videos_by_channel[channel_id] = search_response

它的工作原理,但使用大量的服务器调用,它不是很快。 我已阅读文档,但找不到更快的方法,有什么想法?

Using a list of channel IDs such as:

channel_ids = ['UC6nSFpj9HTCZ5t-N3Ra3-HB', 'UC6nSFpjSEBUCZ5t-N3Ra3-HB', 'UC6nrst90rsd3Z5t-N3Ra3-HC', 'UC6nSFpjd329th0rt-tuTH3-HA']

I want to retrieve the 50 most recent video uploads for all those channels using the YouTube Data API v3, using as few http requests and as little time as possible.

The way I'm currently doing it is:

from apiclient.discovery import build youtube = build('youtube', 'v3', developerKey=key) channel_ids = ['UC6nSFpj9HTCZ5t-N3Ra3-HB', 'UC6nSFpjSEBUCZ5t-N3Ra3-HB', 'UC6nrst90rsd3Z5t-N3Ra3-HC', 'UC6nSFpjd329th0rt-tuTH3-HA'] videos_by_channel = {} for channel_id in channel_ids: search_response = youtube.search().list(part="id", type='video', order='date', channelId=channel_id, maxResults=50).execute() videoIds = [] for search_result in search_response.get("items", []): if search_result["id"]["kind"] == "youtube#video": videoIds.append(search_result['id']['videoId']) search_response = youtube.videos().list( id=",".join(videoIds), part="id,snippet" ).execute() videos_by_channel[channel_id] = search_response

It works but uses a lot of server calls and it isn't exactly fast. I've read the documentation but can't find a faster method, any ideas?

最满意答案

您可以通过发送批量请求发送多个请求

You can send multiple request with Sending Batch Requests

更多推荐

本文发布于:2023-08-03 00:16:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1382433.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   最有效   上传   方法   频道

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!