line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
############################################################ |
2
|
|
|
|
|
|
|
# |
3
|
|
|
|
|
|
|
# $Id: TinyURL.pm 566 2006-06-01 18:38:40Z nicolaw $ |
4
|
|
|
|
|
|
|
# Tie::TinyURL - Tied interface to TinyURL.com |
5
|
|
|
|
|
|
|
# |
6
|
|
|
|
|
|
|
# Copyright 2006 Nicola Worthington |
7
|
|
|
|
|
|
|
# |
8
|
|
|
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); |
9
|
|
|
|
|
|
|
# you may not use this file except in compliance with the License. |
10
|
|
|
|
|
|
|
# You may obtain a copy of the License at |
11
|
|
|
|
|
|
|
# |
12
|
|
|
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0 |
13
|
|
|
|
|
|
|
# |
14
|
|
|
|
|
|
|
# Unless required by applicable law or agreed to in writing, software |
15
|
|
|
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, |
16
|
|
|
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
17
|
|
|
|
|
|
|
# See the License for the specific language governing permissions and |
18
|
|
|
|
|
|
|
# limitations under the License. |
19
|
|
|
|
|
|
|
# |
20
|
|
|
|
|
|
|
############################################################ |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
package Tie::TinyURL; |
23
|
|
|
|
|
|
|
# vim:ts=4:sw=4:tw=78 |
24
|
|
|
|
|
|
|
|
25
|
3
|
|
|
3
|
|
37947
|
use strict; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
118
|
|
26
|
3
|
|
|
3
|
|
3882
|
use LWP::UserAgent qw(); |
|
3
|
|
|
|
|
468501
|
|
|
3
|
|
|
|
|
88
|
|
27
|
3
|
|
|
3
|
|
31
|
use Carp qw(croak carp); |
|
3
|
|
|
|
|
11
|
|
|
3
|
|
|
|
|
222
|
|
28
|
|
|
|
|
|
|
|
29
|
3
|
|
|
3
|
|
16
|
use vars qw($VERSION $DEBUG); |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
2802
|
|
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
$VERSION = '0.02' || sprintf('%d', q$Revision: 566 $ =~ /(\d+)/g); |
32
|
|
|
|
|
|
|
$DEBUG = $ENV{DEBUG} ? 1 : 0; |
33
|
|
|
|
|
|
|
|
34
|
0
|
|
|
0
|
|
0
|
sub UNTIE {} |
35
|
0
|
|
|
0
|
|
0
|
sub DESTROY {} |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub TIEHASH { |
38
|
1
|
|
|
1
|
|
66
|
my $class = shift; |
39
|
|
|
|
|
|
|
|
40
|
1
|
50
|
|
|
|
22
|
my $self = { |
41
|
|
|
|
|
|
|
args => ( @_ % 2 ? [ @_ ] : { @_ } ), |
42
|
|
|
|
|
|
|
seen => {}, |
43
|
|
|
|
|
|
|
ua => LWP::UserAgent->new( |
44
|
|
|
|
|
|
|
timeout => 20, |
45
|
|
|
|
|
|
|
agent => __PACKAGE__ . ' $Id: TinyURL.pm 566 2006-06-01 18:38:40Z nicolaw $', |
46
|
|
|
|
|
|
|
max_redirect => 0, |
47
|
|
|
|
|
|
|
), |
48
|
|
|
|
|
|
|
}; |
49
|
|
|
|
|
|
|
|
50
|
1
|
|
|
|
|
5553
|
$self->{ua}->env_proxy; |
51
|
1
|
|
|
|
|
30290
|
$self->{ua}->max_size(1024*100); |
52
|
1
|
50
|
33
|
|
|
47
|
$self->{ua}->timeout($self->{args}->{timeout}) if |
53
|
|
|
|
|
|
|
ref($self->{args}) eq 'HASH' && defined($self->{args}->{timeout}); |
54
|
|
|
|
|
|
|
|
55
|
1
|
|
|
|
|
14
|
bless $self, $class; |
56
|
1
|
|
|
|
|
6
|
DUMP('$self',$self); |
57
|
1
|
|
|
|
|
6
|
return $self; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub FETCH { |
61
|
3
|
|
|
3
|
|
1172
|
TRACE('FETCH()'); |
62
|
3
|
|
|
|
|
5
|
my $self = shift; |
63
|
3
|
|
|
|
|
22
|
my $url = shift; |
64
|
|
|
|
|
|
|
|
65
|
3
|
|
|
|
|
14
|
TRACE("\$url = '$url'"); |
66
|
3
|
100
|
66
|
|
|
29
|
return unless defined $url && length($url); |
67
|
2
|
50
|
|
|
|
27
|
return $self->{seen}->{$url} if exists $self->{seen}->{$url}; |
68
|
2
|
100
|
|
|
|
8
|
return $self->_retrieve($url) if _isTinyURL($url); |
69
|
1
|
|
|
|
|
6
|
return $self->_store($url); |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub STORE { |
73
|
0
|
|
|
0
|
|
0
|
my $self = shift; |
74
|
0
|
|
|
|
|
0
|
DUMP('$self', $self); |
75
|
0
|
|
|
|
|
0
|
DUMP('@_',\@_); |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub DELETE { |
79
|
0
|
|
|
0
|
|
0
|
my $self = shift; |
80
|
0
|
|
|
|
|
0
|
my $url = shift; |
81
|
|
|
|
|
|
|
|
82
|
0
|
0
|
0
|
|
|
0
|
if (defined $url && exists $self->{seen}->{$url}) { |
83
|
0
|
|
|
|
|
0
|
delete $self->{seen}->{$self->{seen}->{$url}}; |
84
|
0
|
|
|
|
|
0
|
delete $self->{seen}->{$url}; |
85
|
0
|
|
|
|
|
0
|
return 1; |
86
|
|
|
|
|
|
|
} |
87
|
0
|
|
|
|
|
0
|
return 0; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub EXISTS { |
91
|
0
|
|
|
0
|
|
0
|
my $self = shift; |
92
|
0
|
|
|
|
|
0
|
my $url = shift; |
93
|
|
|
|
|
|
|
|
94
|
0
|
0
|
0
|
|
|
0
|
return 0 if !defined($url) || !exists($self->{seen}->{$url}); |
95
|
0
|
|
|
|
|
0
|
return 1; |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
0
|
|
|
0
|
|
0
|
sub FIRSTKEY { |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
0
|
|
|
0
|
|
0
|
sub NEXTKEY { |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
0
|
|
|
0
|
|
0
|
sub SCALAR { |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
sub _isTinyURL { |
108
|
2
|
|
|
2
|
|
24
|
return $_[0] =~ /^http:\/\/(?:www\.)?tinyurl\.com\/[a-zA-Z0-9]+$/i; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
sub _retrieve { |
112
|
1
|
|
|
1
|
|
3
|
TRACE('_retrieve()'); |
113
|
1
|
|
|
|
|
1
|
my $self = shift; |
114
|
1
|
|
|
|
|
3
|
my $tinyurl = shift; |
115
|
|
|
|
|
|
|
|
116
|
1
|
|
|
|
|
6
|
my $response = $self->{ua}->get($tinyurl); |
117
|
1
|
|
50
|
|
|
247194
|
my $url = $response->header('location') || undef; |
118
|
1
|
50
|
|
|
|
67
|
if ($url) { |
119
|
1
|
|
|
|
|
6
|
$self->{seen}->{$tinyurl} = $url; |
120
|
1
|
|
|
|
|
7
|
$self->{seen}->{$url} = $tinyurl; |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
1
|
|
|
|
|
38
|
return $url; |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
sub _store { |
127
|
1
|
|
|
1
|
|
7
|
TRACE('_store()'); |
128
|
1
|
|
|
|
|
2
|
my $self = shift; |
129
|
1
|
|
|
|
|
2
|
my $url = shift; |
130
|
|
|
|
|
|
|
|
131
|
1
|
|
|
|
|
2
|
my $tinyurl = undef; |
132
|
1
|
|
|
|
|
12
|
my $response = $self->{ua}->post( |
133
|
|
|
|
|
|
|
'http://tinyurl.com/create.php', |
134
|
|
|
|
|
|
|
[('url',$url)] |
135
|
|
|
|
|
|
|
); |
136
|
|
|
|
|
|
|
|
137
|
1
|
50
|
|
|
|
12294184
|
if ($response->is_success) { |
138
|
1
|
50
|
|
|
|
18
|
if ($response->content =~ m|
|
139
|
|
|
|
|
|
|
value="(http://tinyurl.com/[a-zA-Z0-9]+)">|x) { |
140
|
0
|
|
|
|
|
0
|
$tinyurl = $1; |
141
|
0
|
|
|
|
|
0
|
$self->{seen}->{$url} = $tinyurl; |
142
|
0
|
|
|
|
|
0
|
$self->{seen}->{$tinyurl} = $url; |
143
|
|
|
|
|
|
|
} else { |
144
|
1
|
|
|
|
|
23
|
TRACE("Couldn't extract tinyurl"); |
145
|
1
|
|
|
|
|
4
|
DUMP("Content",$response->content); |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
} |
148
|
|
|
|
|
|
|
|
149
|
1
|
|
|
|
|
19
|
return $tinyurl; |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
sub TRACE { |
154
|
9
|
50
|
|
9
|
0
|
31
|
return unless $DEBUG; |
155
|
0
|
|
|
|
|
0
|
warn(shift()); |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
sub DUMP { |
159
|
2
|
50
|
|
2
|
0
|
19
|
return unless $DEBUG; |
160
|
0
|
|
|
|
|
|
eval { |
161
|
0
|
|
|
|
|
|
require Data::Dumper; |
162
|
0
|
|
|
|
|
|
warn(shift().': '.Data::Dumper::Dumper(shift())); |
163
|
|
|
|
|
|
|
} |
164
|
|
|
|
|
|
|
} |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
1; |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=pod |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=head1 NAME |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
Tie::TinyURL - Tied interface to TinyURL.com |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=head1 SYNOPSIS |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
use strict; |
179
|
|
|
|
|
|
|
use Tie::TinyURL; |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
my %url; |
182
|
|
|
|
|
|
|
tie %url, "Tie::TinyURL"; |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
## Explicitly set an HTTP timeout of 3 seconds |
185
|
|
|
|
|
|
|
# tie %url, "Tie::TinyURL", "timeout" => 3; |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
my $tinyurl = $url{"http://www.bbc.co.uk"}; |
188
|
|
|
|
|
|
|
my $url = $url{$tinyurl}; |
189
|
|
|
|
|
|
|
print "$tinyurl => $url\n"; |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
=head1 DESCRIPTION |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
This module provides a very basic tied interface to the TinyURL.com |
194
|
|
|
|
|
|
|
web service. |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
=head1 SEE ALSO |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
L, L |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
=head1 VERSION |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
$Id: TinyURL.pm 566 2006-06-01 18:38:40Z nicolaw $ |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
=head1 AUTHOR |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
Nicola Worthington |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
L |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
=head1 COPYRIGHT |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
Copyright 2006 Nicola Worthington. |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
This software is licensed under The Apache Software License, Version 2.0. |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
L |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
=cut |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
__END__ |