10 Jun 2008

TinyUrl Perl Script

It's been a while since my last post so I thought I'd write up a quick post about what I did today.

I've been using the TinyUrl service for some time now and I came across their API, so I though I'd knock up a quick 'n' dirty script to make my TinyUrling easier.

#!/usr/bin/perl -w
use strict;
use LWP;
my $input = $ARGV[0];
$input =~ s/^ht{2}p:\/{2}//;
my $url = 'http://tinyurl.com/api-create.php?url=http://'.$input;
my $browser = LWP::UserAgent->new;
my $response = $browser->get($url);
die "Can't get $url --", $response->status_line unless $response->is_success;
print $response->content."\n"


This script takes your input and prints out the TinyUrl of it, usage is as follows;
$ perl -w tinyurl.pl www.google.com

which gives an output of;
http://tinyurl.com/1c2

Not bad for about 5 minutes work, I hope you will agree.