-
Notifications
You must be signed in to change notification settings - Fork 0
/
GetComments.py
67 lines (52 loc) · 2.06 KB
/
GetComments.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
import os
import json
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
from dotenv import load_dotenv
load_dotenv()
API_SERVICE_NAME = 'youtube'
API_VERSION = 'v3'
VIDEO_ID = 'x6a4hMyiwBo'
DEVELOPER_KEY = os.getenv("YOUTUBE_API_DEVELOPER_KEY") # replace with your actual key
def get_youtube_service():
return build(API_SERVICE_NAME, API_VERSION, developerKey=DEVELOPER_KEY)
def get_comments(youtube, video_id, num_comments=100):
all_comments = []
next_page_token = None
options = {
'part': 'snippet,replies',
'videoId': video_id,
'maxResults': num_comments,
'textFormat': 'plainText',
'order': 'time'
}
while True:
try:
if next_page_token:
options['pageToken'] = next_page_token
response = youtube.commentThreads().list(**options).execute()
for item in response['items']:
comment = item['snippet']['topLevelComment']['snippet']['textDisplay']
all_comments.append(comment)
if 'replies' in item:
for reply in item['replies']['comments']:
all_comments.append(reply['snippet']['textDisplay'])
next_page_token = response.get('next_page_token')
if not next_page_token:
break
except HttpError as e:
print(f'An error occurred: {e}')
break
return all_comments
def save_comments_to_file(comments, filename):
with open(filename, 'w', encoding='utf-8') as f:
json.dump(comments, f, ensure_ascii=False, indent=4)
def main():
youtube = get_youtube_service()
comments = get_comments(youtube, VIDEO_ID)
save_comments_to_file(comments, f'comments_{VIDEO_ID}.json')
if __name__ == "__main__":
# TODO This python file currently doesn't connect up to the rest of the project. It's just a standalone script.
# In the future, update the rest of the project to connect to this file.
# TODO add Scalene to the project to profile the code
main()