django + twitter.com: automatic new statuses
January 14, 2010 | pythonauthor: Karol Zielinski | comments: 6 | views: 3137
Tags: microblogging, status, twitter
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.
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.
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 [...]
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
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.
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.
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.
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 [...]