This feed contains pages in the "barnowl" category.

Christine Spang shortening URLs in barnowl

I post to identica through BarnOwl using Nelson's Twitter extension. This generally means I want to shorten URLs in my posts ("dents" as they're called on identica) so it's easier to fit them into the 140-character limit.

Luckily, BarnOwl can be extended using Perl, which, combined with some libraries from CPAN, makes this task simple. Here's the snippet I wrote:

package ShortenURLs;
use BarnOwl::Editwin qw(:all);
use Text::FindLinks;
use WWW::Shorten 'Bitly', qw(:short);

my $bitly_username = 'YOUR_BITLY_USERNAME_HERE';
my $bitly_apikey = 'YOUR_BITLY_APIKEY_HERE';

sub shorten_urls {
    my $text = save_excursion {
    my $linkified_text = Text::FindLinks::markup_links(
        text => $text,
        handler => sub {
            my ($url, $before, $after) = @_;

            my $short_link = q{};

            # hack around Text::FindLinks including trailing )
            # characters in URLs, which 99% of the time I don't
            # want, since it probably means I've surrounded the
            # link in parens
            if ($before =~ m/\($/ && $url =~ m/\)$/) {
                $url =~ s/\)$//;
                $short_link .= ")";

            return short_link($url, $bitly_username, $bitly_apikey).$short_link;


BarnOwl::new_command('shorten-urls' => \&shorten_urls);
BarnOwl::bindkey(edit => 'C-l' => command => 'shorten-urls');


You'll need a account, and to enter your username and API key from that account in the relevant locations. Paste the snippet into e.g. ~/.owl/, and add require "$ENV{HOME}/.owl/"; to your ~/.owlconf.

It has a sort of ridiculous dependency chain that I am too lazy to cull, which you'll need to install, probably using cpan. I recommend using local::lib if you're installing without root. (I found this to be a pretty good walkthrough). Installing WWW::Shorten::Bitly and Text::FindLinks should pull in all relevant dependencies.

And that's it! Hit C-l in the editwin and all the links in it will be replaced with shortened versions.

Posted Mon 12 Apr 2010 05:23:41 AM UTC Tags: tags/barnowl