fbiter
fbiter
is a simple Python library for iterating through paginated Facebook API endpoints.
Installation
pip install fbiter
Usage
import fbiter
import os
endpoint = fbiter.Endpoint("v2.10/BuzzFeedNews/posts", params={
"access_token": os.environ["FB_ACCESS_TOKEN"],
"limit": 10
})
print(endpoint.get(max_results=15))
fbiter.Endpoint
This is fbiter
's core class. It takes two parameters:
path
: The API endpoint path, including the API version number, but excludinghttps://graph.facebook.com/
.params
: Adict
containing the relevant API params, the only required one of which isaccess_token
.
fbiter.Endpoint
also has the following methods:
.iter_results(max_results=None)
: Returns a Python iterator object that returns one result item at a time, up tomax_results
result items..get(max_results=None)
: Returns all result items as a list, after paginating through each available page, up tomax_results
result items.
Errors
The library tries to handle HTTP errors in the following ways:
HTTP 400
andHTTP 404
: Returns{ "data": [] }
- All other errors: Sleeps 10 seconds and then tries again.