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+
importsys
2+
importrequests
3+
frombs4importBeautifulSoupasbs4
4+
5+
"""
6+
Example usage:
7+
8+
$ python 31_youtube_sentiment.py https://www.youtube.com/watch?v=_vrAjAHhUsA
9+
"""
10+
11+
12+
defget_arguments():
13+
iflen(sys.argv) is2:
14+
returnsys.argv[1]
15+
else:
16+
print('Specify at least 1 argument')
17+
sys.exit()
18+
19+
20+
defget_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.stringforcommentinsoup.findAll('div', class_='Ct')]
24+
25+
26+
defcalculate_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+
forcommentincomments:
38+
ifcommentisNone:
39+
continue
40+
else:
41+
forwordincomment.split(' '):
42+
ifwordinnegative_words:
43+
negative+=1
44+
ifwordinpositive_words:
45+
positive+=1
46+
return{'positive': positive, 'negative': negative}
47+
48+
49+
defmain():
50+
url=get_arguments()
51+
ifurl:
52+
comments=get_comments(url)
53+
iflen(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+
ifpositive_score>negative_score:
61+
print('This video is generally positive:')
62+
print('{0} positive /{1} total hits'.format(
63+
positive_score, total_score))
64+
elifnegative_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)