Learning Python Network Programming

(Sean Pound) #1
Chapter 3

A Twitter world clock


To illustrate some of the functionalities of the Twitter API, we're going to write
the code for a simple Twitter world clock. Our application will periodically poll its
Twitter account for mentions which contain a recognizable city name, and if it finds
one, then it will reply to the Tweet with the current local time of that city. In Twitter
speak, a mention is any Tweet which includes our account name prefixed by an @, for
example, @myaccount.


Authentication for Twitter


Similar to S3, we need to determine how authentication will be managed before we
get started. We need to register, and then we need to find out how Twitter expects us
to authenticate our requests.


Registering your application for the Twitter API


We need to create a Twitter account, register our application against the account,
and then we will receive the authentication credentials for our app. It's also a good
idea to set up a second account, which we can use for sending test tweets to the
application account. This provides for a cleaner way of checking whether the app is
working properly, rather than having the app account send tweets to itself. There's
no limit on the number of Twitter accounts that you can create.


To create an account, go to http://www.twitter.com and complete the signup
process. Do the following for registering your application once you have a
Twitter account:



  1. Log into http://apps.twitter.com with your main Twitter account, and
    then create a new app.

  2. Fill out the new app form, note that Twitter application names need to be
    unique globally.

  3. Go to the app's settings and then change the app permissions to have read
    and write access. You may need to register your mobile number for enabling
    this. Even if you're unhappy about supplying this, we can create the full app;
    however the final function that sends a tweet in reply won't be active.


Now we need to get our access credentials, as shown here:



  1. Go to the Keys and Access Tokens section and then note the Consumer Key
    and the Access Secret.

  2. Generate an Access Token.

  3. Note down the Access Token and the Access Secret.

Free download pdf