|
| 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() |