Get the latest tweet from your Twitter account using Twython and Django

Twython is the premier Python library providing an easy (and up-to-date) way to access Twitter data. Actively maintained and featuring support for Python 2.6+ and Python 3. It’s been battle tested by companies, educational institutions and individuals alike.

At Brite Revolution we were using version 1.0 of the Twitter REST API to pull in the latest tweet from the public timeline of our company twitter account (@briterevolution) to post in the footer of our site.

We had everything working great using python-twitter until Twitter deprecated v1.0 of their REST API. This forced all users to migrate to v1.1 which in turn required authentication on every API endpoint. This includes the public timeline API endpoints which had not required authentication in previous versions. What to do?

My initial thought was to upgrade our python-twitter library to start working with version 1.1 of the new API. To my dismay, as of the time of this post python-twitter still hadn’t released an official update that supports version 1.1 and I’m not keen on deploying beta software into a live production environment. Instead, I turned to the official Twitter API docs and began looking through the recommended Python libraries for a solution. Enter Twython.

Twython

A few new lines in Django settings (we’ll get to that) and Twython made quick work out of grabbing the latest tweet from our public timeline. I’d encourage you to check out Twython when considering a Twitter library for your next project.

Getting the latest tweets with Twython

This tutorial assumes you have a basic Django project up and running. Let’s get started.

1. Install Twython and any dependencies

2. Create your Twitter application

Head over to https://dev.twitter.com/apps and register your Twitter application!

In our particular use case we’re only using this Twitter application for our Twitter account. We still have to use Oauth to authenticate our own Twitter account. The good news is that Twitter makes this really easy. Once you have your Twitter app created scroll down to the bottom of the details section. There you’ll see a “Create my access token” button…click it. This will generate the access token and access token secret necessary for you to properly authenticate with the Twitter REST API.

3. Configure Django settings

We need to tell Twython what our Oauth settings are. There are several ways to do this with some methods being more secure than others. For the sake of simplicity we’re going to put our Oauth credentials into our projects settings.py file.

4. Custom context processor

We’re going to write a custom context processor for our Django project so that our latest tweet is added to the template context.

Some helpful hints for the code above

  1. As of this writing you can make 180 requests per 15 minute window to the REST API while other API calls have different rate limiting. Since our recent tweet loads on every page we don’t want to slow down each page load while waiting for the Twitter API to respond. We chose to set our cache timeout at 10 minutes (600 seconds). You can modify this via the setting for TWITTER_CACHE_TIMEOUT.
  2. I prefer the Tweet parser included with twitter-text-python. I installed it into my projects utils folder as twitter_parser.py.

Put this code into a context_processors.py file in the root of your project. Make sure to add the new context_processors.py file into your settings files so Django knows about it..

5. Template

We’re ready to load our latest tweet into our template. By using a context processor we don’t need to do anything additional with our views to get access to the latest tweet. We just need to use the variable name we created in the context processor (“latest_tweet”).