-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyt_comments.py
More file actions
39 lines (32 loc) · 1.48 KB
/
yt_comments.py
File metadata and controls
39 lines (32 loc) · 1.48 KB
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
37
38
39
# Import the required library
from googleapiclient.discovery import build
# Define a function to get comments from a specific video
def get_comments(youtube, video_id, username, comments=[], token=''):
# Make a request to the YouTube API to get the comment threads of the video
video_response = youtube.commentThreads().list(
part='snippet',
videoId=video_id,
pageToken=token
).execute()
# Iterate over each comment thread
for item in video_response['items']:
# Get the comment and the author's display name
comment = item['snippet']['topLevelComment']['snippet']
# If the author's display name matches the specified username, add the comment to the list
if comment['authorDisplayName'] == username:
comments.append(comment['textDisplay'])
# If there are more comments, make a recursive call to get the next page of comments
if 'nextPageToken' in video_response:
return get_comments(youtube, video_id, username, comments, video_response['nextPageToken'])
else:
return comments
# add your api key here
api_key = ''
# Get the video ID and username from the user
video_id = input('Enter the video ID: ')
username = input('Enter the username: ')
# Build the YouTube service
youtube = build('youtube', 'v3', developerKey=api_key)
# Call the get_comments function and print the comments
comments = get_comments(youtube, video_id, username)
print(comments)