-
Notifications
You must be signed in to change notification settings - Fork 1
/
facebook-graph-api-page.py
49 lines (38 loc) · 1.6 KB
/
facebook-graph-api-page.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
import urllib2
import json
#Create and write the return into a json.
def write_json(file_name, data):
with open(file_name, 'w') as outfile:
json.dump(data, outfile)
#Print the json return on terminal.
def print_json(page_data):
print json.dumps(page_data, indent=4, sort_keys=True)
#Get the info about a page or profile
def get_page_data(page_id,access_token,info,page_or_profile):
api_endpoint = "https://graph.facebook.com/v2.6/"
if page_or_profile == 1:
fb_graph_url = api_endpoint+page_id+"/insights"+info+"?access_token="+access_token
else:
fb_graph_url = api_endpoint+page_id+"?fields="+info+"&access_token="+access_token
try:
print fb_graph_url
api_request = urllib2.Request(fb_graph_url)
api_response = urllib2.urlopen(api_request)
try:
return json.loads(api_response.read())
except (ValueError, KeyError, TypeError):
return "JSON error"
except IOError, e:
if hasattr(e, 'code'):
return e.code
elif hasattr(e, 'reason'):
return e.reason
page = 1;
profile = 2;
profile_info = "id,name,likes,link"
page_user_demographics = "/page_fans,page_fans_locale,page_fans_city,page_fans_country,page_fans_gender_age,page_fan_adds,page_fan_adds_unique,page_fans_by_like_source,page_fans_by_like_source_unique,page_fan_removes,page_fan_removes_unique,page_fans_by_unlike_source_unique"
page_id = "facebook" # Inser the page id or name
token = "YOUR_TOKEN" # Access Token
page_data = get_page_data(page_id,token,profile_info,profile)
print_json(page_data)
write_json("facebook2.json", page_data)