line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
=encoding utf8 |
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
=head1 NAME |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
WWW::Shorten::SapoPuny - Perl interface to xsl.pt |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
=head1 SYNOPSIS |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
use WWW::Shorten::SapoPuny; |
11
|
|
|
|
|
|
|
use WWW::Shorten 'SapoPuny'; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
$short_url = makeashorterlink($long_url); |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
$long_url = makealongerlink($short_url); |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
=head1 DESCRIPTION |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
A Perl interface to the web site xsl.pt. SapoPuny simply maintains |
20
|
|
|
|
|
|
|
a database of long URLs, each of which has a unique identifier. |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
=cut |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
package WWW::Shorten::SapoPuny; |
25
|
|
|
|
|
|
|
$WWW::Shorten::SapoPuny::VERSION = '0.04'; |
26
|
1
|
|
|
1
|
|
13735
|
use 5.006; |
|
1
|
|
|
|
|
3
|
|
27
|
1
|
|
|
1
|
|
3
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
17
|
|
28
|
1
|
|
|
1
|
|
3
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
25
|
|
29
|
|
|
|
|
|
|
|
30
|
1
|
|
|
1
|
|
3
|
use base qw( WWW::Shorten::generic Exporter ); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
431
|
|
31
|
|
|
|
|
|
|
our @EXPORT = qw( makeashorterlink makealongerlink ); |
32
|
|
|
|
|
|
|
our $_error_message = ''; |
33
|
|
|
|
|
|
|
|
34
|
1
|
|
|
1
|
|
46188
|
use Carp; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
347
|
|
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
=head1 Functions |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=head2 makeashorterlink |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
The function C will call the SapoPuny web site passing |
41
|
|
|
|
|
|
|
it your long URL and will return the shorter SapoPuny version. |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=cut |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
#javascript:void(location.href='http://xsl.pt/punify?url='+encodeURIComponent(location.href)) |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub makeashorterlink { |
48
|
2
|
100
|
|
2
|
1
|
171
|
my $url = shift or croak 'No URL passed to makeashorterlink'; |
49
|
1
|
|
|
|
|
3
|
$_error_message = ''; |
50
|
1
|
|
|
|
|
13
|
my $ua = __PACKAGE__->ua(); |
51
|
1
|
|
|
|
|
13187
|
my $tinyurl = 'http://xsl.pt/punify?url='; |
52
|
1
|
|
|
|
|
96
|
print STDERR $tinyurl . $url; |
53
|
1
|
|
|
|
|
13
|
my $resp = $ua->get( $tinyurl . $url ); |
54
|
|
|
|
|
|
|
|
55
|
1
|
50
|
|
|
|
321580
|
unless ( $resp->is_success ) { |
56
|
0
|
|
|
|
|
0
|
$_error_message = $resp->status_line; |
57
|
0
|
|
|
|
|
0
|
return undef; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
1
|
|
|
|
|
15
|
my $content = $resp->content; |
61
|
1
|
50
|
|
|
|
26
|
if ( $content !~ /id="ascii"/ ) { |
62
|
0
|
0
|
|
|
|
0
|
if ( $content =~ /
|
|
|
0
|
|
|
|
|
|
63
|
0
|
|
|
|
|
0
|
$_error_message = 'Error is a html page'; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
elsif ( length($content) > 100 ) { |
66
|
0
|
|
|
|
|
0
|
$_error_message = substr( $content, 0, 100 ); |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
else { |
69
|
0
|
|
|
|
|
0
|
$_error_message = $content; |
70
|
|
|
|
|
|
|
} |
71
|
0
|
|
|
|
|
0
|
return undef; |
72
|
|
|
|
|
|
|
} |
73
|
1
|
50
|
|
|
|
6
|
if ( $resp->content =~ m!(http://[a-z0-9]+\.[a-z0-9]+\.xsl\.pt)!x ) { |
74
|
1
|
|
|
|
|
69
|
return $1; |
75
|
|
|
|
|
|
|
} |
76
|
0
|
|
|
|
|
0
|
return; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=head2 makealongerlink |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
The function C does the reverse. C |
82
|
|
|
|
|
|
|
will accept as an B the full SapoPuny URL. |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
If anything goes wrong, then either function will return C. |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=cut |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
sub makealongerlink { |
89
|
2
|
100
|
|
2
|
1
|
1165
|
my $tinyurl_url = shift |
90
|
|
|
|
|
|
|
or croak 'No SapoPuny key / URL passed to makealongerlink'; |
91
|
1
|
|
|
|
|
3
|
$_error_message = ''; |
92
|
1
|
|
|
|
|
9
|
my $ua = __PACKAGE__->ua(); |
93
|
|
|
|
|
|
|
|
94
|
1
|
50
|
|
|
|
12
|
return undef unless $tinyurl_url =~ m!http://[a-z0-9]+\.[a-z0-9]+\.xsl\.pt!; |
95
|
|
|
|
|
|
|
|
96
|
1
|
|
|
|
|
5
|
my $resp = $ua->get($tinyurl_url); |
97
|
|
|
|
|
|
|
|
98
|
1
|
50
|
|
|
|
757601
|
unless ( $resp->is_redirect ) { |
99
|
0
|
|
|
|
|
0
|
my $content = $resp->content; |
100
|
0
|
0
|
|
|
|
0
|
if ( $content =~ /Error/ ) { |
101
|
0
|
0
|
|
|
|
0
|
if ( $content =~ /
|
|
|
0
|
|
|
|
|
|
102
|
0
|
|
|
|
|
0
|
$_error_message = 'Error is a html page'; |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
elsif ( length($content) > 100 ) { |
105
|
0
|
|
|
|
|
0
|
$_error_message = substr( $content, 0, 100 ); |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
else { |
108
|
0
|
|
|
|
|
0
|
$_error_message = $content; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
else { |
112
|
0
|
|
|
|
|
0
|
$_error_message = 'Unknown error'; |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
0
|
|
|
|
|
0
|
return undef; |
116
|
|
|
|
|
|
|
} |
117
|
1
|
|
|
|
|
12
|
my $url = $resp->header('Location'); |
118
|
1
|
|
|
|
|
39
|
return $url; |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
1; |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
__END__ |