Actively maintained, pure Python wrapper for the Twitter API. Supports both normal and streaming Twitter APIs. http://stackoverflow.com/questions/tagged/twython
Find a file
Mike Helmick b484cef7ad Update READMEs
[ci skip]
2013-06-12 16:54:13 -04:00
docs Fix typo, add edited image example 2013-06-12 16:54:04 -04:00
examples Update stream example, update AUTHORS for future example fix 2013-05-29 11:41:30 -04:00
tests Attempting to exclude lines that we can't necessarily hit in tests, added a test, fixed function name 2013-06-12 10:54:28 -04:00
twython Attempting to exclude lines that we can't necessarily hit in tests, added a test, fixed function name 2013-06-12 10:54:28 -04:00
twython-django@3ffebcc57f Version bump 2012-11-10 20:16:34 -05:00
.coveragerc Attempting to exclude lines that we can't necessarily hit in tests, added a test, fixed function name 2013-06-12 10:54:28 -04:00
.gitignore Making the docs pretty (I think?) 2013-06-06 21:12:14 -04:00
.gitmodules Hmmm, lost submodule status for twython-django; not sure why, adding back. 2011-02-26 01:37:22 -05:00
.travis.yml Attempting to omit advisory.py from coverage 2013-06-11 18:57:10 -04:00
AUTHORS.rst Update stream example, update AUTHORS for future example fix 2013-05-29 11:41:30 -04:00
HISTORY.rst Coverage, tests, secure ACCESS_TOKEN, update HISTORY 2013-06-11 17:12:34 -04:00
LICENSE Update Examples and LICENSE 2013-04-18 22:27:50 -04:00
MANIFEST.in Making the docs pretty (I think?) 2013-06-06 21:12:14 -04:00
README.md Update READMEs 2013-06-12 16:54:13 -04:00
README.rst Update READMEs 2013-06-12 16:54:13 -04:00
requirements.txt Remove sphinx themes from requirements, remove Import error 2013-06-11 19:11:52 -04:00
setup.py Update requests requirement, update READMEs, new secure variables, attempting better coverage reports 2013-06-11 14:03:26 -04:00

Twython

Build Status Downloads Coverage Status

Twython is the premier library providing an easy (and up-to-date) way to access Twitter data in Python. Actively maintained and featuring support for Python 2.6+ and Python 3. It's been battle tested by companies, educational institutions and individuals alike. Try it today!

Features

  • Query data for:
    • User information
    • Twitter lists
    • Timelines
    • Direct Messages
    • and anything found in the docs
  • Image Uploading:
    • Update user status with an image
    • Change user avatar
    • Change user background image
    • Change user banner image
  • OAuth 2 Application Only (read-only) Support
  • Support for Twitter's Streaming API
  • Seamless Python 3 support!

Installation

Install Twython via pip

$ pip install twython

or, with [easy_install](<http://pypi.python.org/pypi/setuptools)

$ easy_install twython

But, hey... that's up to you.

Or, if you want the code that is currently on GitHub

git clone git://github.com/ryanmcgrath/twython.git
cd twython
python setup.py install

Starting Out

First, you'll want to head over to https://dev.twitter.com/apps and register an application!

After you register, grab your applications Consumer Key and Consumer Secret from the application details tab.

The most common type of authentication is Twitter user authentication using OAuth 1. If you're a web app planning to have users sign up with their Twitter account and interact with their timelines, updating their status, and stuff like that this is the authentication for you!

First, you'll want to import Twython

from twython import Twython

Authentication

Obtaining Authorization URL

Now, you'll want to create a Twython instance with your Consumer Key and Consumer Secret

APP_KEY = 'YOUR_APP_KEY'
APP_SECET = 'YOUR_APP_SECRET'

twitter = Twython(APP_KEY, APP_SECRET)
auth = twitter.get_authentication_tokens(callback_url='http://mysite.com/callback')
Handling the Callback

After they authorize your application to access some of their account details, they'll be redirected to the callback url you specified in get_autentication_tokens

You'll want to extract the oauth_token and oauth_verifier from the url.

Django example:

OAUTH_TOKEN = request.GET['oauth_token']
oauth_verifier = request.GET['oauth_verifier']

Now that you have the oauth_token and oauth_verifier stored to variables, you'll want to create a new instance of Twython and grab the final user tokens

twitter = Twython(APP_KEY, APP_SECRET,
                  OAUTH_TOKEN, OAUTH_TOKEN_SECRET)

final_step = twitter.get_authorized_tokens(oauth_verifier)

Once you have the final user tokens, store them in a database for later use!

OAUTH_TOKEN = final_step['oauth_token']
OAUTH_TOKEN_SECERT = final_step['oauth_token_secret']

For OAuth 2 (Application Only, read-only) authentication, see our documentation

Dynamic Function Arguments

Keyword arguments to functions are mapped to the functions available for each endpoint in the Twitter API docs. Doing this allows us to be incredibly flexible in querying the Twitter API, so changes to the API aren't held up from you using them by this library.

Basic Usage

Function definitions (i.e. get_home_timeline()) can be found by reading over twython/endpoints.py

Create a Twython instance with your application keys and the users OAuth tokens

from twython import Twython
twitter = Twython(APP_KEY, APP_SECRET
                  OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
Authenticated Users Home Timeline

Documentation: https://dev.twitter.com/docs/api/1.1/get/statuses/home_timeline

twitter.get_home_timeline()
Updating Status

This method makes use of dynamic arguments, read more about them

Documentation: https://dev.twitter.com/docs/api/1/post/statuses/update

twitter.update_status(status='See how easy using Twython is!')
Searching

https://dev.twitter.com/docs/api/1.1/get/search/tweets says it takes "q" and "result_type" amongst other arguments

twitter.search(q='twitter')
twitter.search(q='twitter', result_type='popular')

Advanced Usage

Notes

  • Twython 3.0.0 has been injected with 1000mgs of pure awesomeness! OAuth 2 application authentication is now supported. And a whole lot more! See the CHANGELOG for more details!

Questions, Comments, etc?

My hope is that Twython is so simple that you'd never have to ask any questions, but if you feel the need to contact me for this (or other) reasons, you can hit me up at ryan@venodesigns.net.

Or if I'm to busy to answer, feel free to ping mikeh@ydekproductions.com as well.

Follow us on Twitter:

Want to help?

Twython is useful, but ultimately only as useful as the people using it (say that ten times fast!). If you'd like to help, write example code, contribute patches, document things on the wiki, tweet about it. Your help is always appreciated!