-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
77 lines (58 loc) · 2.31 KB
/
run.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
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
import os, re
import google.auth
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
import openai
from dotenv import load_dotenv
# set the environment variable
dotenv_path = os.environ.get("PRIVATE_API_KEY_PATH")
load_dotenv(dotenv_path)
openai.api_key = os.environ.get('OPENAI_API_KEY')
# authenticate the client
credentials, project = google.auth.default(scopes=['https://www.googleapis.com/auth/youtube.force-ssl'])
youtube = build('youtube', 'v3', credentials=credentials)
# prompt user for the video URL
video_url = input("Enter the YouTube video URL: ")
# video_url = 'https://www.youtube.com/watch?v=QUYODQB_2wQ&lc=UgyiYJzk2XMGjuIjsSx4AaABAg.9mYftIxYoRS9mgn2hI2WRg'
# extract video ID from the URL
video_id = re.findall(r"v=([-\w]{11})", video_url)
if not video_id:
print("Invalid video URL.")
exit()
video_id = video_id[0]
# function to get comments from a video
def get_comments(video_url, video_id, next_page_token='', comments=[]):
try:
if video_id:
video_id = video_id.strip()
response = youtube.commentThreads().list(
part='snippet',
videoId=video_id,
pageToken=next_page_token,
textFormat='plainText'
).execute()
else:
response = youtube.commentThreads().list(
part='snippet',
videoUrl=video_url,
pageToken=next_page_token,
textFormat='plainText'
).execute()
for item in response['items']:
comment = item['snippet']['topLevelComment']['snippet']['textDisplay']
comments.append(comment)
if item['snippet']['totalReplyCount'] > 0:
reply_comments = get_comments(video_url, video_id, item['id'], [])
comments += reply_comments
if 'nextPageToken' in response:
get_comments(video_url, video_id, response['nextPageToken'], comments)
return comments
except HttpError as error:
print(f"An error occurred: {error}")
return comments
# get comments from video
comments = get_comments(video_url, video_id)
# write comments to file
with open(f'{video_id}-comments.txt', 'w', encoding='utf-8') as file:
for comment in comments:
file.write(comment + '\n')