TwitterAPI is a Python package for accessing Twitter's REST APIs and Streaming APIs. It supports both Version 1.1 and Version 2 endpoints.
REST APIs that are supported are: Public API, Collections API, Curator API, Ads API, Webhook API, Premium Search API.
By default TwitterAPI will permit only Version 1.1 endpoints. To start using Verson 2 endpoint you must supply the api_version parameter:
api = TwitterAPI(consumer_key, consumer_secret, access_token_key, access_token_secret, api_version='2')
Check the examples folder for more information on making requests using Version 2 endpoints.
From the command line:
pip install TwitterAPI
[See TwitterAPI/examples for working examples.]
First, authenticate with your application credentials:
from TwitterAPI import TwitterAPI api = TwitterAPI(consumer_key, consumer_secret, access_token_key, access_token_secret)
Tweet something:
r = api.request('statuses/update', {'status':'This is a tweet!'})
print(r.status_code)
Get tweet by its id:
r = api.request('statuses/show/:%d' % 210462857140252672)
print(r.text)
Get some tweets:
r = api.request('search/tweets', {'q':'pizza'})
for item in r:
print(item)
Stream tweets from New York City:
r = api.request('statuses/filter', {'locations':'-74,40,-73,41'})
for item in r:
print(item)
request() works with all version 1.1 and version 2 endpoints and the other supported Twitter APIs. Typcally, request() takes two arguments: a Twitter endpoint and a dictionary of endpoint parameters. The above examples use get_iterator() to consume each tweet object. The iterator can iterate results returned from either the REST APIs or the Streaming APIs.
You also have access to the response object returned by request(). With a response object r comes the raw response (r.text) and the HTTP status code (r.status_code). See the requests library documentation for more details.
- An Introduction
- Authentication
- Error Handling
- Paging Results
- Tiny Examples
- Fault Tolerant Streams and Pages
Command-Line Utility (examples/cli)
