使用YouTube API v3从多个频道检索最近上载的最有效方法

2024-10-02 00:26:08 发布

您现在位置:Python中文网/ 问答频道 /正文

使用频道ID列表,例如:

通道编号=['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

它可以工作,但是需要大量的服务器调用,而且速度不太快。我看过文档,但找不到更快的方法,有什么想法吗?在


Tags: hcidsearchyoutuberesponsechannelresult频道

热门问题