-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathYoutube_Project
47 lines (34 loc) · 1.24 KB
/
Youtube_Project
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
import os
from dotenv import load_dotenv
from googleapiclient.discovery import build
from utils.comments import process_comments, make_csv
load_dotenv()
API_KEY = os.getenv("API_KEY")
youtube = build("youtube", "v3", developerKey = API_KEY)
def comment_threads(channelID, to_csv=False):
comments_list = []
request = youtube.commentThreads().list(
part='id,replies,snippet',
videoId=channelID,
)
response = request.execute()
#extend instead of append as it causes a nested list and makes it hard to find the 0th element
comments_list.extend(process_comments(response['items']))
i=30
while response.get('nextPageToken', None) and i>0:
request = youtube.commentThreads().list(
part='id,replies,snippet',
videoId=channelID,
pageToken=response['nextPageToken'],
)
i= i-1
response = request.execute()
comments_list.extend(process_comments(response['items']))
print(f'Finished fetching comments for {channelID}. {len(comments_list)} comments found.')
if to_csv:
make_csv(comments_list, channelID)
return comments_list
def main():
comment_threads("kxOuG8jMIgI", to_csv=True)
if __name__ == "__main__":
main()