使用YouTube API v3从多个渠道检索最近上传的最有效方式

时间:2014-05-17 20:49:44

标签: python youtube youtube-api

使用频道ID列表,例如:

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

我想使用YouTube数据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

它有效,但使用了大量的服务器调用,并且速度并不快。我已经阅读了文档,但无法找到更快的方法,任何想法?

1 个答案:

答案 0 :(得分:1)

您可以使用Sending Batch Requests

发送多个请求