Twitting with Python

I would like to start this note with a fair disclaimer to all those who might know me and my stand about “social networking” that cropt in recent years:

“The only reason why I finally surrendered to so-called micro-blogging is my new phone. That does not mean that my opinion about “twittering” has changed a bit: I still feel pity for all those dummies out there that “follows” others on twitter. Only Facebook sheeps ranks lower in my book :-). That said, I do recognize that micro-blogging sites with well documented API might be useful for pure technical utilization. That’s why I opened account on Twitter that will serve to me and only me. Instead of sending alerts to my phone via SMS messages I’ll write a simple python script that will post “statuses” to my twitter account via API. And since my HTC Hero comes with configured Peeps, client application for Twitter, I decided to give it a try, so I opened “non-public” twitter account where python script will send various non-critical technical alerts. Of course, messages that I’m intending to send to will be so obscure that only I and perhaps a couple of my associates will be able to put in the context. Not a chance that any of the messages could jeopardize my client security in any way, even if someone unauthorized could see them.

Enough rumblings, let’s see how easy it is to exploit for some productive work.

With some help from the Google I decided to use python-twitter module.

Steps to install python-twitter and a prerequisite, simplejson:

1) download and install appropriate version of setuptools from

I downloaded and installed setup tools for python 2.6 win32..

This is a prerequisite for simplejson (setuptools version >= 0.6c7).

2) download and install simplejson

– after extracting simplejson-2.0.9.tar.gz to some temporary directory:

cmd> cd simplejson-2.0.9
cmd> python install

3) download and install Python Twitter

– after extracting python-twitter-0.6.tar.gz to some temporary directory:

cmd> cd python-twitter-0.6
cmd> python build
cmd> python install
cmd> python test

4) read documentation about twitter module API

5) let’s see python-twitter module at work:

# Simple test case to check if python twitter module works.
import twitter

# Twitter login account and password
tusername = 'dbatwitaccount'
tpassword = 'mysecret'

# Initialize authenticated twitter API session
api = twitter.Api(username=tusername,password=tpassword)

def PostTweet(message):
    Function for posting a message. 
    if len(message) < 141:
        status = api.PostUpdate(message)
        print ('Error: message is longer than 140 characters!')

def PrintUserTweets(tusername, cnt=0):
    Simple function for printing tweets. You can limit the
    number of tweets with the cnt parameter, default is 0
    which means get and print all tweets.
    statuses = api.GetUserTimeline(user=tusername, count=cnt)
    for status in statuses:
        print (status.created_at + ': [' + status.text + ']')

def DeleteAllTweets(tusername):
    Function for deleting all tweets - use with care!
    i = 0
    statuses = api.GetUserTimeline(user=tusername, count=0)
    for status in statuses:
        i = i + 1
    print ("Number of deleted tweets: " + str(i))

# posting a tweet -- in real life scenario DBA should
# be careful to post only obscure enough messages,
# not to reveal such details as SID, service names,
# hostnames, IP's, account names, even errors...
# The example below is only an example, in real-life
# I would not post ORA- message number nor DB name!
PostTweet('ORA-600 @ DB1 / check alert.log /')
# print all user tweets

# print limited number of tweets (for example last two)
PrintUserTweets(tusername, 2)

# delete all tweets

# End

With above script I only glimpsed over twitter API that is exposed through python twitter module. Refer to documentation for a complete overview.

Posted on 01.11.2009, in Scripting and tagged , . Bookmark the permalink. Comments Off on Twitting with Python.

Comments are closed.