line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# |
2
|
|
|
|
|
|
|
# $Id$ |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# www::shorten Brik |
5
|
|
|
|
|
|
|
# |
6
|
|
|
|
|
|
|
package Metabrik::Www::Shorten; |
7
|
1
|
|
|
1
|
|
543
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
27
|
|
8
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
33
|
|
9
|
|
|
|
|
|
|
|
10
|
1
|
|
|
1
|
|
9
|
use base qw(Metabrik); |
|
1
|
|
|
|
|
8
|
|
|
1
|
|
|
|
|
557
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub brik_properties { |
13
|
|
|
|
|
|
|
return { |
14
|
0
|
|
|
0
|
1
|
|
revision => '$Revision$', |
15
|
|
|
|
|
|
|
tags => [ qw(unstable shortener url uri) ], |
16
|
|
|
|
|
|
|
author => 'GomoR ', |
17
|
|
|
|
|
|
|
license => 'http://opensource.org/licenses/BSD-3-Clause', |
18
|
|
|
|
|
|
|
attributes => { |
19
|
|
|
|
|
|
|
service => [ qw(default|tinyurl|linkz|shorl) ], |
20
|
|
|
|
|
|
|
ssl_verify => [ qw(0|1) ], |
21
|
|
|
|
|
|
|
}, |
22
|
|
|
|
|
|
|
attributes_default => { |
23
|
|
|
|
|
|
|
service => 'default', |
24
|
|
|
|
|
|
|
ssl_verify => 0, |
25
|
|
|
|
|
|
|
}, |
26
|
|
|
|
|
|
|
commands => { |
27
|
|
|
|
|
|
|
'shorten' => [ qw(uri) ], |
28
|
|
|
|
|
|
|
'shorten_default' => [ qw(uri) ], |
29
|
|
|
|
|
|
|
'shorten_tinyurl' => [ qw(uri) ], |
30
|
|
|
|
|
|
|
'unshorten' => [ qw(uri) ], |
31
|
|
|
|
|
|
|
}, |
32
|
|
|
|
|
|
|
require_modules => { |
33
|
|
|
|
|
|
|
'WWW::Shorten' => [ ], |
34
|
|
|
|
|
|
|
'Metabrik::Client::Www' => [ ], |
35
|
|
|
|
|
|
|
'Metabrik::String::Uri' => [ ], |
36
|
|
|
|
|
|
|
}, |
37
|
|
|
|
|
|
|
}; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub shorten_default { |
41
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
42
|
0
|
|
|
|
|
|
my ($uri) = @_; |
43
|
|
|
|
|
|
|
|
44
|
0
|
0
|
|
|
|
|
$self->brik_help_run_undef_arg('shorten_default', $uri) or return; |
45
|
|
|
|
|
|
|
|
46
|
0
|
|
|
|
|
|
my $service = 'http://url.pm'; |
47
|
|
|
|
|
|
|
|
48
|
0
|
0
|
|
|
|
|
my $cw = Metabrik::Client::Www->new_from_brik_init($self) or return; |
49
|
0
|
0
|
|
|
|
|
$cw->post({ _url => $uri }, $service) or return; |
50
|
|
|
|
|
|
|
|
51
|
0
|
|
|
|
|
|
my $shorten; |
52
|
0
|
0
|
|
|
|
|
my $content = $cw->content or return; |
53
|
0
|
0
|
|
|
|
|
if (length($content)) { |
54
|
0
|
|
|
|
|
|
($shorten) = $content =~ m{(http://url\.pm/[^"]+)}; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
0
|
|
|
|
|
|
return $shorten; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub shorten_tinyurl { |
61
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
62
|
0
|
|
|
|
|
|
my ($uri) = @_; |
63
|
|
|
|
|
|
|
|
64
|
0
|
0
|
|
|
|
|
$self->brik_help_run_undef_arg('shorten_tinyurl', $uri) or return; |
65
|
|
|
|
|
|
|
|
66
|
0
|
|
|
|
|
|
eval("use WWW::Shorten 'TinyURL';"); |
67
|
|
|
|
|
|
|
|
68
|
0
|
|
|
|
|
|
my $shorten; |
69
|
0
|
|
|
|
|
|
eval { |
70
|
0
|
|
|
|
|
|
$shorten = makeashorterlink($uri); |
71
|
|
|
|
|
|
|
}; |
72
|
0
|
0
|
|
|
|
|
if ($@) { |
73
|
0
|
|
|
|
|
|
chomp($@); |
74
|
0
|
|
|
|
|
|
return $self->log->error("shorten_tinyurl: failed with [$@]"); |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
0
|
|
|
|
|
|
return $shorten; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
sub shorten { |
81
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
82
|
0
|
|
|
|
|
|
my ($uri) = @_; |
83
|
|
|
|
|
|
|
|
84
|
0
|
|
|
|
|
|
my $service = $self->service; |
85
|
0
|
0
|
|
|
|
|
$self->brik_help_run_undef_arg('shorten', $uri) or return; |
86
|
|
|
|
|
|
|
|
87
|
0
|
|
|
|
|
|
my $shorten = ''; |
88
|
0
|
0
|
|
|
|
|
if ($service eq 'default') { |
|
|
0
|
|
|
|
|
|
89
|
0
|
0
|
|
|
|
|
$shorten = $self->shorten_default($uri) or return; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
elsif ($service eq 'tinyurl') { |
92
|
0
|
0
|
|
|
|
|
$shorten = $self->shorten_tinyurl($uri) or return; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
else { |
95
|
0
|
|
|
|
|
|
return $self->log->error("shorten: don't know how to shorten service with [$service]"); |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
0
|
|
|
|
|
|
return $shorten; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
sub unshorten { |
102
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
103
|
0
|
|
|
|
|
|
my ($uri) = @_; |
104
|
|
|
|
|
|
|
|
105
|
0
|
0
|
|
|
|
|
$self->brik_help_run_undef_arg('unshorten', $uri) or return; |
106
|
|
|
|
|
|
|
|
107
|
0
|
0
|
|
|
|
|
my $cw = Metabrik::Client::Www->new_from_brik_init($self) or return; |
108
|
0
|
0
|
|
|
|
|
my $trace = $cw->trace_redirect($uri) or return; |
109
|
|
|
|
|
|
|
|
110
|
0
|
|
|
|
|
|
my $unshorten; |
111
|
0
|
0
|
0
|
|
|
|
if (@$trace > 0 && exists($trace->[-1]->{uri})) { |
112
|
0
|
|
|
|
|
|
$unshorten = $trace->[-1]->{uri}; |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
0
|
|
0
|
|
|
|
return $unshorten || 'undef'; |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
1; |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
__END__ |