8000 added youtube sentiment · ThinkCode/python-scripts@7a33539 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7a33539

Browse files
committed
added youtube sentiment
1 parent 436f311 commit 7a33539

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

31_youtube_sentiment.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import sys
2+
import requests
3+
from bs4 import BeautifulSoup as bs4
4+
5+
"""
6+
Example usage:
7+
8+
$ python 31_youtube_sentiment.py https://www.youtube.com/watch?v=_vrAjAHhUsA
9+
"""
10+
11+
12+
def get_arguments():
13+
if len(sys.argv) is 2:
14+
return sys.argv[1]
15+
else:
16+
print('Specify at least 1 argument')
17+
sys.exit()
18+
19+
20+
def get_comments(url):
21+
html = requests.get('https://plus.googleapis.com/u/0/_/widget/render/comments?first_party_property=YOUTUBE&href=' + url)
22+
soup = bs4(html.text, 'html.parser')
23+
return [comment.string for comment in soup.findAll('div', class_='Ct')]
24+
25+
26+
def calculate_sentiment(comments):
27+
positive = 0
28+
negative = 0
29+
negative_words = [
30+
'hate', 'hated', 'dislike', 'disliked', 'awful', 'terrible', 'bad',
31+
'painful', 'worst', 'suck', 'rubbish', 'sad', 'sodding'
32+
]
33+
positive_words = [
34+
'love', 'loved', 'like', 'liked', 'awesome', 'amazing', 'good',
35+
'great', 'excellent', 'brilliant', 'cool'
36+
]
37+
for comment in comments:
38+
if comment is None:
39+
continue
40+
else:
41+
for word in comment.split(' '):
42+
if word in negative_words:
43+
negative += 1
44+
if word in positive_words:
45+
positive += 1
46+
return {'positive': positive, 'negative': negative}
47+
48+
49+
def main():
50+
url = get_arguments()
51+
if url:
52+
comments = get_comments(url)
53+
if len(comments) <= 0:
54+
print('This video has no comments.')
55+
sys.exit()
56+
sentiment = calculate_sentiment(comments)
57+
positive_score = sentiment['positive']
58+
negative_score = sentiment['negative']
59+
total_score = positive_score + negative_score
60+
if positive_score > negative_score:
61+
print('This video is generally positive:')
62+
print('{0} positive / {1} total hits'.format(
63+
positive_score, total_score))
64+
elif negative_score > positive_score:
65+
print('This video is generally negative:')
66+
print ('{0} negative / {1} total hits'.format(
67+
negative_score, total_score))
68+
else:
69+
print('This video is mutual:')
70+
print('{0} positive {1} negative'.format(
71+
positive_score, negative_score))
72+
else:
73+
print('No url supplied')
74+
75+
76+
if __name__ == '__main__':
77+
main()

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@
3030
1. **28_income_tax_calculator.py**: Income tax calculator via [Taxee](http://taxee.io/)
3131
1. **29_json_to_yaml.py**: Convert JSON to YAML
3232
1. **30_fullcontact.py**: Call the [FullcContact](https://www.fullcontact.com/developer/) API
33+
1. **31_youtube_sentiment.py**: Calculate sentiment score from the comments of a Youtube video

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
beautifulsoup4==4.4.1
12
PyYAML==3.11
23
requests==2.7.0
34
wheel==0.24.0

0 commit comments

Comments
 (0)
0