NgenTwit – Tweet like no one follows

Well, the “description” part of the title is not a feature at all of a complete application. That’s merely my lack of knowledge in Python, and definitely I don’t have enough time to learn Python. As an old ode says “there isn’t `too late to learn`” and I do believe in my personal inner intelligent and spirit for the sake of the harmony of this universe … I started to learn Python.

As my usual learning scheme, I can’t start if there is no challenge. So, I did challenge myself to write a new twitter desktop application. That was the birth of NgenTwit (credit to http://twitter.com/3072 for the name). The first thing came into my mind is, this application must have a nice GUI. Then, it has the ability to do automatic tweet. For the former spec, choice is PyGTK.

I don’t want to have a big confusion in the beginning, so I look for Python twitter framework, and I choose Tweepy. From it’s documentation, it is clear, clean, and easy to use. Then I start to write.

The first thing to do is to make sure that Tweepy is correctly installed, please refer to Tweepy documentation. You will need Tweepy-example package too, to test Tweepy installation.

Next is twitter related administration. Twitter provide two types of authentication; Basic auth and OAuth. Tweepy provides a very cool API to use respectively. For my case, I will use OAuth. It allows me to tell me follower from where I send my tweet.

Assume that I have an application registered to twitter, I will have consumer key and consumer secret as follow:

consumer_key = "xxxxxxxx"

consumer_secret = "yyyyyyyy"

Each user need access token and access token key. This is persistent once you are authenticated. We can get them by using one of tweepy example: tweepy-examples/oauth/getaccesstoken.py

So now I have four variables needed to access Twitter API; consumer_key, consumer_secret, access_token_key, access_token_secret.

First, I will need Twitter to authenticate me.

 auth = tweepy.OAuthHandler("xxxxxxxx", "yyyyyyyy")

as we have access_token_key and access_token_secret, these variables are set to Tweepy as follow:
assumes their values are “aaaaaaaa” and “bbbbbbb” respectively.

auth.set_access_token("aaaaaaaa", "bbbbbbb")

then initialize Tweepy API with this authenticated access token.

api = tweepy.API(auth)

Now I can do anything I want thanks to tweepy.API.

Now, done with twitter stuffs, I am ready to write the GUI. I am using PyGTK. This is not a rich GUI, it is merely intended to catch my tweet and send it to Twitter once I press return. No way to see the timeline, replies, friends, etc. So, basically only two things needed.
1. User input
2. update_status of Tweepy API

The result is as simply as follow:

Here is the code:

#!/usr/bin/env python
# ngentwit.py

import pygtk
pygtk.require('2.0')
import gtk
import tweepy

class Ngentwit:
    def enter_callback(self, widget, entry):
        entry_text = entry.get_text()
        entry.set_text("Sending to twitter...")
        auth = tweepy.OAuthHandler("xxxxxxxx", "yyyyyyyy")
        auth.set_access_token("aaaaaaaa", "bbbbbbb")
        api = tweepy.API(auth)
        api.update_status(entry_text)
        entry.set_text("")

    def __init__(self):  # create a new window
        window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        window.set_size_request(300, 50)
        window.set_title("NgenTwit")
        window.connect("delete_event", lambda w,e: gtk.main_quit())
        vbox = gtk.VBox(False, 0)  window.add(vbox)
        frame = gtk.Frame("Tweet? No one follows")  

        entry = gtk.Entry()
        entry.set_max_length(140)
        entry.connect("activate", self.enter_callback, entry)
        entry.select_region(0, len(entry.get_text()))  

        frame.add(entry)
        vbox.pack_start(frame, False, False, 0)
        vbox.show()
        window.show_all()

def main():
        gtk.main()
        return 0

if __name__ == "__main__":
        Ngentwit()
        main()