Skip to content

Commit ea5ed51

Browse files
authored
fix: modify the YT mappings generator to fetch all videos through pagination (graphql#1576)
1 parent 9dd6946 commit ea5ed51

File tree

2 files changed

+413
-14
lines changed

2 files changed

+413
-14
lines changed

generate-videos-mappings.py

+29-14
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,37 @@
1313
youtube = googleapiclient.discovery.build(
1414
api_service_name, api_version, developerKey=api_key)
1515

16-
def get_videos(channel_id, max_results=100):
17-
request = youtube.search().list(
18-
part="snippet",
19-
channelId=channel_id,
20-
maxResults=max_results,
21-
order="date",
22-
type="video"
23-
)
24-
response = request.execute()
25-
16+
def get_videos(channel_id, max_results_per_page=50):
2617
videos = []
27-
for item in response.get("items", []):
28-
video_id = item["id"]["videoId"]
29-
title = item["snippet"]["title"]
30-
videos.append({'id': video_id, 'title': title})
18+
page_token = None # Start with no pageToken
19+
total_fetched = 0 # Keep track of the total number of fetched videos
20+
21+
while total_fetched < 200: # Keep looping until 200 videos have been fetched
22+
request = youtube.search().list(
23+
part="snippet",
24+
channelId=channel_id,
25+
maxResults=max_results_per_page,
26+
order="date",
27+
type="video",
28+
pageToken=page_token # Include the current pageToken
29+
)
30+
31+
response = request.execute()
32+
33+
for item in response.get("items", []):
34+
if total_fetched >= 200:
35+
break # Break out of the loop if 200 videos have been fetched
36+
37+
video_id = item["id"]["videoId"]
38+
title = item["snippet"]["title"]
39+
videos.append({'id': video_id, 'title': title})
40+
total_fetched += 1 # Increment the total_fetched count
3141

42+
page_token = response.get("nextPageToken") # Get the next pageToken
43+
44+
if not page_token:
45+
break # Exit the loop if there are no more pages
46+
3247
return videos
3348

3449
def get_channel_id(channel_name):

0 commit comments

Comments
 (0)