Making twython work (again?) in Python 3

- Added a ``HISTORY.rst`` to start tracking history of changes
- Updated ``twitter_endpoints.py`` to ``endpoints.py`` for cleanliness
- Removed twython3k directory, no longer needed
- Added ``compat.py`` for compatability with Python 2.6 and greater
- Added some ascii art, moved description of Twython and ``__author__`` to ``__init__.py``
- Added ``version.py`` to store the current Twython version, instead of repeating it twice -- it also had to go into it's own file because of dependencies of ``requests`` and ``requests-oauthlib``, install would fail because those libraries weren't installed yet (on fresh install of Twython)
- Removed ``find_packages()`` from ``setup.py``, only one package -- we can
just define it
- added quick publish method for Ryan and I: ``python setup.py publish`` is faster to type and easier to remember than ``python setup.py sdist upload``
- Removed ``base_url`` from ``endpoints.py`` because we're just repeating it in
``Twython.__init__``
- ``Twython.get_authentication_tokens()`` now takes ``callback_url`` argument rather than passing the ``callback_url`` through ``Twython.__init__``, ``callback_url`` is only used in the ``get_authentication_tokens`` method and nowhere else (kept in init though for backwards compatability)
- Updated README to better reflect current Twython codebase
- Added ``warnings.simplefilter('default')`` line in ``twython.py`` for Python 2.7 and greater to display Deprecation Warnings in console
- Added Deprecation Warnings for usage of ``twitter_token``, ``twitter_secret`` and ``callback_url`` in ``Twython.__init__``
- Headers now always include the User-Agent as Twython vXX unless User-Agent is overwritten
- Removed senseless TwythonError thrown if method is not GET or POST, who cares -- if the user passes something other than GET or POST just let Twitter return the error that they messed up
- Removed conversion to unicode of (int, bool) params passed to a requests. ``requests`` isn't greedy about variables that can't be converted to unicode anymore
This commit is contained in:
Mike Helmick 2013-04-17 18:59:11 -04:00
parent 8ecc55b5ad
commit bb019d3a57
16 changed files with 306 additions and 1000 deletions

View file

@ -9,12 +9,13 @@ Features
- User information
- Twitter lists
- Timelines
- User avatar URL
- Direct Messages
- and anything found in `the docs <https://dev.twitter.com/docs/api/1.1>`_
* Image Uploading!
- **Update user status with an image**
- Change user avatar
- Change user background image
- Change user banner image
Installation
------------
@ -37,13 +38,12 @@ Usage
Authorization URL
~~~~~~~~~~~~~~~~~
::
from twython import Twython
t = Twython(app_key=app_key,
app_secret=app_secret,
callback_url='http://google.com/')
auth_props = t.get_authentication_tokens()
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']
@ -56,41 +56,59 @@ Handling the callback
~~~~~~~~~~~~~~~~~~~~~
::
'''
oauth_token and oauth_token_secret come from the previous step
if needed, store those in a session variable or something. oauth_verifier from the previous call is now required to pass to get_authorized_tokens
'''
from twython import Twython
t = Twython(app_key=app_key,
app_secret=app_secret,
oauth_token=oauth_token,
oauth_token_secret=oauth_token_secret)
# 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. getHomeTimeline()) can be found by reading over twython/twitter_endpoints.py*
*Function definitions (i.e. getHomeTimeline()) can be found by reading over twython/endpoints.py*
Getting a user home timeline
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
::
'''
oauth_token and oauth_token_secret are the final tokens produced
from the `Handling the callback` step
'''
from twython import Twython
t = Twython(app_key=app_key,
app_secret=app_secret,
oauth_token=oauth_token,
oauth_token_secret=oauth_token_secret)
# 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.getHomeTimeline()
Catching exceptions
~~~~~~~~~~~~~~~~~~~
Twython offers three Exceptions currently: ``TwythonError``, ``TwythonAuthError`` and ``TwythonRateLimitError``
::
from twython import Twython, TwythonAuthError
t = Twython(MY_WRONG_APP_KEY, MY_WRONG_APP_SECRET,
BAD_OAUTH_TOKEN, BAD_OAUTH_TOKEN_SECRET)
try:
t.verifyCredentials()
except TwythonAuthError as e:
print e
Streaming API
~~~~~~~~~~~~~
*Usage is as follows; it's designed to be open-ended enough that you can adapt it to higher-level (read: Twitter must give you access)
@ -143,12 +161,7 @@ from you using them by this library.
Twython 3k
----------
There's an experimental version of Twython that's made for Python 3k. This is currently not guaranteed to
work in all situations, but it's provided so that others can grab it and hack on it.
If you choose to try it out, be aware of this.
**OAuth is now working thanks to updates from [Hades](https://github.com/hades). You'll need to grab
his [Python 3 branch for python-oauth2](https://github.com/hades/python-oauth2/tree/python3) to have it work, though.**
Full compatiabilty with Python 3 is now available seamlessly in the main Twython package. The Twython 3k package has been removed as of Twython 2.8.0
Questions, Comments, etc?
-------------------------
@ -156,8 +169,6 @@ My hope is that Twython is so simple that you'd never *have* to ask any question
You can also follow me on Twitter - `@ryanmcgrath <https://twitter.com/ryanmcgrath>`_
*Twython is released under an MIT License - see the LICENSE file for more information.*
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!