-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyoutube.py
More file actions
75 lines (58 loc) · 2.84 KB
/
youtube.py
File metadata and controls
75 lines (58 loc) · 2.84 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# Import necessary libraries
import os
import googleapiclient.discovery
import googleapiclient.errors
from dotenv import load_dotenv # To load environment variables from a .env file
# Load environment variables (e.g., API_KEY) from a .env file
load_dotenv()
# Get the YouTube API key from environment variables
api_key = os.getenv("API_KEY")
def get_comments(youtube, **kwargs):
"""
This function retrieves comments from a YouTube video using the YouTube API.
Arguments:
youtube -- the YouTube API client
kwargs -- keyword arguments that are passed to the API request (e.g., videoId, part, etc.)
Returns:
A list of comments from the video.
"""
comments = [] # List to store comments
results = youtube.commentThreads().list(**kwargs).execute() # API request to get comments
while results: # Loop through the pages of comments
for item in results['items']: # Iterate through each comment
# Extract the text of the top-level comment
comment = item['snippet']['topLevelComment']['snippet']['textDisplay']
comments.append(comment) # Append the comment to the list
# Check if there are more comments (i.e., next page of results)
if 'nextPageToken' in results:
kwargs['pageToken'] = results['nextPageToken'] # Add the next page token to the request
results = youtube.commentThreads().list(**kwargs).execute() # Fetch the next page of comments
else:
break # No more comments to fetch, exit the loop
return comments # Return the list of comments
def main(video_id, api_key):
"""
This function sets up the YouTube API client and calls the get_comments function to retrieve comments.
Arguments:
video_id -- The YouTube video ID to retrieve comments from
api_key -- The YouTube API key for authentication
Returns:
A list of comments for the given video.
"""
# Disable OAuthlib's HTTPS verification when running locally (useful for local development)
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
# Build the YouTube API client with the provided API key
youtube = googleapiclient.discovery.build(
"youtube", "v3", developerKey=api_key)
# Call get_comments to fetch the comments from the video
comments = get_comments(youtube, part="snippet", videoId=video_id, textFormat="plainText")
return comments # Return the fetched comments
def get_video_comments(video_id):
"""
This function acts as an entry point to retrieve video comments by calling the main function.
Arguments:
video_id -- The YouTube video ID to retrieve comments from
Returns:
A list of comments for the given video.
"""
return main(video_id, api_key) # Call the main function to get the comments