Categories
Technical

You know when you’ve been Twango’d

You take one Django app (this monstrosity, which I mockingly label “my blog”), you take one completely exhausted Ruby On Rails app with a nice simple API but as much stability as a menstruating Dr Jekyll (in this case, Twitter) and what do you get?

Well I kinda ruined it with my hilarious entry-title, but yeah – Twango.

Why I love Twitter

Twitter could very well be my illicit lover, I only have like three “friends” – but that matters none. I still feel the need to tell them what I’m cooking, reading, programming, and why I hate Gillian McKeith (she’s a pretend doctor who dissects people’s poo on TV – who needs any more excuses?) along with stuff that I find to be funny in the empty landscape that is my head (so yes, I am a sadist).

The downside is, of course, that this goes both ways – you get the doings of the people you have subscribed to (it’s only nice to return the favour, isn’t it?) broadcast live to your phone via text message. This has led to some people finding the service “intrusive” and turning the alerts off. I quite like it – generally people on my list have been pretty sensible with their updates and it keeps the (non-salacious) voyeur in me happy. But you know – horses for courses and all that.

But it’s not really all that useful, is it?

That’s what my girlfriend said. (BUH-DUM..TSH!) No really – she did. Only for her to sign up and send this twitter the VERY next day. She’s not used it since, but it’s served as something to sling in her face if she ever moans about my incessant Twittering. “You’re always on your phone!” “Well you lost yours!” – that’ll show her.

Anyway, what we have here is a very nice alerting tool for absolutely nothing. We can use its aforementioned “intrusiveness” for many many things: This guy hooked up his doorbell, Gareth Rushgrove has mentioned the idea at work of hooking it into CruiseControl to tell us when a build has gone awry, and now I’ve hacked together a little something which alerts me whenever somebody posts something on my blog.

So THAT’S where Django comes in…

Django makes such alerting a breeze. All you need to do is override the model in question’s save method so that it sends a Twitter to your twitterbot anytime it is called. Then as long as you are subscribed to your twitterbot’s feed, you’ll get a twitter to your phone shortly after.

It’s proper simple, and the code is pretty straight forward too. There are a bunch of python APIs out there already, but I used the simple posting function released by Matthew Boedicker, I only need that posting functionality, and this seemed ideal – just a couple of URLlib calls and you’re sorted. Just stick that somewhere (I’ve got a nice customised helpers file) and call it a bit like this in your model:


class Comment(models.Model):
   ultra hot model code
   def save(self):
      super(Comment,self).save()
      twitter_update("comment posted to " + self.blog_entry.slug + ": " + self.content)

And it should work nicely.

But couldn’t it all go bits up?

Yeah, there’s every chance that this is open to abuse. Twitter’s far from safe with the whole SMS spoofing thing, and even then people could totally kill your phone if you’re not careful – so obviously use it while acknowledging the risks involved. Luckily, Twitter makes it relatively simple to silence – everything arrives at your phone from one source, so you could easily shut it up – and there’s no stopping you implementing spam-protecting measures such as Askimet and Captchas (the first is definitely next on my BDT todo-list).

Feel free to tell me why I’m wrong, add me on twitter (I’d really liked to be added by people not trying to spam me for a change) or just plain ignore me – thanks for making it this far.