-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathsimple_facebook_sentiment_analysis.py
executable file
·65 lines (44 loc) · 1.76 KB
/
simple_facebook_sentiment_analysis.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
#!/usr/bin/env python
##########################################
#
# simple_facebook_sentiment_analysis.py: Basic script to retrieve and perform Sentiment Analysis on Facebook Posts.
#
# Author: Cosimo Iaia <[email protected]>
# Date: 5/11/2016
#
# This file is distribuited under the terms of GNU General Public
#
#########################################
import facebook as fb
import requests
import argparse
import textblob as tb
FLAGS = None
def sentiment_analysis(post):
# Here's where the magic happens
tb_msg = tb(post['message'])
score = tb_msg.sentiment
print("Date: %s, From: %s\n", post['created_time'], post['from'])
print("%s\nShared: %s, Score: %f", post['message'], post['share'], score)
def connect(access_token, user):
graph = fb.GraphAPI(access_token)
profile = graph.get_object(user)
return graph, profile
def main():
access_token = FLAGS.access_token
user = FLAGS.profile
graph, profile = connect(access_token, user)
posts = graph.get_connections(profile['id'], 'posts')
#Let's grab all the posts and analyze them!
while True:
try:
[sentiment_analysis(post=post) for post in posts['data']]
posts= requests.get(posts['paging']['next']).json()
except KeyError:
break
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Simple Facebook Sentiment Analysis Script')
parser.add_argument('--access_token', type=str, required=True, default='', help='Your Facebook API Access Token: https://developers.facebook.com/docs/graph-api/overview')
parser.add_argument('--profile', type=str, required=True, default='', help='The profile name to retrieve the posts from')
FLAGS = parser.parse_args()
main()