Skip to main content

Minimal wrapper for Twitter's REST and Streaming APIs

Project description

BADGE_DOWNLOADS BADGE_VERSION Join the chat at https://gitter.im/geduldig/TwitterAPI

TwitterAPI is a Python package for accessing Twitter’s REST API and Streaming API. It supports OAuth 1.0 and OAuth 2.0 authentication. And, it works with the latest Python versions in both 2.x and 3.x branches.

Installation

From the command line:

pip install TwitterAPI

Documentation

Some Code…

[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)

Notice that request() works with both REST and Streaming API endpoints. Usually 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 knows how to iterate both REST and Streaming API results.

You also have access to the response object returned by request(). From a response object r you can get the raw response with r.text and the HTTP status code with r.status_code. See the requests library documentation for more details.

Command-Line Utility (examples/cli.py)

For syntax help:

python cli.py -h

You will need to supply your Twitter application OAuth credentials. The easiest option is to save them in TwitterAPI/credentials.txt. It is the default place where cli.py will look for them. You also may supply an alternative credentials file as a command-line argument.

Call any REST API endpoint:

python cli.py -endpoint statuses/update -parameters status='my tweet'

Another example (here using abbreviated option names) that parses selected output fields:

python cli.py -e search/tweets -p q=zzz count=10 -field screen_name text

Calling any Streaming API endpoint works too:

python cli.py -e statuses/filter -p track=zzz -f screen_name text

After the -field option you must supply one or more key names from the raw JSON response object. This will print values only for these keys. When the -field option is omitted cli.py prints the entire JSON response object.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

TwitterAPI-2.3.4.tar.gz (8.5 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page