Update READMEs

[ci skip]
This commit is contained in:
Mike Helmick 2013-06-12 16:54:13 -04:00
parent 57f67b96d3
commit b484cef7ad
2 changed files with 182 additions and 307 deletions

241
README.md
View file

@ -3,212 +3,159 @@ Twython
[![Build Status](https://travis-ci.org/ryanmcgrath/twython.png?branch=master)](https://travis-ci.org/ryanmcgrath/twython) [![Downloads](https://pypip.in/d/twython/badge.png)](https://crate.io/packages/twython/) [![Coverage Status](https://coveralls.io/repos/ryanmcgrath/twython/badge.png?branch=master)](https://coveralls.io/r/ryanmcgrath/twython?branch=master) [![Build Status](https://travis-ci.org/ryanmcgrath/twython.png?branch=master)](https://travis-ci.org/ryanmcgrath/twython) [![Downloads](https://pypip.in/d/twython/badge.png)](https://crate.io/packages/twython/) [![Coverage Status](https://coveralls.io/repos/ryanmcgrath/twython/badge.png?branch=master)](https://coveralls.io/r/ryanmcgrath/twython?branch=master)
```Twython``` is a library providing an easy (and up-to-date) way to access Twitter data in Python. Actively maintained and featuring support for both Python 2.6+ and Python 3, it's been battle tested by companies, educational institutions and individuals alike. Try it today! ```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 Features
-------- --------
* Query data for: - Query data for:
- User information - User information
- Twitter lists - Twitter lists
- Timelines - Timelines
- Direct Messages - Direct Messages
- and anything found in [the docs](https://dev.twitter.com/docs/api/1.1) - and anything found in [the docs](https://dev.twitter.com/docs/api/1.1)
* Image Uploading: - Image Uploading:
- Update user status with an image - Update user status with an image
- Change user avatar - Change user avatar
- Change user background image - Change user background image
- Change user banner image - Change user banner image
* OAuth 2 Application Only (read-only) Support - OAuth 2 Application Only (read-only) Support
* Support for Twitter's Streaming API - Support for Twitter's Streaming API
* Seamless Python 3 support! - Seamless Python 3 support!
Installation Installation
------------ ------------
(pip install | easy_install) twython Install Twython via [pip](http://www.pip-installer.org/)
... or, you can clone the repo and install it the old fashioned way $ pip install twython
or, with [easy_install](<http://pypi.python.org/pypi/setuptools)
$ easy_install twython
But, hey... [that's up to you](http://www.pip-installer.org/en/latest/other-tools.html#pip-compared-to-easy-install).
Or, if you want the code that is currently on GitHub
git clone git://github.com/ryanmcgrath/twython.git git clone git://github.com/ryanmcgrath/twython.git
cd twython cd twython
sudo python setup.py install python setup.py install
Usage Starting Out
----- ------------
##### Authorization URL 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
```python ```python
from twython import Twython from twython import Twython
t = Twython(app_key, app_secret)
auth_props = t.get_authentication_tokens(callback_url='http://google.com')
oauth_token = auth_props['oauth_token']
oauth_token_secret = auth_props['oauth_token_secret']
print 'Connect to Twitter via: %s' % auth_props['auth_url']
``` ```
Be sure you have a URL set up to handle the callback after the user has allowed your app to access their data, the callback can be used for storing their final OAuth Token and OAuth Token Secret in a database for use at a later date. #### Authentication
##### Handling the callback ##### Obtaining Authorization URL
Now, you'll want to create a Twython instance with your `Consumer Key` and `Consumer Secret`
```python ```python
from twython import Twython APP_KEY = 'YOUR_APP_KEY'
APP_SECET = 'YOUR_APP_SECRET'
# oauth_token_secret comes from the previous step twitter = Twython(APP_KEY, APP_SECRET)
# if needed, store that in a session variable or something. auth = twitter.get_authentication_tokens(callback_url='http://mysite.com/callback')
# oauth_verifier and oauth_token from the previous call is now REQUIRED # to pass to get_authorized_tokens
# In Django, to get the oauth_verifier and oauth_token from the callback
# url querystring, you might do something like this:
# oauth_token = request.GET.get('oauth_token')
# oauth_verifier = request.GET.get('oauth_verifier')
t = Twython(app_key, app_secret,
oauth_token, oauth_token_secret)
auth_tokens = t.get_authorized_tokens(oauth_verifier)
print auth_tokens
``` ```
##### 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:
```python
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
```python
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!
```python
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](http://google.com)
#### 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* *Function definitions (i.e. get_home_timeline()) can be found by reading over twython/endpoints.py*
##### Getting a user home timeline Create a Twython instance with your application keys and the users OAuth tokens
```python ```python
from twython import Twython from twython import Twython
twitter = Twython(APP_KEY, APP_SECRET
# oauth_token and oauth_token_secret are the final tokens produced OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
# from the 'Handling the callback' step
t = Twython(app_key, app_secret,
oauth_token, oauth_token_secret)
# Returns an dict of the user home timeline
print t.get_home_timeline()
``` ```
##### Catching exceptions ##### Authenticated Users Home Timeline
> Twython offers three Exceptions currently: TwythonError, TwythonAuthError and TwythonRateLimitError
Documentation: https://dev.twitter.com/docs/api/1.1/get/statuses/home_timeline
```python ```python
from twython import Twython, TwythonAuthError twitter.get_home_timeline()
t = Twython(MY_WRONG_APP_KEY, MY_WRONG_APP_SECRET,
BAD_OAUTH_TOKEN, BAD_OAUTH_TOKEN_SECRET)
try:
t.verify_credentials()
except TwythonAuthError as e:
print e
``` ```
#### Dynamic function arguments ##### Updating Status
> 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.
> https://dev.twitter.com/docs/api/1.1/post/statuses/update says it takes "status" amongst other arguments This method makes use of dynamic arguments, [read more about them](#dynamic-function-arguments)
Documentation: https://dev.twitter.com/docs/api/1/post/statuses/update
```python ```python
from twython import Twython, TwythonAuthError twitter.update_status(status='See how easy using Twython is!')
t = Twython(app_key, app_secret,
oauth_token, oauth_token_secret)
try:
t.update_status(status='Hey guys!')
except TwythonError as e:
print e
``` ```
##### Searching
> https://dev.twitter.com/docs/api/1.1/get/search/tweets says it takes "q" and "result_type" amongst other arguments > https://dev.twitter.com/docs/api/1.1/get/search/tweets says it takes "q" and "result_type" amongst other arguments
```python ```python
from twython import Twython, TwythonAuthError twitter.search(q='twitter')
twitter.search(q='twitter', result_type='popular')
t = Twython(app_key, app_secret,
oauth_token, oauth_token_secret)
try:
t.search(q='Hey guys!')
t.search(q='Hey guys!', result_type='popular')
except TwythonError as e:
print e
``` ```
##### Posting a Status with an Image Advanced Usage
```python --------------
from twython import Twython
t = Twython(app_key, app_secret, - [Advanced Twython Usage](http://google.com)
oauth_token, oauth_token_secret) - [Streaming with Twython](http://google.com)
# The file key that Twitter expects for updating a status with an image
# is 'media', so 'media' will be apart of the params dict.
# You can pass any object that has a read() function (like a StringIO object)
# In case you wanted to resize it first or something!
photo = open('/path/to/file/image.jpg', 'rb')
t.update_status_with_media(media=photo, status='Check out my image!')
```
##### Posting a Status with an Editing Image *(This example resizes an image)*
```python
from twython import Twython
t = Twython(app_key, app_secret,
oauth_token, oauth_token_secret)
# Like said in the previous section, you can pass any object that has a read() method
# Assume you are working with a JPEG
from PIL import Image
from StringIO import StringIO
photo = Image.open('/path/to/file/image.jpg')
basewidth = 320
wpercent = (basewidth / float(photo.size[0]))
height = int((float(photo.size[1]) * float(wpercent)))
photo = photo.resize((basewidth, height), Image.ANTIALIAS)
image_io = StringIO.StringIO()
photo.save(image_io, format='JPEG')
# If you do not seek(0), the image will be at the end of the file and
# unable to be read
image_io.seek(0)
t.update_status_with_media(media=photo, status='Check out my edited image!')
```
##### Streaming API
```python
from twython import TwythonStreamer
class MyStreamer(TwythonStreamer):
def on_success(self, data):
print data
def on_error(self, status_code, data):
print status_code, data
# Requires Authentication as of Twitter API v1.1
stream = MyStreamer(APP_KEY, APP_SECRET,
OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
stream.statuses.filter(track='twitter')
```
Notes 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](https://github.com/ryanmcgrath/twython/blob/master/HISTORY.rst#300-2013-xx-xx>) for more details! * 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](https://github.com/ryanmcgrath/twython/blob/master/HISTORY.rst#300-2013-xx-xx) for more details!
Questions, Comments, etc? Questions, Comments, etc?
------------------------- -------------------------

View file

@ -9,230 +9,158 @@ Twython
.. image:: https://coveralls.io/repos/ryanmcgrath/twython/badge.png?branch=master .. image:: https://coveralls.io/repos/ryanmcgrath/twython/badge.png?branch=master
:target: https://coveralls.io/r/ryanmcgrath/twython?branch=master :target: https://coveralls.io/r/ryanmcgrath/twython?branch=master
``Twython`` is a library providing an easy (and up-to-date) way to access Twitter data in Python. Actively maintained and featuring support for both Python 2.6+ and Python 3, it's been battle tested by companies, educational institutions and individuals alike. Try it today! ``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 Features
-------- --------
* Query data for: - Query data for:
- User information - User information
- Twitter lists - Twitter lists
- Timelines - Timelines
- Direct Messages - Direct Messages
- and anything found in `the docs <https://dev.twitter.com/docs/api/1.1>`_ - and anything found in `the docs <https://dev.twitter.com/docs/api/1.1>`_
* Image Uploading: - Image Uploading:
- Update user status with an image - Update user status with an image
- Change user avatar - Change user avatar
- Change user background image - Change user background image
- Change user banner image - Change user banner image
* OAuth 2 Application Only (read-only) Support - OAuth 2 Application Only (read-only) Support
* Support for Twitter's Streaming API - Support for Twitter's Streaming API
* Seamless Python 3 support! - Seamless Python 3 support!
Installation Installation
------------ ------------
::
pip install twython Install Twython via `pip <http://www.pip-installer.org/>`_::
or, you can clone the repo and install it the old fashioned way $ pip install twython
:: or, with `easy_install <http://pypi.python.org/pypi/setuptools>`_::
$ easy_install twython
But, hey... `that's up to you <http://www.pip-installer.org/en/latest/other-tools.html#pip-compared-to-easy-install>`_.
Or, if you want the code that is currently on GitHub::
git clone git://github.com/ryanmcgrath/twython.git git clone git://github.com/ryanmcgrath/twython.git
cd twython cd twython
sudo python setup.py install python setup.py install
Starting Out
------------
Usage First, you'll want to head over to https://dev.twitter.com/apps and register an application!
-----
Authorization URL 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 from twython import Twython
t = Twython(app_key, app_secret) Authentication
~~~~~~~~~~~~~~
auth_props = t.get_authentication_tokens(callback_url='http://google.com') Obtain Authorization URL
^^^^^^^^^^^^^^^^^^^^^^^^
oauth_token = auth_props['oauth_token'] Now, you'll want to create a Twython instance with your ``Consumer Key`` and ``Consumer Secret``
oauth_token_secret = auth_props['oauth_token_secret']
print 'Connect to Twitter via: %s' % auth_props['auth_url']
Be sure you have a URL set up to handle the callback after the user has allowed your app to access their data, the callback can be used for storing their final OAuth Token and OAuth Token Secret in a database for use at a later date.
Handling the callback
~~~~~~~~~~~~~~~~~~~~~
::
from twython import Twython
# oauth_token_secret comes from the previous step
# if needed, store that in a session variable or something.
# oauth_verifier and oauth_token from the previous call is now REQUIRED # to pass to get_authorized_tokens
# In Django, to get the oauth_verifier and oauth_token from the callback
# url querystring, you might do something like this:
# oauth_token = request.GET.get('oauth_token')
# oauth_verifier = request.GET.get('oauth_verifier')
t = Twython(app_key, app_secret,
oauth_token, oauth_token_secret)
auth_tokens = t.get_authorized_tokens(oauth_verifier)
print auth_tokens
*Function definitions (i.e. get_home_timeline()) can be found by reading over twython/endpoints.py*
Getting a user home timeline
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
::
from twython import Twython
# oauth_token and oauth_token_secret are the final tokens produced
# from the 'Handling the callback' step
t = Twython(app_key, app_secret,
oauth_token, oauth_token_secret)
# Returns an dict of the user home timeline
print t.get_home_timeline()
Catching exceptions
~~~~~~~~~~~~~~~~~~~
Twython offers three Exceptions currently: ``TwythonError``, ``TwythonAuthError`` and ``TwythonRateLimitError``
:: ::
from twython import Twython, TwythonAuthError APP_KEY = 'YOUR_APP_KEY'
APP_SECET = 'YOUR_APP_SECRET'
t = Twython(MY_WRONG_APP_KEY, MY_WRONG_APP_SECRET, twitter = Twython(APP_KEY, APP_SECRET)
BAD_OAUTH_TOKEN, BAD_OAUTH_TOKEN_SECRET) auth = twitter.get_authentication_tokens(callback_url='http://mysite.com/callback')
try: Handling the Callback
t.verify_credentials() ^^^^^^^^^^^^^^^^^^^^^
except TwythonAuthError as e:
print e
Dynamic function arguments 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 <http://google.com>`_
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. 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.
https://dev.twitter.com/docs/api/1.1/post/statuses/update says it takes "status" amongst other arguments Basic Usage
-----------
:: **Function definitions (i.e. get_home_timeline()) can be found by reading over twython/endpoints.py**
from twython import Twython, TwythonAuthError Create a Twython instance with your application keys and the users OAuth tokens::
t = Twython(app_key, app_secret,
oauth_token, oauth_token_secret)
try:
t.update_status(status='Hey guys!')
except TwythonError as e:
print e
::
# https://dev.twitter.com/docs/api/1.1/get/search/tweets says it takes "q" and "result_type" amongst other arguments
from twython import Twython, TwythonAuthError
t = Twython(app_key, app_secret,
oauth_token, oauth_token_secret)
try:
t.search(q='Hey guys!')
t.search(q='Hey guys!', result_type='popular')
except TwythonError as e:
print e
Posting a Status with an Image
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
::
from twython import Twython from twython import Twython
twitter = Twython(APP_KEY, APP_SECRET
OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
t = Twython(app_key, app_secret, Authenticated Users Home Timeline
oauth_token, oauth_token_secret) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# The file key that Twitter expects for updating a status with an image Documentation: https://dev.twitter.com/docs/api/1.1/get/statuses/home_timeline
# is 'media', so 'media' will be apart of the params dict.
# You can pass any object that has a read() function (like a StringIO object)
# In case you wanted to resize it first or something!
photo = open('/path/to/file/image.jpg', 'rb')
t.update_status_with_media(media=photo, status='Check out my image!')
Posting a Status with an Editing Image *(This example resizes an image)*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
:: ::
from twython import Twython twitter.get_home_timeline()
t = Twython(app_key, app_secret, Updating Status
oauth_token, oauth_token_secret) ~~~~~~~~~~~~~~~
# Like I said in the previous section, you can pass any object that has a This method makes use of dynamic arguments, `read more about them <http://google.com>`_
# read() method
# Assume you are working with a JPEG Documentation: https://dev.twitter.com/docs/api/1/post/statuses/update
from PIL import Image
from StringIO import StringIO
photo = Image.open('/path/to/file/image.jpg')
basewidth = 320
wpercent = (basewidth / float(photo.size[0]))
height = int((float(photo.size[1]) * float(wpercent)))
photo = photo.resize((basewidth, height), Image.ANTIALIAS)
image_io = StringIO.StringIO()
photo.save(image_io, format='JPEG')
# If you do not seek(0), the image will be at the end of the file and
# unable to be read
image_io.seek(0)
t.update_status_with_media(media=photo, status='Check out my edited image!')
Streaming API
~~~~~~~~~~~~~
:: ::
from twython import TwythonStreamer twitter.update_status(status='See how easy using Twython is!')
Searching
~~~~~~~~~
class MyStreamer(TwythonStreamer): https://dev.twitter.com/docs/api/1.1/get/search/tweets says it takes "q" and "result_type" amongst other arguments
def on_success(self, data):
print data
def on_error(self, status_code, data): ::
print status_code, data
# Requires Authentication as of Twitter API v1.1 twitter.search(q='twitter')
stream = MyStreamer(APP_KEY, APP_SECRET, twitter.search(q='twitter', result_type='popular')
OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
stream.statuses.filter(track='twitter') Advanced Usage
--------------
- `Advanced Twython Usage <http://google.com>`_
- `Streaming with Twython <http://google.com>`_
Notes 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<https://github.com/ryanmcgrath/twython/blob/master/HISTORY.rst#300-2013-xx-xx>`_ for more details! - 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 <https://github.com/ryanmcgrath/twython/blob/master/HISTORY.rst#300-2013-xx-xx>`_ for more details!
Questions, Comments, etc? Questions, Comments, etc?
------------------------- -------------------------