Saturday 12 March 2016

Use the Twitter streaming API in Django

languages over the years, and one of these is, of course, the very popular Python. In the past, we’ve done tutorials on using Twitter via Python to create little apps and streaming programs. The various libraries make good use of the API and, with a little bit of tweaking, you can carry out a lot of Twitter-powered functions.

Twitter is a web tool, though, but luckily there is a version of Python for the web: Django. Some of the tools have been further ported to Django and allow you to integrate it into websites fairly easily. This can be used for something like a sidebar that has a running stream of tweets from the website owner or from a list of relevant information, or for sniffing out a certain hashtag. Using these in web development requires you to have your own API key from Twitter, but those are fairly easy to get when you look in the right place, and we’ll show you where that place is shortly.

Getting a Twitter API key enables you to start accessing Twitter via a whole number of ways, not just for a bit of Django in this tutorial. Bear in mind that it still doesn’t guarantee full access to Twitter, though; there is a limit to the amount of requests that it can make and there are no push notifications available for anything but the homegrown Twitter applications. Still, you can carry out a lot more different functions with it than those you might see in more official efforts.

To get the API, you need to first head to the Twitter Developer site while logged in (http://dev.twitter.com). The layout has changed a bit recently, but to find your list of apps to manage and/or add a new one, scroll down to the bottom to find the ‘Manage Your Apps’ option. From there, go to ‘Create New App’ and fill out all the necessary details for whatever it is that you want to create, filling in any blanks in your profile that Twitter wants in the process.

django1-1

Get the library set up

We’re going to be using the excellent Django Twitter Stream library by user michaelbrooks (http://ift.tt/1YJe2mD) to create our own Twitter streams. It runs on tweepy, one of the better Python modules for using Twitter, and makes use of a database to store data. We first need to install it using:

  $ pip install -e git+http://ift.tt/1plSukj

django-twitter-stream.git#egg=django-twitter-stream

Once installed, you should now add it to the INSTALLED_APPS in the Django settings file:

  INSTALLED_APPS = (

      # other apps

      “twitter_stream”,

  )

If you’re using MySQL, you’ll need to make an entry that enables it to save the tweets the correct way; MySQL is set to only read standard utf8 characters sets, whereas tweets use 4-byte characters that aren’t supported by that. You can add an entry to your database settings to fix that, though:

  DATABASES = {

      ‘default’: {

          ‘ENGINE’: ‘django.db.backends.mysql’,

          # username, password, etc…

          ‘OPTIONS’: {

              ‘charset’: ‘utf8mb4’,

          },

      }

  }

Finally, you need to update your database for the installation to complete. Do this with:

  $ python manage.py syncdb

django4-1

Connect to Twitter

Now the Twitter stream module is installed, you’ll find a new section on the Django admin page for ApiKey. Here you can add your Twitter keys – get them from the settings of the application we created earlier, filling in all the requested fields from the ApiKey.

We can now add filtering terms to the Twitter stream. Bear in mind that when we say ‘filter’, we’re referring to a specific term in the Twitter API. It’s basically the same as the standard Twitter search function, but you can give it more precise parameters that will return very specific tweets, such as all posts from a certain user, hashtag, list or whatever you’d like the website to display. You can add whatever filters you want to use to the database using the Django Admin interface again. There are exact details on all available filters inside the documentation (bit.ly/1LfN2Gh).

Most of it is the way that it reacts to certain phrases. Words with no punctuation will lead it to look for any instance of that word on its own. This includes Twitter account names, hashtags, instances where the word has punctuation attached to it and even links with the term in.

Adding a space between words allows you to search for multiple words in one tweet, and they don’t have to be together or in a specific order. You can also add a comma between terms as an AND/OR statement, returning results that contain either or both set of words.

The GET user parameter can be used to returns results from specific users, either from just their feed or only certain results from that feed, based on the keywords. There are more ways to narrow it down, such as by location or via lists, and with a bit of ingenuity you can create a complicated string that handles all your needs.

django2-1

Start streaming tweets

To begin building the database that you can start drawing from, you need to start the streaming API using the following:

  $ python manage.py stream

This will fill up by polling the stream every ten seconds. You can limit this rate by instructing when the stream should poll, but you cannot get it to create multiple streams. You can try if you want, but you may get your own API banned, so it’s not worth it. Anyway, to change this rate you can instead use:

  $ python manage.py stream MyAPIKeys –poll-interval 30

You can also get the stream to save to a .json file. You can do this if you want to read the file rather than the database, or if you want to shut down the database. To redirect it, use:

  $ python manage.py stream –to-file [file].json

Tweet page

You can create a stream page to initially check the stream before you start adding elements from it into a website. To do this, you’ll first need to add a new URL line to the url conf like so:

  url(r‘^stream/’, include(‘twitter_stream.urls’,

namespace=“twitter_stream”)),

You’ll also need to add it as an app in the INSTALLED_APPS file, with a section like so:

  INSTALLED_APPS = (

      # other apps

      ‘django.contrib.humanize’,

      ‘bootstrap3’,

      ‘jsonview’,

  )

Create a settings file

If you want to start customising the Twitter streamer, you need to add specific settings to the Django settings file. You can’t just change one setting and ignore the rest, though, so you’ll need to add all the default settings before making any modifications. Here’s what you should start with in your settings file:

  TWITTER_STREAM_SETTINGS = {

  # Set to True to save embedded retweeted_status tweets

  # Normally these are discarded.

      ‘CAPTURE_EMBEDDED’: False,

  # Change the default term track & tweet insert interval

      ‘POLL_INTERVAL’: 10,

  # The name of the default keys to use for streaming.

  # If not set, we’ll just grab one.

      ‘DEFAULT_KEYS_NAME’: None,

  # Put the stream in a loop so random termination

  # will be prevented.

      ‘PREVENT_EXIT’: False,

  }

With this, you can start making permanent changes to the way the stream works, as well which API keys to use at any given time if you’ve decided to use multiple APIs in the app.

django3-1

Implement into your site

The API allows you to create a database or a file to keep the necessary tweets in. As with any database or list file in Django, you can start using data from it to fill up a page or part of a page. Tweets contain time data, among other things, so you can easily organise them into chronological order to create a more natural stream.

If you are looking at retweets, though, be aware that if you’re writing information to a file, new entries will overwrite other data, so you need to use a database for that.

With these tools you can start updating your website to be much more in line with modern social media practices, and help direct people to other avenues of information than the main website.

Test settings

Try the following settings file to get things working at first, and build upon it to make the code better and more efficient.

  from os.path import abspath, dirname, join, normpath

  # Absolute filesystem path to the

  # Django project directory:

  DJANGO_ROOT = dirname(dirname(abspath(__file__)))

  SECRET_KEY = ‘secret’

  DATABASES = {

      ‘default’: {

          ‘ENGINE’: ‘django.db.backends.sqlite3’,

          ‘NAME’: ‘test_database.db’,

      }

  }

  STATICFILES_FINDERS = (

      ‘django.contrib.staticfiles.finders.

FileSystemFinder’,

      ‘django.contrib.staticfiles.finders.

AppDirectoriesFinder’,

  )

  TEMPLATE_LOADERS = (

      ‘django.template.loaders.filesystem.Loader’,

      ‘django.template.loaders.app_directories.Loader’,

  )

  TEMPLATE_DIRS = (

      normpath(join(DJANGO_ROOT, ‘templates’)),

  )

  INSTALLED_APPS = (

      ‘django.contrib.humanize’,

      ‘django.contrib.staticfiles’,

      ‘bootstrap3’,

      ‘twitter_stream’,

      ‘south’,

 



from Linux User & Developer – the Linux and FOSS mag for a GNU generation http://ift.tt/1V0zq6Y
via IFTTT

No comments:

Post a Comment

Amazon

Donate

Donate Towards More Raspberry PI's for Projects