Skip to content

Instantly share code, notes, and snippets.

@katz
Forked from my-slab/get_posts.py
Last active August 29, 2015 14:10
Show Gist options
  • Save katz/f89e230dfdd901e8d1b4 to your computer and use it in GitHub Desktop.
Save katz/f89e230dfdd901e8d1b4 to your computer and use it in GitHub Desktop.
import facebook
import requests
def some_action(post):
""" Here you might want to do something with each post. E.g. grab the
post's message (post['message']) or the post's picture (post['picture']).
In this implementation we just print the post's created time.
"""
print post['created_time']
# You'll need an access token here to do anything. You can get a temporary one
# here: https://developers.facebook.com/tools/explorer/
access_token = ''
# Look at Bill Gates's profile for this example by using his Facebook id.
user = 'BillGates'
graph = facebook.GraphAPI(access_token)
profile = graph.get_object(user)
posts = graph.get_connections(profile['id'], 'posts')
# Wrap this block in a while loop so we can keep paginating requests until
# finished.
while True:
try:
# Perform some action on each post in the collection we receive from
# Facebook.
[some_action(post=post) for post in posts['data']]
# Attempt to make a request to the next page of data, if it exists.
posts = requests.get(posts['paging']['next']).json()
except KeyError:
# When there are no more pages (['paging']['next']), break from the
# loop and end the script.
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment