Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Unshorten plugin to follow redirects from known short URLs. Curre…
…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.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |