Verified Commit 676e63bd authored by Andy Dirnberger's avatar Andy Dirnberger 🍥
Browse files

[youtube] Handle pagination when fetching videos

YouTube's API doesn't allow you to fetch more than 50 videos in a single
request. If we have more than that, we need to make multiple requests.
Since YouTube provides a unique token for each page, we need to fetch
each page in order to fetch them all
parent bfce8819
......@@ -67,16 +67,26 @@ service = get_authenticated_service()
playlist_id = service.channels().list(
mine=True,
part='contentDetails',
).execute()['items'][0]['contentDetails']['relatedPlaylists']['uploads']
part="contentDetails",
).execute()["items"][0]["contentDetails"]["relatedPlaylists"]["uploads"]
videos = service.playlistItems().list(
videos_result = service.playlistItems().list(
part="snippet",
playlistId=playlist_id,
maxResults=50,
).execute()
videos = videos_result["items"]
for video in videos["items"]:
while videos_result.get("nextPageToken"):
videos_result = service.playlistItems().list(
part="snippet",
playlistId=playlist_id,
pageToken=videos_result["nextPageToken"],
maxResults=50,
).execute()
videos.extend(videos_result["items"])
for video in videos:
title = video["snippet"]["title"]
slug = slugify(title)
video_id = video["snippet"]["resourceId"]["videoId"]
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment