line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package WWW::Shorten::TinyURL; |
2
|
|
|
|
|
|
|
|
3
|
7
|
|
|
7
|
|
130176
|
use strict; |
|
7
|
|
|
|
|
37
|
|
|
7
|
|
|
|
|
199
|
|
4
|
7
|
|
|
7
|
|
42
|
use warnings; |
|
7
|
|
|
|
|
19
|
|
|
7
|
|
|
|
|
183
|
|
5
|
7
|
|
|
7
|
|
80
|
use Carp (); |
|
7
|
|
|
|
|
28
|
|
|
7
|
|
|
|
|
142
|
|
6
|
|
|
|
|
|
|
|
7
|
7
|
|
|
7
|
|
32
|
use base qw( WWW::Shorten::generic Exporter ); |
|
7
|
|
|
|
|
11
|
|
|
7
|
|
|
|
|
3977
|
|
8
|
|
|
|
|
|
|
our $_error_message = ''; |
9
|
|
|
|
|
|
|
our @EXPORT = qw( makeashorterlink makealongerlink ); |
10
|
|
|
|
|
|
|
our $VERSION = '3.094'; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub makeashorterlink { |
13
|
3
|
100
|
|
3
|
1
|
1293
|
my $url = shift or Carp::croak('No URL passed to makeashorterlink'); |
14
|
2
|
|
|
|
|
5
|
$_error_message = ''; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
# terrible, bad! skip live testing for now. |
17
|
2
|
50
|
|
|
|
6
|
if ( $ENV{'WWW-SHORTEN-TESTING'} ) { |
18
|
2
|
100
|
|
|
|
7
|
return 'https://tinyurl.com/abc12345' |
19
|
|
|
|
|
|
|
if ( $url eq 'https://metacpan.org/release/WWW-Shorten' ); |
20
|
1
|
|
|
|
|
2
|
$_error_message = 'Incorrect URL for testing purposes'; |
21
|
1
|
|
|
|
|
2
|
return undef; |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
# back to normality. |
25
|
0
|
|
|
|
|
0
|
my $ua = __PACKAGE__->ua(); |
26
|
0
|
|
|
|
|
0
|
my $tinyurl = 'https://tinyurl.com/api-create.php'; |
27
|
0
|
|
|
|
|
0
|
my $resp |
28
|
|
|
|
|
|
|
= $ua->post($tinyurl, [url => $url, source => "PerlAPI-$VERSION",]); |
29
|
0
|
0
|
|
|
|
0
|
return undef unless $resp->is_success; |
30
|
0
|
|
|
|
|
0
|
my $content = $resp->content; |
31
|
0
|
0
|
|
|
|
0
|
if ($content =~ /Error/) { |
32
|
|
|
|
|
|
|
|
33
|
0
|
0
|
|
|
|
0
|
if ($content =~ /
|
|
|
0
|
|
|
|
|
|
34
|
0
|
|
|
|
|
0
|
$_error_message = 'Error is a html page'; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
elsif (length($content) > 100) { |
37
|
0
|
|
|
|
|
0
|
$_error_message = substr($content, 0, 100); |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
else { |
40
|
0
|
|
|
|
|
0
|
$_error_message = $content; |
41
|
|
|
|
|
|
|
} |
42
|
0
|
|
|
|
|
0
|
return undef; |
43
|
|
|
|
|
|
|
} |
44
|
0
|
0
|
|
|
|
0
|
if ($resp->content =~ m!(\Qhttps://tinyurl.com/\E\w+)!x) { |
45
|
0
|
|
|
|
|
0
|
return $1; |
46
|
|
|
|
|
|
|
} |
47
|
0
|
|
|
|
|
0
|
return; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub makealongerlink { |
51
|
4
|
100
|
|
4
|
1
|
3858
|
my $url = shift |
52
|
|
|
|
|
|
|
or Carp::croak('No TinyURL key / URL passed to makealongerlink'); |
53
|
3
|
|
|
|
|
6
|
$_error_message = ''; |
54
|
3
|
100
|
|
|
|
18
|
$url = "https://tinyurl.com/$url" |
55
|
|
|
|
|
|
|
unless $url =~ m!^https://!i; |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
# terrible, bad! skip live testing for now. |
58
|
3
|
50
|
|
|
|
8
|
if ( $ENV{'WWW-SHORTEN-TESTING'} ) { |
59
|
3
|
100
|
|
|
|
9
|
return 'https://metacpan.org/release/WWW-Shorten' |
60
|
|
|
|
|
|
|
if ( $url eq 'https://tinyurl.com/abc12345' ); |
61
|
1
|
|
|
|
|
2
|
$_error_message = 'Incorrect URL for testing purposes'; |
62
|
1
|
|
|
|
|
3
|
return undef; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
# back to normality |
66
|
0
|
|
|
|
|
|
my $ua = __PACKAGE__->ua(); |
67
|
|
|
|
|
|
|
|
68
|
0
|
|
|
|
|
|
my $resp = $ua->get($url); |
69
|
|
|
|
|
|
|
|
70
|
0
|
0
|
|
|
|
|
unless ($resp->is_redirect) { |
71
|
0
|
|
|
|
|
|
my $content = $resp->content; |
72
|
0
|
0
|
|
|
|
|
if ($content =~ /Error/) { |
73
|
0
|
0
|
|
|
|
|
if ($content =~ /
|
|
|
0
|
|
|
|
|
|
74
|
0
|
|
|
|
|
|
$_error_message = 'Error is a html page'; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
elsif (length($content) > 100) { |
77
|
0
|
|
|
|
|
|
$_error_message = substr($content, 0, 100); |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
else { |
80
|
0
|
|
|
|
|
|
$_error_message = $content; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
else { |
84
|
0
|
|
|
|
|
|
$_error_message = 'Unknown error'; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
0
|
|
|
|
|
|
return undef; |
88
|
|
|
|
|
|
|
} |
89
|
0
|
|
|
|
|
|
my $long = $resp->header('Location'); |
90
|
0
|
|
|
|
|
|
return $long; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
1; |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
=head1 NAME |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
WWW::Shorten::TinyURL - Perl interface to L |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
=head1 SYNOPSIS |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
use strict; |
102
|
|
|
|
|
|
|
use warnings; |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
use WWW::Shorten::TinyURL; |
105
|
|
|
|
|
|
|
use WWW::Shorten 'TinyURL'; |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
my $short_url = makeashorterlink('https://www.foo.com/some/long/url'); |
108
|
|
|
|
|
|
|
my $long_url = makealongerlink($short_url); |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=head1 DESCRIPTION |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
A Perl interface to the web site L. The service simply maintains |
113
|
|
|
|
|
|
|
a database of long URLs, each of which has a unique identifier. |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=head1 Functions |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=head2 makeashorterlink |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
The function C will call the L web site passing |
120
|
|
|
|
|
|
|
it your long URL and will return the shorter version. |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=head2 makealongerlink |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
The function C does the reverse. C |
125
|
|
|
|
|
|
|
will accept as an argument either the full URL or just the identifier. |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
If anything goes wrong, then either function will return C. |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=head2 EXPORT |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
makeashorterlink, makealongerlink |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=head1 SUPPORT, LICENSE, THANKS and SUCH |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
See the main L docs. |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=head1 AUTHOR |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
Iain Truskett |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=head1 SEE ALSO |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
L, L |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=cut |