Merge 30a57c7950 into 445f9bde8b
This commit is contained in:
commit
7918d10174
8 changed files with 181 additions and 205 deletions
2
LICENSE
2
LICENSE
|
|
@ -1,6 +1,6 @@
|
||||||
The MIT License
|
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
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
|
|
||||||
|
|
@ -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
80
README.rst
Normal 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>`_
|
||||||
54
setup.py
54
setup.py
|
|
@ -4,39 +4,27 @@ from setuptools import setup
|
||||||
from setuptools import find_packages
|
from setuptools import find_packages
|
||||||
|
|
||||||
__author__ = 'Ryan McGrath <ryan@venodesigns.net>'
|
__author__ = 'Ryan McGrath <ryan@venodesigns.net>'
|
||||||
__version__ = '1.4.6'
|
__version__ = '1.5.0'
|
||||||
|
|
||||||
setup(
|
setup(
|
||||||
# Basic package information.
|
name='twython-django',
|
||||||
name = 'twython-django',
|
version=__version__,
|
||||||
version = __version__,
|
install_requires=['twython>=3.0.0', 'django'],
|
||||||
packages = find_packages(),
|
author='Ryan McGrath',
|
||||||
|
author_email='ryan@venodesigns.net',
|
||||||
# Packaging options.
|
license=open('LICENSE').read(),
|
||||||
include_package_data = True,
|
url='https://github.com/ryanmcgrath/twython/tree/master',
|
||||||
|
keywords='twitter search api tweet twython stream django integration',
|
||||||
# Package dependencies.
|
description='Actively maintained, pure Python wrapper for the Twitter API. Supports both normal and streaming Twitter APIs',
|
||||||
install_requires = ['simplejson',
|
long_description=open('README.rst').read(),
|
||||||
'oauth2',
|
include_package_data=True,
|
||||||
'httplib2',
|
packages=find_packages(),
|
||||||
'twython>=2.7.2',
|
classifiers=[
|
||||||
'django'],
|
'Development Status :: 4 - Beta',
|
||||||
provides = ['twython_django_oauth'],
|
'Intended Audience :: Developers',
|
||||||
|
'License :: OSI Approved :: MIT License',
|
||||||
# Metadata for PyPI.
|
'Topic :: Software Development :: Libraries :: Python Modules',
|
||||||
author = 'Ryan McGrath',
|
'Topic :: Communications :: Chat',
|
||||||
author_email = 'ryan@venodesigns.net',
|
'Topic :: Internet'
|
||||||
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 = [
|
|
||||||
'Development Status :: 4 - Beta',
|
|
||||||
'Intended Audience :: Developers',
|
|
||||||
'License :: OSI Approved :: MIT License',
|
|
||||||
'Topic :: Software Development :: Libraries :: Python Modules',
|
|
||||||
'Topic :: Communications :: Chat',
|
|
||||||
'Topic :: Internet'
|
|
||||||
]
|
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,3 @@
|
||||||
{% for tweet in tweets %}
|
{% for tweet in tweets %}
|
||||||
{{ tweet.text }}
|
{{ tweet.text }}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,13 @@
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
|
|
||||||
|
|
||||||
class TwitterProfile(models.Model):
|
class TwitterProfile(models.Model):
|
||||||
"""
|
"""
|
||||||
An example Profile model that handles storing the oauth_token and
|
An example Profile model that handles storing the oauth_token and
|
||||||
oauth_secret in relation to a user. Adapt this if you have a current
|
oauth_secret in relation to a user. Adapt this if you have a current
|
||||||
setup, there's really nothing special going on here.
|
setup, there's really nothing special going on here.
|
||||||
"""
|
"""
|
||||||
user = models.OneToOneField(User)
|
user = models.OneToOneField(User)
|
||||||
oauth_token = models.CharField(max_length = 200)
|
oauth_token = models.CharField(max_length=200)
|
||||||
oauth_secret = models.CharField(max_length = 200)
|
oauth_secret = models.CharField(max_length=200)
|
||||||
|
|
|
||||||
|
|
@ -2,18 +2,17 @@ from django.conf.urls.defaults import *
|
||||||
|
|
||||||
# your_app = name of your root djang app.
|
# your_app = name of your root djang app.
|
||||||
urlpatterns = patterns('twython_django_oauth.views',
|
urlpatterns = patterns('twython_django_oauth.views',
|
||||||
|
# First leg of the authentication journey...
|
||||||
# First leg of the authentication journey...
|
url(r'^login/?$', "begin_auth", name="twitter_login"),
|
||||||
url(r'^login/?$', "begin_auth", name="twitter_login"),
|
|
||||||
|
|
||||||
# Logout, if need be
|
# Logout, if need be
|
||||||
url(r'^logout/?$', "logout", name="twitter_logout"), # Calling logout and what not
|
url(r'^logout/?$', "logout", name="twitter_logout"), # Calling logout and what not
|
||||||
|
|
||||||
# This is where they're redirected to after authorizing - we'll
|
|
||||||
# further (silently) redirect them again here after storing tokens and such.
|
|
||||||
url(r'^thanks/?$', "thanks", name="twitter_callback"),
|
|
||||||
|
|
||||||
# An example view using a Twython method with proper OAuth credentials. Clone
|
# This is where they're redirected to after authorizing - we'll
|
||||||
# this view and url definition to get the rest of your desired pages/functionality.
|
# further (silently) redirect them again here after storing tokens and such.
|
||||||
url(r'^user_timeline/?$', "user_timeline", name="twitter_timeline"),
|
url(r'^thanks/?$', "thanks", name="twitter_callback"),
|
||||||
|
|
||||||
|
# An example view using a Twython method with proper OAuth credentials. Clone
|
||||||
|
# this view and url definition to get the rest of your desired pages/functionality.
|
||||||
|
url(r'^user_timeline/?$', "user_timeline", name="twitter_timeline"),
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -13,84 +13,72 @@ from twython_django_oauth.models import TwitterProfile
|
||||||
|
|
||||||
|
|
||||||
def logout(request, redirect_url=settings.LOGOUT_REDIRECT_URL):
|
def logout(request, redirect_url=settings.LOGOUT_REDIRECT_URL):
|
||||||
"""
|
"""
|
||||||
Nothing hilariously hidden here, logs a user out. Strip this out if your
|
Nothing hilariously hidden here, logs a user out. Strip this out if your
|
||||||
application already has hooks to handle this.
|
application already has hooks to handle this.
|
||||||
"""
|
"""
|
||||||
django_logout(request)
|
django_logout(request)
|
||||||
return HttpResponseRedirect(request.build_absolute_uri(redirect_url))
|
return HttpResponseRedirect(request.build_absolute_uri(redirect_url))
|
||||||
|
|
||||||
|
|
||||||
def begin_auth(request):
|
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'))
|
|
||||||
)
|
|
||||||
|
|
||||||
# Request an authorization url to send the user to...
|
For the most part, this is 100% drag and drop.
|
||||||
auth_props = twitter.get_authentication_tokens()
|
"""
|
||||||
|
# Instantiate Twython with the first leg of our trip.
|
||||||
|
twitter = Twython(settings.TWITTER_KEY, settings.TWITTER_SECRET)
|
||||||
|
|
||||||
|
# Request an authorization url to send the user to...
|
||||||
|
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'])
|
||||||
|
|
||||||
# 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):
|
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.
|
||||||
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
|
||||||
This is the view that stores the tokens you want
|
for querying data. Pay attention to this.
|
||||||
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'],
|
|
||||||
)
|
|
||||||
|
|
||||||
# Retrieve the tokens we want...
|
"""
|
||||||
authorized_tokens = twitter.get_authorized_tokens(request.GET['oauth_verifier'])
|
# Now that we've got the magic tokens back from Twitter, we need to exchange
|
||||||
|
# for permanent ones and store them...
|
||||||
|
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)
|
||||||
|
|
||||||
# If they already exist, grab them, login and redirect to a page displaying stuff.
|
# Retrieve the tokens we want...
|
||||||
try:
|
authorized_tokens = twitter.get_authorized_tokens(request.GET['oauth_verifier'])
|
||||||
user = User.objects.get(username = authorized_tokens['screen_name'])
|
|
||||||
except User.DoesNotExist:
|
# If they already exist, grab them, login and redirect to a page displaying stuff.
|
||||||
# We mock a creation here; no email, password is just the token, etc.
|
try:
|
||||||
user = User.objects.create_user(authorized_tokens['screen_name'], "fjdsfn@jfndjfn.com", authorized_tokens['oauth_token_secret'])
|
user = User.objects.get(username=authorized_tokens['screen_name'])
|
||||||
profile = TwitterProfile()
|
except User.DoesNotExist:
|
||||||
profile.user = user
|
# We mock a creation here; no email, password is just the token, etc.
|
||||||
profile.oauth_token = authorized_tokens['oauth_token']
|
user = User.objects.create_user(authorized_tokens['screen_name'], "fjdsfn@jfndjfn.com", authorized_tokens['oauth_token_secret'])
|
||||||
profile.oauth_secret = authorized_tokens['oauth_token_secret']
|
profile = TwitterProfile()
|
||||||
profile.save()
|
profile.user = user
|
||||||
|
profile.oauth_token = authorized_tokens['oauth_token']
|
||||||
|
profile.oauth_secret = authorized_tokens['oauth_token_secret']
|
||||||
|
profile.save()
|
||||||
|
|
||||||
|
user = authenticate(
|
||||||
|
username=authorized_tokens['screen_name'],
|
||||||
|
password=authorized_tokens['oauth_token_secret']
|
||||||
|
)
|
||||||
|
login(request, user)
|
||||||
|
return HttpResponseRedirect(redirect_url)
|
||||||
|
|
||||||
user = authenticate(
|
|
||||||
username = authorized_tokens['screen_name'],
|
|
||||||
password = authorized_tokens['oauth_token_secret']
|
|
||||||
)
|
|
||||||
login(request, user)
|
|
||||||
return HttpResponseRedirect(redirect_url)
|
|
||||||
|
|
||||||
def user_timeline(request):
|
def user_timeline(request):
|
||||||
"""
|
"""An example view with Twython/OAuth hooks/calls to fetch data about the user in question."""
|
||||||
An example view with Twython/OAuth hooks/calls to fetch data about the user
|
user = request.user.twitterprofile
|
||||||
in question. Pretty self explanatory if you read through it...
|
twitter = Twython(settings.TWITTER_KEY, settings.TWITTER_SECRET,
|
||||||
"""
|
user.oauth_token, user.oauth_secret)
|
||||||
user = request.user.twitterprofile
|
user_tweets = twitter.get_home_timeline()
|
||||||
twitter = Twython(
|
return render_to_response('tweets.html', {'tweets': user_tweets})
|
||||||
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()
|
|
||||||
return render_to_response('tweets.html', {'tweets': user_tweets})
|
|
||||||
|
|
|
||||||
Reference in a new issue