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

 用户um940d5n0q 发布于 2023-01-14 16:06
  • php
  • 使用频道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
    

    它工作但使用了很多服务器调用,并不是很快.我已经阅读了文档,但找不到更快的方法,任何想法?

    撰写答案
    今天,你开发时遇到什么问题呢?
    立即提问
    热门标签
    PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
    Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有