Monday, September 28, 2009

Twitter Application with Google App Engine/Python


To get started, you will need

  • Python 2.5.2 - According to Google App Engine documentation here - make sure to use the right version to avoid any compatibility issues
  • Eclipse - IDE - makes your life easier
  • PyDev- Eclipse Plugin to support Python - to install : in Eclipse, go to Help -> Software Updates -> Add Site :
  • Google App Engine SDK - get the latest version .msi file. double click
  • Tweepy - best damn Twitter/Python API to date
    • If you have git installed: 1. clone the repository from github 2. in the tweepy directory, type in install
    • If you don't have git, download the archive from the site, unzip, and follow 2. from above
  • Github - optional - it's a good practice to use SCM for your code. I wrote the following article on how to use it with Eclipse
    • I won't be discussing how to commit your code (atleast here). If you want to maintain sanity, I suggest you use an SCM of some sort

Screwing Around with Tweepy

Next we're going to console use the Python console to experiment with the Twitter API. Please note, text in Courier New is meant to be executed in a Python shell

>>> import tweepy # if this worked, you installed it correctly

There are two way to get authenticated with Twitter. With an authenticated Twitter API you can do all the things you can do with a twitter account: post messages, read your DM, etc.
  1. via username and password ( basic authentication )
  2. OAuth - requires registering your app here.

>>> twitter ='basic','twitter_account_name','twitter_account_password')

if this worked, you're in! Let's try to update your status

>>> twitter.update_status("I'm using the internet!")

So now that you have the Twitter API working, go through tweepy's API/tutorials to figure out what it can do. Try calling other methods on the twitter class. See what happens. If you're reading this tutorial, you're probably ahead of my on this one

Setting Up Eclipse

Follow the Configuring Eclipse on Windows to Use With Google App Engine tutorial. It's pretty detailed.

Google App Engine requires you to upload all the extra libraries you need to appspot in order for you app to work. This means you need to add the tweepy library to your project:

  1. open the directory where you installed tweepy above in Explorer
  2. Drag&Drop the whole tweepy directory in build\lib\ into the \src folder
  3. Tweepy is installed - use it

Making the App

We're going to make a simple app. It will
  • run a search on all public tweets for the phrase "#python"
  • store the results
  • display the top 20

To do so, we will need two scripts files to execute
  1. - shows top 20 tweets
  2. - performs searches at 5 minute intervals

and a common library file to store stuff ( )

stay tuned for Part 2

No comments: