-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path31_youtube_sentiment.py
97 lines (85 loc) · 2.44 KB
/
31_youtube_sentiment.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import sys
import requests
from bs4 import BeautifulSoup as bs4
"""
Example usage:
$ python 31_youtube_sentiment.py https://www.youtube.com/watch?v=_vrAjAHhUsA
"""
def get_arguments():
if len(sys.argv) is 2:
return sys.argv[1]
else:
print("Specify at least 1 argument")
sys.exit()
def get_comments(url):
html = requests.get(
"https://plus.googleapis.com/u/0/_/widget/render/comments?first_party_property=YOUTUBE&href="
+ url
)
soup = bs4(html.text, "html.parser")
return [comment.string for comment in soup.findAll("div", class_="Ct")]
def calculate_sentiment(comments):
positive = 0
negative = 0
negative_words = [
"hate",
"hated",
"dislike",
"disliked",
"awful",
"terrible",
"bad",
"painful",
"worst",
"suck",
"rubbish",
"sad",
"sodding",
]
positive_words = [
"love",
"loved",
"like",
"liked",
"awesome",
"amazing",
"good",
"great",
"excellent",
"brilliant",
"cool",
]
for comment in comments:
if comment is None:
continue
else:
for word in comment.split(" "):
if word in negative_words:
negative += 1
if word in positive_words:
positive += 1
return {"positive": positive, "negative": negative}
def main():
url = get_arguments()
if url:
comments = get_comments(url)
if len(comments) <= 0:
print("This video has no comments.")
sys.exit()
sentiment = calculate_sentiment(comments)
positive_score = sentiment["positive"]
negative_score = sentiment["negative"]
total_score = positive_score + negative_score
if positive_score > negative_score:
print("This video is generally positive:")
print("{0} positive / {1} total hits".format(positive_score, total_score))
elif negative_score > positive_score:
print("This video is generally negative:")
print("{0} negative / {1} total hits".format(negative_score, total_score))
else:
print("This video is mutual:")
print("{0} positive {1} negative".format(positive_score, negative_score))
else:
print("No url supplied")
if __name__ == "__main__":
main()