MT-Twitter and Shortening URLs

I’ve been using the MT-Twitter plugin for a while now, so that when updating the MissingLinks page, a corresponding twitter posting is made. It used to be using to shorten the URLs (prior to the big server crash, I was using, but lost all my code when the RAID controller on the server failed). Sometime in mid-November, the URLs just disappeared, making me thing something went wrong on twitter’s API. Either that, or something went wrong on my server, which I hadn’t even touched that month.
So, I looked around for some suitable Perl code to re-introduce the API calls that mysteriously went missing. The answer was a chunk of code from (now apparently defunct). I’m reposting it here, so it might help someone out (myself included — I’m backing stuff up properly now).
The script just needs a few extra lines. I inserted this code just after the “my $intro” line in the _update_twitter function:
my $bitly = LWP::UserAgent->new;
my $url_response =
    $bitly->get(“” . $obj->permalink);
my $small_url;
if($url_response->is_success) {
   $small_url = $url_response->content;
} else {
   $small_url = $obj->permalink;

Then you have to update the part of the script which creates the twitter message. Replace the original $text string with the $small_url string:
# my $text = $intro . $entry->title . ‘ ‘ . $obj->permalink;
my $text = $intro . $entry->title . ‘ ‘ . $small_url;

Presumably, you can use any URL shortener API you want — just insert the proper API call. Or, if you’re enterprising, make your own URL shortener. That way, you own all the links.
Someone even came up with an idea to use .htaccess 301 redirects to bounce a link ending with <mt:EntryID> to the proper <$mt:EntryLink$>. Of course, this really only works if your domain name is short to begin with.

One Response to “MT-Twitter and Shortening URLs