“Professional service,
that won't make you nervous”

Macintosh  Perl  Linux

WWW::Mechanize::Tinyurl

An Object-oriented Perl module to streamline access to tinyurl.com

Download the script

Fully CPAN-ready installable package, Includes an Object-Oriented module, documentation, and example scripts for commandline usage, and within XChat as a plugin.

WWW::Mechanize::TinyURL - Perl extension for accessing TinyURL.com via a scripted interface

SYNOPSIS

    use WWW::Mechanize::TinyURL;
	my $url     = 
	'http://www.someplacespecial.com/withlots.php?of=long&line=noise'
	my $urlref  = WWW::Mechanize::TinyURL->new( $url );
	my $tinyurl = $urlref->maketiny();

or you can just do it all in one go, thusly:

    # take the url passed on the commandline
    die "No URL Specified" unless $ARGV[0];
    # spit out a shorter link
    print WWW::Mechanize::TinyURL->new( $ARGV[0] )->maketiny();

DESCRIPTION

Basically this provides a quick and easy method of grabbing a shorter link in a scripted environment.

For example, you may wish to obfuscate certain URLs on your website.

Or in a IRC chat environment, having a bot that returns a link from an HTML validator that's all long and ugly obscures its true purpose, whereas passing it thru this package first results in far shorter output and still gets the job done.

Originally this script was done for just such a purpose, and then refactored to be Object-oriented for practice, and then packaged up as you see it here due to its general usefulness.

By way of example from a live chat session, which do you think gets the point across more easily?

 <WebDragon> `validate http://www.amazon.com/exec/obidos/tg/stores/
 artist/glance/-/222413/ref=pd_ap_sr/104-3932366-9810311

 <GwaiLo> here goes 29 lines of my screen :(

 <miniE> The URL http%3A%2F%2Fwww.amazon.com%2Fexec%2Fobidos%2Ftg%2F
 stores%2Fartist%2Fglance%2F-%2F222413%2Fref%3Dpd_ap_sr%2F104-3932366
 -9810311i contains invalid content with 519 errors. Please see 
 http://validator.w3.org/check?uri=http%3A%2F%2Fwww.amazon.com%2Fexec
 %2Fobidos%2Ftg%2Fstores%2Fartist%2Fglance%2F-%2F222413%2Fref%3Dpd_ap
 _sr%2F104-3932366-9810311

See ? It’s Horrible! Totally obscures the error message in the result. Far better to see:

 <miniE> The URL http://tinyurl.com/6r4x4 [www.amazon.com] contains 
 invalid content with 519 errors. Please see http://tinyurl.com/5vfyl 
 [validator.w3.org] for validation results.

SEE ALSO

WWW::Mechanize, HTML::Parser, HTTP::Response, and LWP, for further details of the methods used herein

WWW::Shorten, CGI::Shorten, for additional modules that do similar things. In particular, WWW::Shorten does the same thing as this module and other things besides, however I wanted them done in a particular way, and picked my own path. Perl allows this.

AUTHOR

Scott R. Godin, <nospam (at) webdragon (dot) net>

COPYRIGHT AND LICENSE

Copyright (C) 2005 by Scott R. Godin

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.5 or, at your option, any later version of Perl 5 you may have available.

Module interface code

  1  package WWW::Mechanize::TinyURL;
  2
  3  use 5.006001;
  4  use strict;
  5  use warnings;
  6
  7  our $VERSION = '1.00';
  8
  9  use WWW::Mechanize 1.08;
 10  use HTML::TokeParser 2.30;# part of HTML::Parser 3.45
 11
 12  sub new {
 13      my ($class, $arg) = @_;
 14      bless { _url => $arg }, $class;
 15  }
 16
 17  sub _get_url { $_[0]->{_url} }
 18
 19  sub _get_result {
 20      my ($self, $contentref) = @_;
 21      # set up a token parser for the content
 22      my $parser = HTML::TokeParser->new( $contentref ); #content already a scalar reference here
 23      # extract tags from HTML::TokeParser object until we get an input tag, and grab its contents
 24      while (my $tagref = $parser->get_tag('input') )
 25      {
 26          # loop til EOF or until we find an input tag that matches the one we want, and return its value
 27          next unless $tagref->[1]->{name} eq 'tinyurl';
 28          return $tagref->[1]->{value};
 29      }
 30      # bummer, we couldn't find the tag we wanted out of all that html content, so return the original url
 31      return $self->_get_url();
 32  }
 33
 34  sub maketiny {
 35      my ($self) = @_;
 36      my $mech = WWW::Mechanize->new();
 37      # pretend to be firefox because we're all for Firefox's mindshare going up
 38      $mech->{agent} = "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.5) Gecko/20041111 Firefox/1.0";
 37      $mech->get( 'http://tinyurl.com' );# load up our tinyurl website
 38
 39      # submit our url to their mixing engine
 40      $mech->submit_form(
 41          form_number => 1,
 42          fields      => { url => $self->_get_url() },
 43          button      => 'submit',
 44      );
 45
 46      if ( $mech->success() )# if we succeed somehow
 47      {
 48          # $mech->response() is now an HTTP::Response object
 49          # so decode it, 
 50          my $htmlcontent = $mech->response()->decoded_content();
 51          # and either return our tiny url via parsing the returned content for the html token, 
 52          # containing the data we want, or the original url if the first fails for whatever reason
 53          return $self->_get_result(\$htmlcontent);
 54      }
 55      else {
 56          # somehow our request to tinyurl.com itself failed, (website down?) 
 57          # so return the un-tiny url, don't just die uselessly
 58          return $self->_get_url();
 59      }
 60  }
 61
 62  1;