Skip to content

Commit

Permalink
Add Unshorten plugin to follow redirects from known short URLs. Curre…
Browse files Browse the repository at this point in the history
…ntly only knows t.co.
  • Loading branch information
Stuart A Johnston committed Aug 28, 2011
1 parent 6147e28 commit 02e4068
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions 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

0 comments on commit 02e4068

Please sign in to comment.