line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package WWW::Shorten::ptl;
|
2
|
3
|
|
|
3
|
|
11190
|
use strict;
|
|
3
|
|
|
|
|
9
|
|
|
3
|
|
|
|
|
115
|
|
3
|
3
|
|
|
3
|
|
15
|
use warnings;
|
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
153
|
|
4
|
|
|
|
|
|
|
our $VERSION = '0.04';
|
5
|
|
|
|
|
|
|
|
6
|
3
|
|
|
3
|
|
2870
|
use JSON::Any;
|
|
3
|
|
|
|
|
83926
|
|
|
3
|
|
|
|
|
17
|
|
7
|
3
|
|
|
3
|
|
34207
|
use Carp;
|
|
3
|
|
|
|
|
8
|
|
|
3
|
|
|
|
|
233
|
|
8
|
3
|
|
|
3
|
|
2850
|
use URI;
|
|
3
|
|
|
|
|
14744
|
|
|
3
|
|
|
|
|
105
|
|
9
|
|
|
|
|
|
|
|
10
|
3
|
|
|
3
|
|
32
|
use base qw/WWW::Shorten::generic Exporter/;
|
|
3
|
|
|
|
|
10
|
|
|
3
|
|
|
|
|
1901
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our @EXPORT = qw/makeashorterlink makealongerlink/;
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub new
|
15
|
|
|
|
|
|
|
{
|
16
|
|
|
|
|
|
|
my ($cls, @prm)= @_;
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
if ( @prm % 2 ) {
|
19
|
|
|
|
|
|
|
croak "parameters should be HASH form";
|
20
|
|
|
|
|
|
|
}
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
my %org_prm = @prm;
|
23
|
|
|
|
|
|
|
my %prm;
|
24
|
|
|
|
|
|
|
for my $key (qw/apikey/) {
|
25
|
|
|
|
|
|
|
if (exists $org_prm{$key}) {
|
26
|
|
|
|
|
|
|
$prm{$key} = delete $org_prm{$key};
|
27
|
|
|
|
|
|
|
}
|
28
|
|
|
|
|
|
|
}
|
29
|
|
|
|
|
|
|
if ( %org_prm ) {
|
30
|
|
|
|
|
|
|
carp "unknown parameter keys:", join(",", keys %org_prm);
|
31
|
|
|
|
|
|
|
}
|
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
my $ua = __PACKAGE__->ua;
|
34
|
|
|
|
|
|
|
$ua->requests_redirectable([]);
|
35
|
|
|
|
|
|
|
bless { ua => $ua, %prm }, $cls;
|
36
|
|
|
|
|
|
|
}
|
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub apikey
|
39
|
|
|
|
|
|
|
{
|
40
|
|
|
|
|
|
|
my $self = shift;
|
41
|
|
|
|
|
|
|
if ( @_ ) {
|
42
|
|
|
|
|
|
|
$self->{apikey} = shift;
|
43
|
|
|
|
|
|
|
}
|
44
|
|
|
|
|
|
|
$self->{apikey};
|
45
|
|
|
|
|
|
|
}
|
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub shorten
|
48
|
|
|
|
|
|
|
{
|
49
|
|
|
|
|
|
|
my $self = shift;
|
50
|
|
|
|
|
|
|
my $url = shift;
|
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
my $apikey = $self->apikey;
|
53
|
|
|
|
|
|
|
if ( !$apikey ) {
|
54
|
|
|
|
|
|
|
croak "no API-Key has been set";
|
55
|
|
|
|
|
|
|
}
|
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
if ( !$url ) {
|
58
|
|
|
|
|
|
|
carp "no url passed";
|
59
|
|
|
|
|
|
|
return;
|
60
|
|
|
|
|
|
|
}
|
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
my $uri = URI->new("http://p.tl/api/api_simple.php");
|
63
|
|
|
|
|
|
|
$uri->query_form(
|
64
|
|
|
|
|
|
|
key => $apikey,
|
65
|
|
|
|
|
|
|
url => $url,
|
66
|
|
|
|
|
|
|
);
|
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
my $res = $self->{ua}->get($uri->as_string);
|
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
if (!$res->is_success) {
|
72
|
|
|
|
|
|
|
carp "failed to request : ", $res->status_line;
|
73
|
|
|
|
|
|
|
return;
|
74
|
|
|
|
|
|
|
}
|
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
JSON::Any->jsonToObj($res->content);
|
77
|
|
|
|
|
|
|
}
|
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub extract
|
80
|
|
|
|
|
|
|
{
|
81
|
|
|
|
|
|
|
my $self = shift;
|
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
my $url = shift;
|
84
|
|
|
|
|
|
|
if (!$url) {
|
85
|
|
|
|
|
|
|
carp "no url passed";
|
86
|
|
|
|
|
|
|
return;
|
87
|
|
|
|
|
|
|
}
|
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
if ( !$self->_check_url($url) ) {
|
90
|
|
|
|
|
|
|
carp "given url is not for p.tl : ", $url;
|
91
|
|
|
|
|
|
|
return;
|
92
|
|
|
|
|
|
|
}
|
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
my $res = $self->{ua}->head($url);
|
95
|
|
|
|
|
|
|
$res->header( "Location" );
|
96
|
|
|
|
|
|
|
}
|
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
sub _check_url
|
99
|
|
|
|
|
|
|
{
|
100
|
|
|
|
|
|
|
my $self = shift;
|
101
|
|
|
|
|
|
|
my $url = shift;
|
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
scalar $url =~ m{^http://p\.tl/.+};
|
104
|
|
|
|
|
|
|
}
|
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
sub makeashorterlink
|
107
|
|
|
|
|
|
|
{
|
108
|
|
|
|
|
|
|
my $url = shift;
|
109
|
|
|
|
|
|
|
my $apikey = shift;
|
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
if ( !$url ) {
|
112
|
|
|
|
|
|
|
carp "no url passed";
|
113
|
|
|
|
|
|
|
return;
|
114
|
|
|
|
|
|
|
}
|
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
if ( !$apikey ) {
|
117
|
|
|
|
|
|
|
carp "no apikey passed";
|
118
|
|
|
|
|
|
|
return;
|
119
|
|
|
|
|
|
|
}
|
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
my $res = WWW::Shorten::ptl->new(apikey => $apikey)->shorten($url);
|
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
return if !$res;
|
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
if ( $res->{status} ne "ok" ) {
|
126
|
|
|
|
|
|
|
carp "request failed: ", $res->{status};
|
127
|
|
|
|
|
|
|
return;
|
128
|
|
|
|
|
|
|
}
|
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
return $res->{short_url};
|
131
|
|
|
|
|
|
|
}
|
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
sub makealongerlink
|
134
|
|
|
|
|
|
|
{
|
135
|
|
|
|
|
|
|
my $url = shift;
|
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
if ( !$url ) {
|
138
|
|
|
|
|
|
|
carp "no url passed";
|
139
|
|
|
|
|
|
|
return;
|
140
|
|
|
|
|
|
|
}
|
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
WWW::Shorten::ptl->new->extract($url);
|
143
|
|
|
|
|
|
|
}
|
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
1;
|
146
|
|
|
|
|
|
|
__END__
|