|
13 | 13 | youtube = googleapiclient.discovery.build(
|
14 | 14 | api_service_name, api_version, developerKey=api_key)
|
15 | 15 |
|
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): |
26 | 17 | 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 |
31 | 41 |
|
| 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 | + |
32 | 47 | return videos
|
33 | 48 |
|
34 | 49 | def get_channel_id(channel_name):
|
|
0 commit comments