Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Most efficient way to retrieve recent uploads from multiple channels with YouTube API v3

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?

like image 234
amba88 Avatar asked Nov 10 '22 08:11

amba88


1 Answers

You can send multiple request with Sending Batch Requests

like image 82
hoozecn Avatar answered Nov 15 '22 07:11

hoozecn