Interfacing with Twitter 4

Posted by Gian Zas on March 31, 2009

twitter_birdTweet tweet tweet! All the farm is chating about Twitter, all the farm is using twitter and all the farm is building applications on top of it.

Luckily, Twitter seems to be a programmer’s best friend, why? you may be asking… because of its wonderful API. Well designed and documented, what else could you wish?

The Twitter API is RESTful, but it doesn’t only mean that you can access to it over HTTP, sending parameters in GET or POST requests, it also means that it is designed complying with the principles of REST. A plus to this API is that it supports JSON, XML, RSS and Atom data formats and user authentication is reached by Basic Auth.

Due to the popularity of the service, a bunch of libraries have been developed and are heavily used to interact with Twitter, but if you like to explore the other end of the telephone line you are reading the right post ;) . As we’ve said the API is accessible through HTTP, so let’s go and fire up your browser and point to:

http://twitter.com/statuses/user_timeline.xml?screen_name=twitterapi

Ok, what do you see? the last tweets of the user whose screen name is twitterapi formatted in xml. Another popular operation is:

http://twitter.com/statuses/friends_timeline.json?

That url returns (after successful authentication) the latest tweets posted by the authenticated user and it’s friends.

Because requesting a URL and satisfying basic authentication mechanisms are easy tasks in the day-to-day programming languages, invoking the API is a piece of cake. As Python has been one of our favorite languages, we’ll show you how to retrieve user statuses and post a new one. Here we go!

import simplejson as json
import urllib
import urllib2
GET_STATUS_URL = 'http://twitter.com/statuses/user_timeline.json?'
UPDATE_STATUS_URL = 'http://twitter.com/statuses/update.json?'

First we must declare the necessary imports (simplejson must be downloaded and installed before) and declare the URLs to be accessed.

def get_statuses(screen_name):
    data = {'screen_name' : screen_name}
    url = GET_STATUS_URL + urllib.urlencode(data)
    f = urllib2.urlopen(url)
    response = ''.join(f.readlines())
    return json.loads(response)

get_statuses retrieves the last tweets of #{screen_name}, it simply adds the screen_name parameter to the url, then requests it and finally parse the response using the json library. To print the tweets retrieved in a simple manner:

    statuses = get_statuses(username)
    for status in statuses:
        print "# " + status['user']['screen_name'] + ": " + status['text']

To post a tweet we simply do a POST request to the UPDATE_STATUS_URL passing the status (the tweet’s text) parameter.

def update_status(username, password, status):
    __authenticate(username, password)
    data = {'status' : status}
    f = urllib2.urlopen(UPDATE_STATUS_URL, urllib.urlencode(data))
    response = ''.join(f.readlines())
    return json.loads(response)

Username and password corresponds to the twitter user (you for example) whose status will be updated, obviously the user (ex: you again) must have a twitter account.

Because the API uses basic authentication we call to the __authenticate function:

def __authenticate(username, password):
    passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
    passman.add_password(None, TWITTER_HOST, username, password)
    auth_handler = urllib2.HTTPBasicAuthHandler(passman)
    opener = urllib2.build_opener(auth_handler)
    urllib2.install_opener(opener)

For a good explanation on how this chunk of code works, please refer to  this great tutorial

As you’ve seen, in a small amount of code we are interacting with Twitter, where to use it, how to display the results and what ideas bring
to life is up to you!

[Post to Twitter] Tweet This Post 

Algorithm for the generation of a soccer (or any sport or event) fixture

Posted by Pablo Ifran on March 27, 2009

soccer2What’s the problem?

We wish to create a fixture in which every team will face every other team one single time. It is also required that each team will only play one single match in each of the defined rounds. It’s a classic tournament or league scenario.


Algorithm description

The first thing we must do is to see if the amount of teams is odd or even. In case it is odd, an additional team needs to be created which will be used as a “free day”, the team that has a match against it will not play in that round.

Once we have all the teams we must arrange them in the following way:

  • First round

    • The first with the last

    • The second with the penultimate

    • And so on until every team is assigned

  • To generate the following rounds, we must take the last element of the previous one (the last team of the first round order) and the last team (team number 6 if there are 6 teams) and we arrange them in the following way:

    • If the round we are generating is even (First one is 1, so it is always odd) we must take the first team of the previous round and then the last team. On the contrary, if it’s even, the last team goes first and then we put the last team of the previous round.

Note: last team = 6 if there are 6 teams. Last team of previous round = 3 if the round was 6 vs 5, 1 vs 4 and 2 vs 3

    • Then we must remove the teams that were first in the previous round, keeping the same order.

    • Once we have this list, we put the eliminated teams at the beginning of it.

    • Beginning from the end, we take two teams from this list and we put them at the front. We do this with every pair until we go through the entire list.

  • We keep the process until every round is generated.

Example


We have 6 teams, so we will generate 5 rounds with 3 matches each.

Teams – 1, 2, 3, 4, 5 y 6

First round

1 vs 6, 2 vs 5 and 3 vs 4

Second round

We take the last team of the previous round (team 4) and the last team (team 6), since the round is par we must put first 6 and then 4, the result is 6 vs 4.

We remove 6 and 4 from the list of the first round (1, 6, 2, 5, 3 and 4 ) and we sep the resulting order, which results in 1, 2, 5 and 3. We take the last two elements of this list (5 and 3) and we put them after 6 and 4. Finally, we add 1 and 2 to the end of the list:

6 vs 4, 5 vs 3 and 1 vs 2

Third round

Applying the same as in the previous round:

2 vs 6, 3 vs 1 and 4 vs 5

Fourth round

6 vs 5, 1 vs 4 and 2 vs 3

Fifth round

3 vs 6, 4 vs 2 and 5 vs 1


[Post to Twitter] Tweet This Post 

How to resolve mystic problems? 1

Posted by Ariel Ludueña on March 15, 2009

rubber duckA few days ago, my friend Gian sent me a very interesting link that I’d like to share with you.

The post describes a technique to solve complex problems or what I usually call “mystical problems”.

This technique is called by the author: “Rubber Duck method of debugging” and seems to work very well. I have tested this with a fellow programmer instead of a Rubber Duck and works!

Here you have the technique and the reference link:

There is an entire development methodology (whose name escapes me at the moment) that makes use of that very phenomenon.

We called it the Rubber Duck method of debugging.  It goes like this:

1) Beg, borrow, steal, buy, fabricate or otherwise obtain a rubber duck (bathtub variety)

2) Place rubber duck on desk and inform it you are just going to go over some code with it, if that’s all right.

3) Explain to the duck what you code is supposed to do, and then go into detail and explain things line by line

4) At some point you will tell the duck what you are doing next and then realize that is not in fact what you are actually doing.  The duck will sit there serenely, happy in the knowledge that it has helped you on your way.

Works every time.  Actually, if you don’t have a rubber duck you could at a pinch ask a fellow programmer or engineer to sit in.

Andy

Reference link :)

[Post to Twitter] Tweet This Post 


Tweet This Post links powered by Tweet This v1.3.9, a WordPress plugin for Twitter.