-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomment_extractor.py
More file actions
77 lines (51 loc) · 1.91 KB
/
comment_extractor.py
File metadata and controls
77 lines (51 loc) · 1.91 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
76
77
import os
import csv
from dotenv import load_dotenv
load_dotenv()
from googleapiclient.discovery import build
apiKey = os.getenv("API_KEY")
youtube = build("youtube", "v3", developerKey=apiKey)
def comment_threads(channelID):
comments_list = []
request = youtube.commentThreads().list(
part="id,replies,snippet",
videoId=channelID,
)
response = request.execute()
comments_list.extend(process_comments(response["items"]))
while response.get("nextPageToken", None):
request = youtube.commentThreads().list(
part="id,replies,snippet",
videoId=channelID,
pageToken=response["nextPageToken"],
)
response = request.execute()
comments_list.extend(process_comments(response["items"]))
return comments_list
comments = []
def process_comments(response_items):
for res in response_items:
if "replies" in res.keys():
for reply in res["replies"]["comments"]:
comment = reply["snippet"]
comment["commentId"] = reply["id"]
comments.append(comment)
else:
comment = {}
comment["snippet"] = res["snippet"]["topLevelComment"]["snippet"]
comment["snippet"]["parentId"] = None
comment["snippet"]["commentId"] = res["snippet"]["topLevelComment"]["id"]
comments.append(comment["snippet"])
make_csv(comments)
return comments
def make_csv(comment):
header = comments[0].keys()
filename = f"comments_dataset.csv"
with open(filename, "w", encoding="utf8", newline="") as f:
writer = csv.DictWriter(f, fieldnames=header, extrasaction="ignore")
writer.writeheader()
writer.writerows(comments)
def main():
comment_threads("kX3nB4PpJko")
if (__name__) == "__main__":
main()