Skip to content

Instantly share code, notes, and snippets.

@aparrish
Created April 21, 2011 22:37
Show Gist options
  • Save aparrish/935631 to your computer and use it in GitHub Desktop.
Save aparrish/935631 to your computer and use it in GitHub Desktop.
page through any facebook graph request
# usage:
# python page_graph.py https://graph.facebook.com/some/path?access_token=x
# ... where some/path is the thing you want to get from the graph api, and 'x' is a
# valid access token (such as can be obtained from http://developers.facebook.com/docs/reference/api/)
# redefine the 'tester' function to change what happens with each data item in the
# response
import json
import urllib
import time
def page_graph(url, callback, maxpages=10):
resp = urllib.urlopen(url)
data = json.loads(resp.read())
for item in data['data']:
callback(item)
if 'paging' in data and 'next' in data['paging'] and maxpages > 1:
time.sleep(0.5)
page_graph(data['paging']['next'], callback, maxpages - 1)
if __name__ == '__main__':
import sys
def tester(item):
if 'message' in item:
print item['message'].encode('ascii', 'replace')
url = sys.argv[1]
page_graph(url, tester)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment