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
#droid.makeToast(statusdata)
# 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
h.putheader('user-agent','Android-Scripting-Engine-Python')
# we're done with the headers....
h.endheaders()
# 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
And if we’re successful, we should see the update on our 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!
Frank Ableson is an entrepreneur, writer and mobile software developer who probably should have been a hardware designer, but soldering got in the way. Frank's technical interests are in mobile and embedded computing. Frank is the lead author of
Unlocking Android 2e, an intermediate text on software development for the Android platform.
Use coupon code linuxmag for a 30% discount.
Comments on "Writing an Android Twitter Client with Python"
hello, am trying the above application but am receiving the following error
statusdata=”%(result)s “% statusmsg
TypeError: format requires a mapping
Kindly help
Regards
Boniface
Boniface, simply change the first line to:
statusdata=”%s ” % statusmsg
Thanks for any other informative website. Where else may I get that type of info written in such a perfect approach? I have a mission that I’m simply now running on, and I’ve been at the glance out for such info.
Great blog here! Also your web site a lot up fast! What host are you the use of? Can I get your affiliate link on your host? I wish my site loaded up as fast as yours lol
I am not sure the place you are getting your information, however good topic. I needs to spend a while studying much more or working out more. Thanks for excellent information I used to be on the lookout for this information for my mission.
I’m getting an error after fixing the “%s” %statusmsg line.
complains that for that line, “not all arguments converted during string formating”.
Any suggestions?
I know I’m a bit late to offer help, but I had the same probleems as you. I fixed most of them by changing a few lines.
droid.getInput(…..
change it to
droid.dialogGetInput(…
, then
statusdata = “%s” % statusmsg
To
statusdata = statusmsg.result
Or just add .result to then end of input and change the variable to statusdata, omitting statusmsg altogether. Hope that helped even if just for my own curiosity
Fantastic goods from you, man. I have understand your stuff previous to and you are just too magnificent.
I actually like what you have acquired here, really like what
you’re stating and the way in which you say it. You make it enjoyable and you still take care of to keep it wise. I cant wait to read far more from you. This is really a wonderful site.
Thanks for putting this out here. I am just now trying it out, but without success. Does the script still work for others that it worked for before. My concern is if twitter is enforcing https and that could be my problem.