line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package WWW::Shorten::ShadyURL; |
2
|
|
|
|
|
|
|
# |
3
|
|
|
|
|
|
|
# WWW::Shorten module for shadyurl.com |
4
|
|
|
|
|
|
|
# |
5
|
|
|
|
|
|
|
# $Id: ShadyURL.pm 141 2010-05-18 03:41:20Z infidel $ |
6
|
|
|
|
|
|
|
# |
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
98391
|
use 5.006; |
|
1
|
|
|
|
|
7
|
|
|
1
|
|
|
|
|
58
|
|
9
|
|
|
|
|
|
|
#use URI::Escape qw( uri_escape uri_unescape ); |
10
|
1
|
|
|
1
|
|
8
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
43
|
|
11
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
21
|
|
|
1
|
|
|
|
|
47
|
|
12
|
|
|
|
|
|
|
|
13
|
1
|
|
|
1
|
|
6
|
use base qw( WWW::Shorten::generic Exporter ); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
1942
|
|
14
|
|
|
|
|
|
|
our @EXPORT = qw( makeashorterlink makealongerlink ); |
15
|
|
|
|
|
|
|
our $VERSION = '0.03'; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
use Carp; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
### |
20
|
|
|
|
|
|
|
### Vars |
21
|
|
|
|
|
|
|
### |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
my $shortener_url = 'http://www.shadyurl.com/create.php?myUrl='; |
24
|
|
|
|
|
|
|
my $arg_sep = '&'; |
25
|
|
|
|
|
|
|
my $params = { |
26
|
|
|
|
|
|
|
'shorten' => '&shorten=on', |
27
|
|
|
|
|
|
|
}; |
28
|
|
|
|
|
|
|
my $url_map = { |
29
|
|
|
|
|
|
|
'5z8.info' => 'www.5z8.info', |
30
|
|
|
|
|
|
|
}; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
=head1 NAME |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
WWW::Shorten::ShadyURL - Perl interface to shadyurl.com |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
=head1 SYNOPSIS |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
use WWW::Shorten::ShadyURL; |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
use WWW::Shorten 'ShadyURL'; |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
$shady_url = makeashorterlink( $long_url ); |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
$orig_url = makealongerlink( $shady_url ); |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
=head1 DESCRIPTION |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
A Perl interface to the web site shadyurl.com. ShadyURL simply maintains |
49
|
|
|
|
|
|
|
a database of long URLs, each of which has a unique identifier, that |
50
|
|
|
|
|
|
|
appears at first glance "shady", but resolves just like any other shortener |
51
|
|
|
|
|
|
|
service. However, the URLS may not be actually shorter, just sketchier. |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
Their motto is: |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=over 4 |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
I |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
=back |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
I am not affiliated with them. See L for more |
62
|
|
|
|
|
|
|
information. |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=head1 FUNCTIONS |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=head2 B( $url [, $shorten ] ) |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
The function C will connect to the ShadyURL web site and |
69
|
|
|
|
|
|
|
attempt to create an alias to the URL supplied. |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=head3 Arguments: |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
B<$url> [required] - The URL you wish to shorten. |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
B<$shorten> [optional] - Set to a C value to actually create a short link. |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
The ShadyURL service creates links that appear sketchy, there is no guarantee |
78
|
|
|
|
|
|
|
that they will actually be shorter than the supplied URL. This attempts to |
79
|
|
|
|
|
|
|
make them shorter, but they will then appear less dubious. |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=cut |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub makeashorterlink ($;$) |
84
|
|
|
|
|
|
|
{ |
85
|
|
|
|
|
|
|
my $url = shift or croak 'No URL passed to makeashorterlink'; |
86
|
|
|
|
|
|
|
my $shorten = shift; |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
# Construct the request URL |
89
|
|
|
|
|
|
|
my @url_args; |
90
|
|
|
|
|
|
|
# $url = $shortener_url . uri_escape( $url ); |
91
|
|
|
|
|
|
|
$url = $shortener_url . $url; |
92
|
|
|
|
|
|
|
push( @url_args, $params->{'shorten'} ) if( $shorten ); |
93
|
|
|
|
|
|
|
$url = join( $arg_sep, $url, @url_args ); |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
# Get the page |
96
|
|
|
|
|
|
|
my $ua = __PACKAGE__->ua(); |
97
|
|
|
|
|
|
|
my $resp = $ua->get( $url ); |
98
|
|
|
|
|
|
|
return unless $resp->is_success; |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
# HTML manual parsing = evil, but there's no API, and I'm not going |
101
|
|
|
|
|
|
|
# to pull in a whole parser. If ShadyURL starts making a lot of |
102
|
|
|
|
|
|
|
# changes, I'll do it in a later revision. Deal. |
103
|
|
|
|
|
|
|
my $content = $resp->decoded_content; |
104
|
|
|
|
|
|
|
my ( $shorturl ) = $content =~ m#is now.*?href.*?\>(.*?)\#; |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
return( $shorturl ); |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=head2 B( $shorturl ) |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
The function C does the reverse. C |
112
|
|
|
|
|
|
|
will accept as an argument a full ShadyURL link. |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
If anything goes wrong, then either function will return C. |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=cut |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
sub makealongerlink ($) |
119
|
|
|
|
|
|
|
{ |
120
|
|
|
|
|
|
|
my $url = shift |
121
|
|
|
|
|
|
|
or croak 'No URL passed to makealongerlink'; |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
# skip unnecessary shadyURL double redirects ( canonical -> www. ) |
124
|
|
|
|
|
|
|
$url =~ s/\Q$_\E/$url_map->{$_}/ |
125
|
|
|
|
|
|
|
for( keys( %$url_map ) ); |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
# get Location header |
128
|
|
|
|
|
|
|
my $ua = __PACKAGE__->ua(); |
129
|
|
|
|
|
|
|
my $resp = $ua->get( $url ); |
130
|
|
|
|
|
|
|
my $location = $resp->header('Location'); |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
# return uri_unescape( $location ) if( $location ); |
133
|
|
|
|
|
|
|
return $location if( $location ); |
134
|
|
|
|
|
|
|
return; |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
1; |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
__END__ |