Programming a twitter-bot using python – Part 1/2

Hello folks,

this is going to be my very first post on my blog written in english. I want to share with you my experience while I am learning python in a small little weekend project. The project is about writing a script using the scripting language python. The purpose of the script will be posting tweets on a twitter account – that is the plan so far.

So first of all, I am using Linux Ubuntu as OS in version 10.04 LTS šŸ˜‰ so getting python installed is quite easy. Open up a terminal (press Crlt + Alt + T simultaneously) or Alt + F2 and enter “gnome-terminal” or find it in the top menu > Applications > Accessories > Terminal.

Type in the following command, confirm and then provide your user password to execute the installation:

sudo apt-get install python

If you are not familiar with python yet you can find instructional tutorials here: http://docs.python.org/tutorial/

Basically python is quite easy and very similar to a lot of other programming languages, but I don’t want to go into the details here, that will maybe the topic of another post šŸ˜‰ I rather would like two things the came to my eyes in the last 3 days since I started working with python:

  1. Python is very strict with indentions of source code blocks
  2. the if statement is somehow wired

I would like to illustrate that in a short example:

x = 1
if x > 0:
   print "Hallo Welt"

The condition of the if statement does not have parenthesis BUT a colon in the end and the following code block needs to be indented with a tabulator.

Ok, but now let us continue with the project. To work with twitter you have to use the API provided by them, here is the link to the development webpage of twitter: https://dev.twitter.com/

So I want to utilize a predefined library to access the twitter API. I found this one: Python-Twitter and it seems to be the kind what I was locking for. It requires me to install some additional software:

The first two are also available via apt-get so I use the repositories to install them. The third one requires also the installation of python-setuptools, so here is how you can install those libs via terminal:

sudo apt-get install python-setuptools python-simplejson python-httplib2

Having that on your machine you can download and unpack python-oauth2 from github. Open the folder in a termial and execute the following commands to get python-oauth2 compiled and installed:

python setup.py build
sudo python setup.py install

By now we already have something that should work. Lets try and read some twitter feeds using our brand new scripting skills. Open a terminal and start python in interactive mode by typing python. You see the command prompt of your python interpreter and it starts with “>>>”. Now do the following:

import twitter
client = twitter.Api()
recentposts = client.GetUserTimeline("username")
print [s.text for s in recentposts]

These four commands are doing the following:

  1. imports the just installed python wrapper for the twitter API
  2. creates a client to which we are going to send our commands which will be executed
  3. send a command to the client, here we want to get the last recent posts of a specified user and we store the result in a variable
  4. print the content of the result to the terminal

Getting writing access is the next thing our program should be capable of, but this requires authentication. We want to use the OAuth method to authenticate toward twitter, but to do so we first have to register our application on the twitter web page to get our consumer key and consumer secret. Go to this web page: https://dev.twitter.com/ and klick on the link in the middle: Register an app. On the next page you have to provide some information. For Application Type select Client and we want to have Read & Write access as Default Access Type. You have to provide a Applications Website (just anyone – for testing purpose it doesn’ t really matter), enter the CAPTCHA, accept the terms of usage and you are done šŸ™‚

The following page shows you the information necessary to establish a authenticated client connection – lets test that šŸ˜€ Find your consumer key and consumer secret in the OAuth 1.0a section and the Access Token as well as the Access Token Secret via the menu on the right. The command to set up a authenticated client is this:

import twitter
client = twitter.Api(consumer_key='twitter consumer key',
                     consumer_secret='twitter consumer secret',
                     access_token_key='the_key_given',
                     access_token_secret='the_key_secret')

Now we have to reinitialize our client in the way described above. Having that the client is now allowed to post to our twitter account. Use the command

status = client.PostUpdate('Yes, it works')

to send this tweet to your timeline. You can easily verify if it has worked by login into your twitter account via web interface and checking your timeline.

Ahh well, that’s it for the first part. To sum up, we now have the ability to read and write to a twitter account using python. In the next post I will put all this together to a small script that can read some kind of input from a text file e.g. and post it to a twitter account – something like that šŸ˜›

So see you tomorrow to the second part of my weekend project.

Advertisements

One thought on “Programming a twitter-bot using python – Part 1/2

  1. Hi there, you have written a nice clear manual for this task. It is easy to understand if…. yes, if if you are a computer specialist. For non-specialists it is hard to understand, but I think you are living in a information processing world where all these terms are quite common. Nevertheless, I like to read your texts. Please continue.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s