-
Notifications
You must be signed in to change notification settings - Fork 21
/
api_utils.py
61 lines (40 loc) · 1.23 KB
/
api_utils.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
import requests
import facebook
#authentication
access_token='XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
graph = facebook.GraphAPI(access_token=access_token, version='2.5')
def get_all_pages(posts):
allposts=[]
allposts = posts['data']
while(1):
try:
next_page_url=posts['paging']['next'] #get url for next page
except KeyError:
break
posts = requests.get(next_page_url).json()
allposts += posts['data']
return allposts
def get_post_likes(post):
mylikes=[]
try:
likes=graph.get_connections(post['id'],"likes")
mylikes = get_all_pages(likes)
except:
pass
return mylikes
def get_liked_pages():
mypages=[]
pages = graph.get_connections('me',connection_name='likes')
mypages = get_all_pages(pages)
return mypages
def get_user_posts():
myposts=[]
#get my posts on page 1
posts = graph.get_connections('me',connection_name='posts')
#visit all pages
myposts = get_all_pages(posts)
return myposts
def get_user_details():
#graph api does not allow to access all fields at once...so you have to list the fields you require!
user = graph.get_object('me',fields='name,id,email,education,friends,age_range,birthday')
return user