-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_youtube_video_list.py
37 lines (33 loc) · 1.24 KB
/
get_youtube_video_list.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import os
import google.auth
from googleapiclient.discovery import build
import json
def get_video_list(channel_id, api_key, output_file):
print("fetching latest videos")
youtube = build('youtube', 'v3', developerKey=api_key)
next_page_token = None
total_results = []
while True:
playlist_items = youtube.playlistItems().list(
part='snippet',
maxResults=50,
playlistId=channel_id,
pageToken = next_page_token
).execute()
filtered_items = []
for item in playlist_items.get('items', []):
snippet = item.get('snippet', {})
resourceId = snippet.get('resourceId', {})
filtered_item = {
'title': snippet.get('title'),
'publishedAt': snippet.get('publishedAt'),
'videoId': resourceId.get('videoId'),
}
filtered_items.append(filtered_item)
total_results.extend(filtered_items)
next_page_token = playlist_items.get('nextPageToken')
if not next_page_token:
break
with open(output_file, 'w') as json_file:
json.dump(total_results, json_file, indent=2)
print(f'video list has been downloaded to {output_file}')