From 30a57c7950c87c1b2024042114a7a121b33d5a2b Mon Sep 17 00:00:00 2001 From: Mike Helmick Date: Wed, 19 Jun 2013 19:11:43 -0400 Subject: [PATCH] Fixed to work with Twython 3.0.0 or greater and cleanup Added LICENSE updated README updated setup.py PEP8'd stuff --- LICENSE | 2 +- README.markdown | 80 --------------------- README.rst | 80 +++++++++++++++++++++ setup.py | 54 ++++++-------- templates/tweets.html | 2 +- twython_django_oauth/models.py | 19 ++--- twython_django_oauth/urls.py | 23 +++--- twython_django_oauth/views.py | 126 +++++++++++++++------------------ 8 files changed, 181 insertions(+), 205 deletions(-) delete mode 100644 README.markdown create mode 100644 README.rst diff --git a/LICENSE b/LICENSE index cd5b253..f723943 100644 --- a/LICENSE +++ b/LICENSE @@ -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 diff --git a/README.markdown b/README.markdown deleted file mode 100644 index fa4a07d..0000000 --- a/README.markdown +++ /dev/null @@ -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. diff --git a/README.rst b/README.rst new file mode 100644 index 0000000..dd9717f --- /dev/null +++ b/README.rst @@ -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 `_ + +.. code-block:: bash + + $ pip install twython-django + +or, with `easy_install `_ + +.. code-block:: bash + + $ easy_install twython-django + +But, hey... `that's up to you `_. + +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 `_ diff --git a/setup.py b/setup.py index 9ce1b4e..83f1a5b 100644 --- a/setup.py +++ b/setup.py @@ -4,39 +4,27 @@ from setuptools import setup from setuptools import find_packages __author__ = 'Ryan McGrath ' -__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 = [ - 'Development Status :: 4 - Beta', - 'Intended Audience :: Developers', - 'License :: OSI Approved :: MIT License', - 'Topic :: Software Development :: Libraries :: Python Modules', - 'Topic :: Communications :: Chat', - 'Topic :: Internet' - ] + 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', + 'Topic :: Software Development :: Libraries :: Python Modules', + 'Topic :: Communications :: Chat', + 'Topic :: Internet' + ] ) diff --git a/templates/tweets.html b/templates/tweets.html index a2ec453..da30e5d 100644 --- a/templates/tweets.html +++ b/templates/tweets.html @@ -1,3 +1,3 @@ {% for tweet in tweets %} - {{ tweet.text }} + {{ tweet.text }} {% endfor %} diff --git a/twython_django_oauth/models.py b/twython_django_oauth/models.py index 0e51bc3..3319d40 100644 --- a/twython_django_oauth/models.py +++ b/twython_django_oauth/models.py @@ -1,12 +1,13 @@ from django.db import models -from django.contrib.auth.models import User +from django.contrib.auth.models import User + class TwitterProfile(models.Model): - """ - 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 - 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) + """ + 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 + 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) diff --git a/twython_django_oauth/urls.py b/twython_django_oauth/urls.py index 27a4164..2b6e180 100644 --- a/twython_django_oauth/urls.py +++ b/twython_django_oauth/urls.py @@ -2,18 +2,17 @@ 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"), + # First leg of the authentication journey... + url(r'^login/?$', "begin_auth", name="twitter_login"), - # Logout, if need be - 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"), + # Logout, if need be + url(r'^logout/?$', "logout", name="twitter_logout"), # Calling logout and what not - # 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"), + # 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 view and url definition to get the rest of your desired pages/functionality. + url(r'^user_timeline/?$', "user_timeline", name="twitter_timeline"), ) diff --git a/twython_django_oauth/views.py b/twython_django_oauth/views.py index 5a31326..b171adc 100644 --- a/twython_django_oauth/views.py +++ b/twython_django_oauth/views.py @@ -13,84 +13,72 @@ from twython_django_oauth.models import TwitterProfile def logout(request, redirect_url=settings.LOGOUT_REDIRECT_URL): - """ - Nothing hilariously hidden here, logs a user out. Strip this out if your - application already has hooks to handle this. - """ - django_logout(request) - return HttpResponseRedirect(request.build_absolute_uri(redirect_url)) + """ + Nothing hilariously hidden here, logs a user out. Strip this out if your + application already has hooks to handle this. + """ + django_logout(request) + return HttpResponseRedirect(request.build_absolute_uri(redirect_url)) + def begin_auth(request): - """ - 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')) - ) + """The view function that initiates the entire handshake. - # Request an authorization url to send the user to... - auth_props = twitter.get_authentication_tokens() + For the most part, this is 100% drag and drop. + """ + # 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): - """ - 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 - 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'], - ) + This is the view that stores the tokens you want + for querying data. Pay attention to this. - # 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. - try: - 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']) - profile = TwitterProfile() - profile.user = user - profile.oauth_token = authorized_tokens['oauth_token'] - profile.oauth_secret = authorized_tokens['oauth_token_secret'] - profile.save() + # 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']) + 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']) + profile = TwitterProfile() + 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): - """ - An example view with Twython/OAuth hooks/calls to fetch data about the user - in question. Pretty self explanatory if you read through it... - """ - 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() - return render_to_response('tweets.html', {'tweets': user_tweets}) + """An example view with Twython/OAuth hooks/calls to fetch data about the user in question.""" + user = request.user.twitterprofile + 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}) -- 2.39.5