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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With