From 02e40686980dcf7b7d37d6e732fa788d94a2f3cc Mon Sep 17 00:00:00 2001 From: Stuart A Johnston Date: Sat, 27 Aug 2011 22:43:56 -0500 Subject: [PATCH] Add Unshorten plugin to follow redirects from known short URLs. Currently only knows t.co. --- lib/Text/AutoLink/Plugin/Unshorten.pm | 40 +++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 lib/Text/AutoLink/Plugin/Unshorten.pm diff --git a/lib/Text/AutoLink/Plugin/Unshorten.pm b/lib/Text/AutoLink/Plugin/Unshorten.pm new file mode 100644 index 0000000..4f305fc --- /dev/null +++ b/lib/Text/AutoLink/Plugin/Unshorten.pm @@ -0,0 +1,40 @@ +package Text::AutoLink::Plugin::Unshorten; +use strict; +use warnings; +use base qw(Text::AutoLink::Plugin); + +use LWP::UserAgent (); +my $ua = LWP::UserAgent->new; + +sub process +{ + my $self = shift; + my $ref = shift; + + $$ref =~ s/https?:\/\/t\.co\/([a-z0-9]+)/ + $self->unshorten($$ref) + /gex; +} + +sub unshorten +{ + my $self = shift; + my $url = shift; + + my $request = HTTP::Request->new(HEAD => $url); + my $response = $ua->request($request); + + if ($response->redirects) { + return $response->request->uri; + } + + return $url; +} + +1; + +=head1 NAME + +Text::AutoLink::Plugin::Unshorten - Follow redirects to unshorten + +=cut