... because from time to time I'm a web developer, too
About me
Projects
Contact
Links

django + twitter.com: automatic new statuses

January 14, 2010 | python
author: Karol Zielinski | comments: 6 | views: 3137
Tags: , ,

Earlier we created functionality for automatic adding of statuses to blip.pl. Today we would like to do the same thing, however for adding statuses to twitter.com. It will be based on twitter API, so without any “hacks”.

 

Everybody knows what is twitter, so I won’t describe it here. I just want to give you a link to official documantation of twitter’s API: http://apiwiki.twitter.com/Twitter-API-Documentation.

We will use only one method from this API, so we need to read “Twitter REST API Method: statuses/update” section in this documentation. You can find this section here.

I prefer JSON format, than XML in communication with any API.

So, let’s fun.

Open *.py file with your model and change “save()” method:

def save(self):
    if not self.id:
        import urllib2, urllib

        twitter_status = self.title + ' http://our_domain_name/' + str(self.slug)

        request = urllib2.Request('http://twitter.com/statuses/update.json')
        request.headers['Authorization'] = 'Basic %s' % ( base64.b64encode('TWITTER_USERNAME:TWITTER_PASSWORD'),)
        request.data = urllib.urlencode({'status': twitter_status})
        response = urllib2.urlopen(request)

and that’s all.

After you will add new item to this model – message with title of an item (self.title) and link to this item (self.slug) will be added to your account on twitter.com.

Easy, isnt’t it?

Btw. Yes. I know that base64 is not as safe, as oAuth. However this way is much faster to implement.

Bookmark and Share
Post django + twitter.com: automatic new statuses to develway Post django + twitter.com: automatic new statuses to Delicious Post django + twitter.com: automatic new statuses to Digg Post django + twitter.com: automatic new statuses to Facebook Post django + twitter.com: automatic new statuses to Reddit Post django + twitter.com: automatic new statuses to StumbleUpon

Related news and resources

Comments (6)

4Avatars v0.3.1 v0.3.1
django + twitter.com: automatyczne statusy - develway.pl - wiadomości dla programistów, wiadomości IT, świeże linki ze świata IT
January 14, 2010, 7:41 am

[...] tech.karolzielinski.com Follow us on Twitter 1,521 śledzących RSS Feed 173 czytelników django + twitter.com: automatyczne statusy 1 głosuj! Jak zaimplementować (w django) funkcjonalność, która automatycznie będzie [...]

4Avatars v0.3.1 v0.3.1
John Boxall
January 14, 2010, 9:15 am

Whenever you’re communicating with other services, remember to consider how your code will behave when that service goes down.

If you’re not catching exceptions from HTTP errors and handling them gracefully you can be opening yourself to all kinds of nasty errors.

For example, let’s say you’re pulling Twitter status updates onto your homepage – what happens if Twitter goes down? Does your homepage go down too because you forgot to catch the HTTP error thrown by urllib2?

Always wrap this kind of thing in a try/except and have a fallback!

Cheers,

John

4Avatars v0.3.1 v0.3.1
Karol Zielinski
January 14, 2010, 1:30 pm

@John

I totally agree! However in this example I wanted to show the easiest possible way to communicate with twitter. That’s why I don’t cache any exceptions, check response status, etc.

However in real, production environment – I agree. It has to be done like you just said.

4Avatars v0.3.1 v0.3.1
Thomas
January 15, 2010, 7:23 am

Check out Tweepy – http://github.com/joshthecoder/tweepy. It’ll save you a lot of time if you decide to add additional Twitter integration later.

4Avatars v0.3.1 v0.3.1
Karol Zielinski
January 16, 2010, 4:25 am

@Thomas

I agree. In more complicated examples it’s much better to use external library. However in easy examples – it’s not necessary to include huge libraries into our code.

4Avatars v0.3.1 v0.3.1
Download a number of your twitter followers in python
January 18, 2010, 5:01 am

[...] already know how to implement functionality for automatic adding statuses to twitter. Today we will implement a functionality for automatic downloading a number of your twitter [...]

Write a comment

Karol Zielinski :: Just a tech stuff Hello, I'm Karol Zielinski, internet evangelist, an entrepreneur, project manager and a web developer from Gdynia, Poland. I like creative design, good advertisement, social media and all kind of stuff around the web.

Most popular posts

Much more links

Karol Zielinski    |   contact me
Gdynia, Poland
RSS - Just a tech stuff - python, java blog - web development blog Karol Zielinski on twitter Karol Zielinski on LinkedIn Karol Zielinski on facebook Karol Zielinski on delicious Karol Zielinski on digg Karol Zielinski on flickr Karol Zielinski on stumbleupon Karol Zielinski on technorati