Skip to content

fix: modify the YT mappings generator to fetch all videos through pagination #1576

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 29 additions & 14 deletions generate-videos-mappings.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,37 @@
youtube = googleapiclient.discovery.build(
api_service_name, api_version, developerKey=api_key)

def get_videos(channel_id, max_results=100):
request = youtube.search().list(
part="snippet",
channelId=channel_id,
maxResults=max_results,
order="date",
type="video"
)
response = request.execute()

def get_videos(channel_id, max_results_per_page=50):
videos = []
for item in response.get("items", []):
video_id = item["id"]["videoId"]
title = item["snippet"]["title"]
videos.append({'id': video_id, 'title': title})
page_token = None # Start with no pageToken
total_fetched = 0 # Keep track of the total number of fetched videos

while total_fetched < 200: # Keep looping until 200 videos have been fetched
request = youtube.search().list(
part="snippet",
channelId=channel_id,
maxResults=max_results_per_page,
order="date",
type="video",
pageToken=page_token # Include the current pageToken
)

response = request.execute()

for item in response.get("items", []):
if total_fetched >= 200:
break # Break out of the loop if 200 videos have been fetched

video_id = item["id"]["videoId"]
title = item["snippet"]["title"]
videos.append({'id': video_id, 'title': title})
total_fetched += 1 # Increment the total_fetched count

page_token = response.get("nextPageToken") # Get the next pageToken

if not page_token:
break # Exit the loop if there are no more pages

return videos

def get_channel_id(channel_name):
Expand Down
Loading