| 1 | package WWW::Lengthen; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 1 | our $VERSION = '0.08'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 1 | our %KnownServices = ( | |||||||||||||||||||||||||||
| 10 | '0rz' => qr{^http://0rz\.tw/.+}, | ||||||||||||||||||||||||||||
| 11 | Metamark => qr{^http://xrl\.us/.+}, | ||||||||||||||||||||||||||||
| 12 | SnipURL => qr{^http://snipurl\.com/.+}, | ||||||||||||||||||||||||||||
| 13 | TinyURL => qr{^http://tinyurl\.com/.+}, | ||||||||||||||||||||||||||||
| 14 | snurl => qr{^http://snurl\.com/.+}, | ||||||||||||||||||||||||||||
| 15 | bitly => qr{^http://bit\.ly/.+}, | ||||||||||||||||||||||||||||
| 16 | htly => qr{^http://ht\.ly/.+}, | ||||||||||||||||||||||||||||
| 17 | isgd => qr{^http://is\.gd/.+}, | ||||||||||||||||||||||||||||
| 18 | owly => qr{^http://ow\.ly/.+}, | ||||||||||||||||||||||||||||
| 19 | urlchen => qr{^http://urlchen\.de/.+}, | ||||||||||||||||||||||||||||
| 20 | google => qr{^http://goo\.gl/.+}, | ||||||||||||||||||||||||||||
| 21 | ); | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | # Can't test, but widely used | ||||||||||||||||||||||||||||
| 24 | 1 | our %PartOfOtherServices = ( | |||||||||||||||||||||||||||
| 25 | twitterco => qr{^http://t\.co/.+}, | ||||||||||||||||||||||||||||
| 26 | hatena => qr{^http://htn\.to/.+}, | ||||||||||||||||||||||||||||
| 27 | jmp => qr{^http://j\.mp/.+}, | ||||||||||||||||||||||||||||
| 28 | tumblr => qr{^http://tmblr\.co/.+}, | ||||||||||||||||||||||||||||
| 29 | facebook => qr{^http://fb\.me/.+}, | ||||||||||||||||||||||||||||
| 30 | ); | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 1 | our %ExtraServices = ( | |||||||||||||||||||||||||||
| 33 | OneShortLink => [ qr{^http://1sl\.net/.+}, 'OneShortLink' ], | ||||||||||||||||||||||||||||
| 34 | Shorl => [ qr{^http://shorl\.com/.+}, 'Shorl' ], | ||||||||||||||||||||||||||||
| 35 | ); | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | # not only dead but also failed anyhow when I tested | ||||||||||||||||||||||||||||
| 38 | 1 | our %UnsupportedOrDeadServices = ( | |||||||||||||||||||||||||||
| 39 | icanhaz => qr{^http://icanhaz\.com/.+}, | ||||||||||||||||||||||||||||
| 40 | urlTea => qr{^http://urltea\.com/.+}, | ||||||||||||||||||||||||||||
| 41 | BabyURL => qr{^http://babyurl\.com/.+}, | ||||||||||||||||||||||||||||
| 42 | Linkz => qr{^http://lin\.kz/?\?.+}, | ||||||||||||||||||||||||||||
| 43 | TinyClick => qr{^http://tinyclick\.com/?\?.+}, | ||||||||||||||||||||||||||||
| 44 | V3 => qr{^http://\w+\.v3\.net/}, | ||||||||||||||||||||||||||||
| 45 | ShortenURL => qr{^http://www\.shortenurl\.com/.+}, | ||||||||||||||||||||||||||||
| 46 | URLjr => qr{^http://urljr\.com/.+}, | ||||||||||||||||||||||||||||
| 47 | qURL => qr{^http://qurl\.net/.+}, | ||||||||||||||||||||||||||||
| 48 | SmLnk => qr{^http://smlnk\.com/.+}, | ||||||||||||||||||||||||||||
| 49 | ShortLink => qr{^http://shortlink\.us/.+}, | ||||||||||||||||||||||||||||
| 50 | EkDk => qr{^http://add\.redir\.ek\.dk/.+}, | ||||||||||||||||||||||||||||
| 51 | MakeAShorterLink => qr{^http://tinyurl\.com/.+}, | ||||||||||||||||||||||||||||
| 52 | LinkToolbot => qr{^http://link\.toolbot\.com/.+}, | ||||||||||||||||||||||||||||
| 53 | haojp => qr{^http://hao\.jp/.+}, | ||||||||||||||||||||||||||||
| 54 | Smallr => qr{^http://smallr\.com/.+}, | ||||||||||||||||||||||||||||
| 55 | unu => qr{^http://u\.nu/.+}, | ||||||||||||||||||||||||||||
| 56 | Tinylink => [ qr{^http://tinylink\.com/.+}, 'Tinylink' ], | ||||||||||||||||||||||||||||
| 57 | durlme => qr{^http://durl\.me/.+}, | ||||||||||||||||||||||||||||
| 58 | NotLong => qr{^http://[\w\-]+\.notlong\.com/?$}, | ||||||||||||||||||||||||||||
| 59 | shadyurl => qr{^http://5z8\.info/.+}, | ||||||||||||||||||||||||||||
| 60 | miudin => qr{^http://miud\.in/.+}, | ||||||||||||||||||||||||||||
| 61 | ); | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | sub new { | ||||||||||||||||||||||||||||
| 64 | 0 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 0 | my %services; | |||||||||||||||||||||||||||
| 67 | 0 | ▶ | if ( @_ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 0 | foreach my $name ( @_ ) { | |||||||||||||||||||||||||||
| 69 | $services{$name} = $PartOfOtherServices{$name} | ||||||||||||||||||||||||||||
| 70 | 0 | ▶ | || $KnownServices{$name}; | ||||||||||||||||||||||||||
|
Condition0%0/3
$WWW::Lengthen::PartOfOtherServices{$name} || $WWW::Lengthen::KnownServices{$name}
MC/DC0%0/2
$WWW::Lengthen::PartOfOtherServices{$name} || $WWW::Lengthen::KnownServices{$name}
$WWW::Lengthen::PartOfOtherServices{$name}
$WWW::Lengthen::KnownServices{$name}
Truth table0%0/3
A || B
A= $WWW::Lengthen::PartOfOtherServices{$name}B= $WWW::Lengthen::KnownServices{$name}
| |||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | else { | ||||||||||||||||||||||||||||
| 74 | 0 | %services = (%KnownServices, %PartOfOtherServices); | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 0 | my $ua = LWP::UserAgent->new( | |||||||||||||||||||||||||||
| 78 | env_proxy => 1, | ||||||||||||||||||||||||||||
| 79 | timeout => 30, | ||||||||||||||||||||||||||||
| 80 | agent => "$class/$VERSION", | ||||||||||||||||||||||||||||
| 81 | requests_redirectable => [], | ||||||||||||||||||||||||||||
| 82 | ); | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 0 | bless { ua => $ua, services => \%services }, $class; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 0 | ▶ | sub ua { shift->{ua} } | ||||||||||||||||||||||||||
|
Subroutine
ua: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | sub try { | ||||||||||||||||||||||||||||
| 90 | 0 | ▶ | my ($self, $url) = @_; | ||||||||||||||||||||||||||
|
Subroutine
try: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 0 | my $new_url; | |||||||||||||||||||||||||||
| 93 | my %seen; | ||||||||||||||||||||||||||||
| 94 | 0 | my $max_try = 5; | |||||||||||||||||||||||||||
| 95 | 0 | ▶ | while ($max_try--) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 0 | $new_url = $self->_try($url); | |||||||||||||||||||||||||||
| 97 | 0 | ▶ | return $new_url if $new_url eq $url or $seen{$new_url}++; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$new_url eq $url or $seen{$new_url}++
MC/DC0%0/2
$new_url eq $url or $seen{$new_url}++
$new_url eq $url
$seen{$new_url}++
Truth table0%0/3
A or B
A= $new_url eq $urlB= $seen{$new_url}++
| |||||||||||||||||||||||||||||
| 98 | 0 | $url = $new_url; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | 0 | return $url; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | sub _try { | ||||||||||||||||||||||||||||
| 104 | 0 | ▶ | my ($self, $url) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_try: not called CC=12 SCAR=50.5
| |||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 0 | foreach my $name ( keys %{ $self->{services} } ) { | |||||||||||||||||||||||||||
| 107 | 0 | my $service = $self->{services}->{$name}; | |||||||||||||||||||||||||||
| 108 | 0 | ▶ | next unless $service; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 0 | ▶ | if ( ref $service eq 'Regexp' ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 110 | 0 | ▶ | next unless $url =~ /$service/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 0 | my $res = $self->ua->get($url); | |||||||||||||||||||||||||||
| 112 | 0 | ▶ | next unless $res->is_redirect; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 0 | my $location = $res->header('Location'); | |||||||||||||||||||||||||||
| 114 | 0 | ▶ | return $location if defined $location; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | elsif ( ref $service eq 'ARRAY' ) { | ||||||||||||||||||||||||||||
| 117 | 0 | my ($regex, $package) = @{ $service }; | |||||||||||||||||||||||||||
| 118 | 0 | ▶ | next unless $url =~ /$regex/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 0 | ▶ | $package = 'WWW::Shorten::'.$package | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | unless $package =~ /^WWW::Shorten::/; | ||||||||||||||||||||||||||||
| 121 | 0 | eval "require $package"; | |||||||||||||||||||||||||||
| 122 | 0 | ▶ | unless ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 0 | my $longer_url = eval "$package\::makealongerlink('$url')"; | |||||||||||||||||||||||||||
| 124 | 0 | ▶ | return $longer_url if defined $longer_url; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | 0 | return $url; | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | sub add { | ||||||||||||||||||||||||||||
| 132 | 0 | ▶ | my ($self, %hash) = @_; | ||||||||||||||||||||||||||
|
Subroutine
add: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 0 | %{ $self->{services} } = ( %{ $self->{services} }, %hash ); | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 1 | 1; | |||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | __END__ | ||||||||||||||||||||||||||||