Fixed to work with Twython 3.0.0 or greater and cleanup

Added LICENSE
updated README
updated setup.py
PEP8'd stuff
This commit is contained in:
Mike Helmick 2013-06-19 19:11:43 -04:00
parent 445f9bde8b
commit 30a57c7950
8 changed files with 181 additions and 205 deletions

View file

@ -1,6 +1,6 @@
The MIT License
Copyright (c) 2009 - 2010 Ryan McGrath
Copyright (c) 2013 Ryan McGrath
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View file

@ -1,80 +0,0 @@
Twython-Django (An example Django Python Twitter OAuth Application, using Twython)
=========================================================================================
OAuth is an annoying specification to work with. Twitter has an awesome and somewhat unique
real time data stream, though, and it'd be a shame to miss out on that stuff because of the warts
of a specification.
Twython supports OAuth authentication with Twitter now, and this is a sample Django application to get
people up and running (fairly) instantly with Twitter OAuth in Django. Enjoy.
Requirements
-----------------------------------------------------------------------------------------------------
Django - pretty self explanatory. http://djangoproject.com/
Twython - the Python Twitter API wrapper of choice.
(pip install | easy_install) twython
...or, you can clone the repo and install it the old fashioned way.
git clone https://ryanmcgrath@github.com/ryanmcgrath/twython.git
cd twython
sudo python setup.py install
Twython (for versions of Python before 2.6) requires a library called
"simplejson". Depending on your flavor of package manager, you can do the following...
(pip install | easy_install) simplejson
Twython also requires the (most excellent) OAuth2 library for handling OAuth tokens/signing/etc. Again...
(pip install | easy_install) oauth2
Installation
-----------------------------------------------------------------------------------------------------
pip install git+http://github.com/ryanmcgrath/twython-django.git
Add "twython_django_oauth" to your "INSTALLED_APPS" in your settings.py file. If you wish to use the example
template, feel free to copy that over as well.
After you've done that, specify the following urlconf in your root urls.py:
(r'^your_url_extension/', include('twython_django_oauth.urls')),
Add the following settings to settings.py:
TWITTER_KEY = 'your_key'
TWITTER_SECRET = 'your_secret'
LOGIN_URL='/your_url_extension/login'
LOGOUT_URL='/your_url_extension/logout'
LOGIN_REDIRECT_URL='/'
LOGOUT_REDIRECT_URL='/'
Restart your Django app, and it should all work auto-magically. Build onwards from here if you're
just starting out, or integrate this into your existing model setup if you're already Django-savvy.
Enjoy!
Need Twython Help?
-----------------------------------------------------------------------------------------------------
If you need help with the Twython library itself, check out the project on Github, it's all pretty self
contained (twython/twitter_endpoints.py contains just about every function definition you'll need):
https://github.com/ryanmcgrath/twython
Questions, Comments, etc?
-----------------------------------------------------------------------------------------------------
My hope is that twython-django 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.
twython-django is released under an MIT License - see the LICENSE file for more information.

80
README.rst Normal file
View file

@ -0,0 +1,80 @@
Twython-Django
==============
(An example Django Python Twitter OAuth Application, using Twython)
OAuth is an annoying specification to work with. Twitter has an awesome and somewhat unique real time data stream, though, and it'd be a shame to miss out on that stuff because of the warts of a specification.
Twython supports OAuth authentication with Twitter now, and this is a sample Django application to get people up and running (fairly) instantly with Twitter OAuth in Django. Enjoy.
Installation
------------
Install `twython-django` via `pip <http://www.pip-installer.org/>`_
.. code-block:: bash
$ pip install twython-django
or, with `easy_install <http://pypi.python.org/pypi/setuptools>`_
.. code-block:: bash
$ easy_install twython-django
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
.. code-block:: bash
git clone git://github.com/ryanmcgrath/twython-django.git
cd twython
python setup.py install
Getting Started
---------------
Add ``twython_django_oauth`` to your ``INSTALLED_APPS`` in your ``settings.py`` file.
If you wish to use the example template, feel free to copy that over as well.
Update urls
^^^^^^^^^^^
Specify the following urlconf in your root urls.py:
.. code-block:: python
(r'^your_url_extension/', include('twython_django_oauth.urls')),
Modify settings.py
^^^^^^^^^^^^^^^^^^
Add the following settings to your settings.py
.. code-block:: python
TWITTER_KEY = 'your_key'
TWITTER_SECRET = 'your_secret'
LOGIN_URL='/your_url_extension/login'
LOGOUT_URL='/your_url_extension/logout'
LOGIN_REDIRECT_URL='/'
LOGOUT_REDIRECT_URL='/'
Need Twython Help?
------------------
If you need help with the Twython library itself, check out the project on Github. It's all pretty self contained (``twython/endpoints.py`` contains just about every function definition you'll need):
https://github.com/ryanmcgrath/twython
Questions, Comments, etc?
-------------------------
My hope is that twython-django 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 contact me on Twitter:
- `@ryanmcgrath <https://twitter.com/ryanmcgrath>`_

View file

@ -4,34 +4,22 @@ from setuptools import setup
from setuptools import find_packages
__author__ = 'Ryan McGrath <ryan@venodesigns.net>'
__version__ = '1.4.6'
__version__ = '1.5.0'
setup(
# Basic package information.
name = 'twython-django',
version = __version__,
packages = find_packages(),
# Packaging options.
include_package_data = True,
# Package dependencies.
install_requires = ['simplejson',
'oauth2',
'httplib2',
'twython>=2.7.2',
'django'],
provides = ['twython_django_oauth'],
# Metadata for PyPI.
author = 'Ryan McGrath',
author_email = 'ryan@venodesigns.net',
license = 'MIT License',
url = 'http://github.com/ryanmcgrath/twython-django/tree/master',
keywords = 'Django integration for twython',
description = 'An easy (and up to date) way to access Twitter data with Python.',
long_description = open('README.markdown').read(),
classifiers = [
name='twython-django',
version=__version__,
install_requires=['twython>=3.0.0', 'django'],
author='Ryan McGrath',
author_email='ryan@venodesigns.net',
license=open('LICENSE').read(),
url='https://github.com/ryanmcgrath/twython/tree/master',
keywords='twitter search api tweet twython stream django integration',
description='Actively maintained, pure Python wrapper for the Twitter API. Supports both normal and streaming Twitter APIs',
long_description=open('README.rst').read(),
include_package_data=True,
packages=find_packages(),
classifiers=[
'Development Status :: 4 - Beta',
'Intended Audience :: Developers',
'License :: OSI Approved :: MIT License',

View file

@ -1,6 +1,7 @@
from django.db import models
from django.contrib.auth.models import User
class TwitterProfile(models.Model):
"""
An example Profile model that handles storing the oauth_token and
@ -8,5 +9,5 @@ class TwitterProfile(models.Model):
setup, there's really nothing special going on here.
"""
user = models.OneToOneField(User)
oauth_token = models.CharField(max_length = 200)
oauth_secret = models.CharField(max_length = 200)
oauth_token = models.CharField(max_length=200)
oauth_secret = models.CharField(max_length=200)

View file

@ -2,7 +2,6 @@ from django.conf.urls.defaults import *
# your_app = name of your root djang app.
urlpatterns = patterns('twython_django_oauth.views',
# First leg of the authentication journey...
url(r'^login/?$', "begin_auth", name="twitter_login"),

View file

@ -20,50 +20,44 @@ def logout(request, redirect_url=settings.LOGOUT_REDIRECT_URL):
django_logout(request)
return HttpResponseRedirect(request.build_absolute_uri(redirect_url))
def begin_auth(request):
"""
The view function that initiates the entire handshake.
"""The view function that initiates the entire handshake.
For the most part, this is 100% drag and drop.
"""
# Instantiate Twython with the first leg of our trip.
twitter = Twython(
twitter_token = settings.TWITTER_KEY,
twitter_secret = settings.TWITTER_SECRET,
callback_url = request.build_absolute_uri(reverse('twython_django_oauth.views.thanks'))
)
twitter = Twython(settings.TWITTER_KEY, settings.TWITTER_SECRET)
# Request an authorization url to send the user to...
auth_props = twitter.get_authentication_tokens()
callback_url = request.build_absolute_uri(reverse('twython_django_oauth.views.thanks'))
auth_props = twitter.get_authentication_tokens(callback_url)
# Then send them over there, durh.
request.session['request_token'] = auth_props
return HttpResponseRedirect(auth_props['auth_url'])
def thanks(request, redirect_url=settings.LOGIN_REDIRECT_URL):
"""
A user gets redirected here after hitting Twitter and authorizing your
app to use their data.
***
def thanks(request, redirect_url=settings.LOGIN_REDIRECT_URL):
"""A user gets redirected here after hitting Twitter and authorizing your app to use their data.
This is the view that stores the tokens you want
for querying data. Pay attention to this.
***
"""
# Now that we've got the magic tokens back from Twitter, we need to exchange
# for permanent ones and store them...
twitter = Twython(
twitter_token = settings.TWITTER_KEY,
twitter_secret = settings.TWITTER_SECRET,
oauth_token = request.session['request_token']['oauth_token'],
oauth_token_secret = request.session['request_token']['oauth_token_secret'],
)
oauth_token = request.session['request_token']['oauth_token']
oauth_token_secret = request.session['request_token']['oauth_token_secret']
twitter = Twython(settings.TWITTER_KEY, settings.TWITTER_SECRET,
oauth_token, oauth_token_secret)
# Retrieve the tokens we want...
authorized_tokens = twitter.get_authorized_tokens(request.GET['oauth_verifier'])
# If they already exist, grab them, login and redirect to a page displaying stuff.
try:
user = User.objects.get(username = authorized_tokens['screen_name'])
user = User.objects.get(username=authorized_tokens['screen_name'])
except User.DoesNotExist:
# We mock a creation here; no email, password is just the token, etc.
user = User.objects.create_user(authorized_tokens['screen_name'], "fjdsfn@jfndjfn.com", authorized_tokens['oauth_token_secret'])
@ -74,23 +68,17 @@ def thanks(request, redirect_url=settings.LOGIN_REDIRECT_URL):
profile.save()
user = authenticate(
username = authorized_tokens['screen_name'],
password = authorized_tokens['oauth_token_secret']
username=authorized_tokens['screen_name'],
password=authorized_tokens['oauth_token_secret']
)
login(request, user)
return HttpResponseRedirect(redirect_url)
def user_timeline(request):
"""
An example view with Twython/OAuth hooks/calls to fetch data about the user
in question. Pretty self explanatory if you read through it...
"""
"""An example view with Twython/OAuth hooks/calls to fetch data about the user in question."""
user = request.user.twitterprofile
twitter = Twython(
twitter_token = settings.TWITTER_KEY,
twitter_secret = settings.TWITTER_SECRET,
oauth_token = user.oauth_token,
oauth_token_secret = user.oauth_secret
)
user_tweets = twitter.getHomeTimeline()
twitter = Twython(settings.TWITTER_KEY, settings.TWITTER_SECRET,
user.oauth_token, user.oauth_secret)
user_tweets = twitter.get_home_timeline()
return render_to_response('tweets.html', {'tweets': user_tweets})