| 1 | # $Id$ | ||||||||||
| 2 | |||||||||||
| 3 | =head1 NAME | ||||||||||
| 4 | |||||||||||
| 5 | WWW::Shorten::Digg - Perl interface to digg.com | ||||||||||
| 6 | |||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||
| 8 | |||||||||||
| 9 | use WWW::Shorten::Digg; | ||||||||||
| 10 | use WWW::Shorten 'Digg'; | ||||||||||
| 11 | |||||||||||
| 12 | $short_url = makeashorterlink($long_url); | ||||||||||
| 13 | |||||||||||
| 14 | $long_url = makealongerlink($short_url); | ||||||||||
| 15 | |||||||||||
| 16 | =head1 DESCRIPTION | ||||||||||
| 17 | |||||||||||
| 18 | A Perl interface to the web site digg.com. Digg maintains | ||||||||||
| 19 | a database of long URLs, each of which has a unique identifier. | ||||||||||
| 20 | |||||||||||
| 21 | =cut | ||||||||||
| 22 | |||||||||||
| 23 | package WWW::Shorten::Digg; | ||||||||||
| 24 | |||||||||||
| 25 | 1 | ▶ | use 5.006; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 26 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 27 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 28 | 1 | ▶ | use URI::Escape; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 29 | 1 | ▶ | use HTML::Entities; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 30 | |||||||||||
| 31 | 1 | ▶ | use base qw( WWW::Shorten::generic Exporter ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 32 | 1 | our @EXPORT = qw( makeashorterlink makealongerlink ); | |||||||||
| 33 | 1 | our $VERSION = '0.01'; | |||||||||
| 34 | |||||||||||
| 35 | 1 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 36 | |||||||||||
| 37 | =head1 Functions | ||||||||||
| 38 | |||||||||||
| 39 | =head2 makeashorterlink | ||||||||||
| 40 | |||||||||||
| 41 | The function C<makeashorterlink> will call the Digg web site passing | ||||||||||
| 42 | it your long URL and will return the shorter Digg version. | ||||||||||
| 43 | |||||||||||
| 44 | =cut | ||||||||||
| 45 | |||||||||||
| 46 | sub makeashorterlink ($) | ||||||||||
| 47 | { | ||||||||||
| 48 | 1 | ▶ | my $url = uri_escape(shift) or croak 'No URL passed to makeashorterlink'; | ||||||||
|
Subroutine
makeashorterlink: called CC=4 SCAR=21.7
Pod
documented
Branch: 1 branch
| |||||||||||
| 49 | 1 | my $ua = __PACKAGE__->ua(); | |||||||||
| 50 | 1 | my $diggurl = "http://services.digg.com/url/short/create?url=$url&appkey=http%3A%2F%2Fsearch.cpan.org%2Fsearch%3Fquery%3DWWW-Shorten-Digg&type=xml"; | |||||||||
| 51 | 1 | my $resp = $ua->get($diggurl); | |||||||||
| 52 | 0 | ▶ | return undef unless $resp->is_success; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 53 | 0 | my $content = $resp->content; | |||||||||
| 54 | 0 | ▶ | if ($resp->content =~ m!short_url="(\Qhttp://digg.com/\E\w+)"!x) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 55 | 0 | return $1; | |||||||||
| 56 | } | ||||||||||
| 57 | 0 | return; | |||||||||
| 58 | } | ||||||||||
| 59 | |||||||||||
| 60 | =head2 makealongerlink | ||||||||||
| 61 | |||||||||||
| 62 | The function C<makealongerlink> does the reverse. C<makealongerlink> | ||||||||||
| 63 | will accept as an argument either the full Digg URL or just the | ||||||||||
| 64 | Digg identifier. | ||||||||||
| 65 | |||||||||||
| 66 | If anything goes wrong, then either function will return C<undef>. | ||||||||||
| 67 | |||||||||||
| 68 | =cut | ||||||||||
| 69 | |||||||||||
| 70 | sub makealongerlink ($) | ||||||||||
| 71 | { | ||||||||||
| 72 | 0 | ▶ | my $short = uri_escape(shift) or croak 'No Digg key / URL passed to makealongerlink'; | ||||||||
|
Subroutine
makealongerlink: not called CC=5 SCAR=34.0
Pod
documented
Branch: 1 branch
| |||||||||||
| 73 | 0 | ▶ | if ($short =~ m!\Qhttp%3A%2F%2Fdigg.com%2F\E(\w+)!x) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 74 | 0 | $short = $1; | |||||||||
| 75 | } | ||||||||||
| 76 | 0 | my $ua = __PACKAGE__->ua(); | |||||||||
| 77 | 0 | my $diggurl = "http://services.digg.com/url/short/$short?appkey=http%3A%2F%2Fsearch.cpan.org%2Fsearch%3Fquery%3DWWW-Shorten-Digg&type=xml"; | |||||||||
| 78 | 0 | my $resp = $ua->get($diggurl); | |||||||||
| 79 | 0 | ▶ | return undef unless $resp->is_success; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 80 | 0 | my $content = $resp->content; | |||||||||
| 81 | 0 | ▶ | if ($resp->content =~ m!link="(.*?)"!x) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 82 | 0 | return decode_entities($1, '<&>"'); | |||||||||
| 83 | } | ||||||||||
| 84 | 0 | return; | |||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | 1 | 1; | |||||||||
| 88 | |||||||||||
| 89 | __END__ | ||||||||||