Writing an Android Twitter Client with Python

Get up and running with the Android Scripting Environment. Whip up a Twitter update app in a matter of minutes and tell everyone what sandwich you're eating from within Android!

I used the adb utility which ships with the SDK to copy the script to the device. Assuming you have the SDK installed, you can do this with the following command: adb push ts.py /data/data/com.google.ase/scripts/ts.py. In order to test this script on your own, you will require a Twitter account. My device is running Android 1.5. Of course, using the adb utility to copy the file to your device is not required — just trying to save you a little time.

Confessions of a Python Newbie

The script used for our demo (ts.py) is written in Python. Before we jump into the script itself, I have a quick confession to make: I have avoided Python up until now.

Now don’t throw anything at me, I haven’t avoided it because there is anything inherently wrong with Python. There is just not enough time in the day to keep up on every language and up until now, I have been quite happy working in other environments and languages. But I have been curious about Python and there is nothing I like better than an excuse to learn something new. So I used the opportunity of learning more about Android Scripting Environment as an opportunity to learn Python!

While I’m no Pythonistia yet, this actually uncovers one of the beauties of Android Scripting Engine: I was able to write something functional in a language I have never used before. If you have skills in Python, or one of the other supported scripting languages, you can be up and running in no time at all creating your own Android applications. So with that little caveat behind us, let’s have a look at the ts.py script which allows us to make updates to a Twitter account.

import android       # core Android routines
import httplib       # for talking to web servers
import urllib        # to format our status update nicely
import base64        # to encode our username and password for Basic authentication

print 'Cool, we\'re running!'       # sorry, had to do this...

# get an instance of Android
droid = android.Android()

# where are we posting data to?
twitterhost = 'twitter.com'
uri = '/statuses/update.xml?'

# get our status update from user
statusmsg = droid.getInput('Twitter Update','Whatcha Doin?')

#extract the "textual" portion of the response
statusdata = "%(result)s" % statusmsg

# uncomment the next line to display the message to the terminal screen
#print statusdata

# uncomment the next line to display a notification to the user

# clean up the data so it can be sent as the 'querystring'
statusupdate = urllib.urlencode({'status':statusdata})

# setup your username and password here...
username = 'yourusernamehere'
password = 'yourpasswordhere'

credentials = username + ":" + password

# uncomment the next line to see what credentials you are using....
#print credentials

encodedcredentials = base64.encodestring(credentials)

# connect to server
h = httplib.HTTP(twitterhost)

# build url we want to request
fullurl = uri + statusupdate

#uncomment the next line to see the url printed
#print fullurl

# POST our data.  Twitter requires status updates to be POSTed
h.putrequest('POST',uri + statusupdate)

# setup the authentication header
h.putheader('Authorization','Basic ' + base64.encodestring(credentials))

# setup the user agent

# we're done with the headers....

# get the response from the request
returncode,returnmsg,headers = h.getreply()

# should compare the returncode to 200 for a good response, etc.

#display whatever the results are....
f = h.getfile()
print f.read()

This source file is maintained in a Google Code hosting project.

Twitter Script in Action

When the app initiated, it prompts the user for a textual input, asking the user for a status update. If you are not familiar with what a status update is, ask a teenager. Just remember to keep it clean — as it will be visible to the world on your Twitter account! Once you have entered a small piece of text which informs the world of the very important task you are accomplishing such as brushing your teeth, taking a walk in the park, writing a new Python script, etc., hit the OK button on the dialog.

Twitter Status application in action
Twitter Status application in action

And if we’re successful, we should see the update on our Twitter page.

Update shown on my Twitter page
The extremely meta update shown on my Twitter page

There is so much more to the Android Scripting Environment — I highly encourage you to take a look, at the project page to learn a bit more for yourself. Why not install the ASE on your Android device and show off your Python skills? If Python is not your thing, it appears that the ASE will soon be supporting Ruby and JavaScript. Happy scripting!

Comments on "Writing an Android Twitter Client with Python"

Here is an excellent Weblog You might Find Fascinating that we encourage you to visit.

That will be the finish of this report. Here you?ll come across some websites that we think you?ll enjoy, just click the hyperlinks.

We prefer to honor many other net web sites on the web, even when they aren?t linked to us, by linking to them. Under are some webpages worth checking out.

Usually posts some extremely exciting stuff like this. If you?re new to this site.

Leave a Reply