|
| 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() |
0 commit comments