| « Decorations | Avada kedavra » |
Tue, Dec 06, 2011
![[Icon]](rsc/img/chain_link.gif)
If you spend much time chatting online, you'll have encountered URLs that are far too long to be nice in a conversation. It's nice to have an alternative, and that's what tinyurl.com and other such services do - give them a long URL, and they give you a short one that redirects to the original.
Very nice.. but a bit tedious. Instead of
You have to mess around with
Meh.
What you REALLY want is to press a button and it magically just does everything for you. And since the motto of Perl is "Making the easy things easy and the hard things possible" I figured it must be do-able, and in fact somebody else must have already done almost all the work for me.
I was right. The Perl module "Clipboard" and the 'curl' command-line app were all I needed to use Google's API to shorten URLs using the http://goo.gl service.
So now I have a keyboard shortcut to run a script. The script gets the URL from the clipboard, submits it to goo.gl, gets back the new short redirect, and copies it into the clipboard so I can paste it.
In other words, the new flow is:
Job done!
It's useful enough to me that I thought I'd shunt it online in case anyone else wanted it. So here you go!
Follow up:
#!/usr/bin/perl
# Take URL from clipboard & copy smaller redirect URL to clipboard
use strict;
use warnings;
use Clipboard;
# Ugly fixify of cliboard order
if ('Clipboard::Xclip' eq $Clipboard::driver) {
no warnings 'redefine'; *Clipboard::Xclip::all_selections = sub {
qw(clipboard primary buffer secondary)
};
}
# Get the long URL from cliboard
my $url = Clipboard->paste;
# Get short URL via curl
my $short_url = qx#curl -s -d'&url=$url' http://goo.gl/api/url#;
# Returns in the format {"short_url":"http://goo.gl/xxxx","added_to_history":false}
# Crop out everything but the URL
$short_url =~ s#^.*goo.gl([^"]+).*#http://goo.gl$1#;
# Put the short URL onto the clipboard so we can paste it
Clipboard->copy($short_url);
![[Links]](http://geekblog.oneandoneis2.org/skins/112/rsc/img/chain_link.gif)
Hmm.. new look for twitter? I hope it gets less "Ick! Change! Put it back!" nonsense than Facebook..
08/02/12
Facebook Syndication Error
22/02/12
![]()
I last listened to:
Johann Pachelbel - Canon in D major
Most recent photo:
Submersible houseboat