WWW::Shorten::Bitly - Interface to shortening URLs using L<http://bit.ly>


WWW-Shorten-Bitly documentation Contained in the WWW-Shorten-Bitly distribution.

Index


Code Index:

NAME

Top

WWW::Shorten::Bitly - Interface to shortening URLs using http://bit.ly

VERSION

Top

$Revision: 1.14 $

SYNOPSIS

Top

WWW::Shorten::Bitly provides an easy interface for shortening URLs using http://bit.ly. In addition to shortening URLs, you can pull statistics that bit.ly gathers regarding each shortened WWW::Shorten::Bitly uses XML::Simple to convert the xml response for the meta info and click stats to create a hashref of the results.

WWW::Shorten::Bitly provides two interfaces. The first is the common makeashorterlink and makealongerlink that WWW::Shorten provides. However, due to the way the bit.ly API works, additional arguments are required. The second provides a better way of retrieving additional information and statistics about a bit.ly URL.

use WWW::Shorten::Bitly;

my $url = "http://www.example.com";

my $tmp = makeashorterlink($url, 'MY_BITLY_USERNAME', 'MY_BITLY_API_KEY'); my $tmp1 = makealongerlink($tmp, 'MY_BITLY_USERNAME', 'MY_BITLY_API_KEY');

or

use WWW::Shorten::Bitly;

my $url = "http://www.example.com"; my $bitly = WWW::Shorten::Bitly->new(URL => $url, USER => "my_user_id", APIKEY => "my_api_key");

$bitly->shorten(URL => $url); print "shortened URL is $bitly->{bitlyurl}\n";

$bitly->expand(URL => $bitly->{bitlyurl}); print "expanded/original URL is $bitly->{longurl}\n";

my $info = $bitly->info(); say "City referred to is " . $info->{calais}->{city}->{item}; say "Companies referred to are " . $info->{calais}->{company}->{item}[0] . "and " . $info->{calais}->{company}->{item}[1]; say "Title of the page is " . $info->{htmlTitle};

my $clicks = $bitly->clicks(); say "Total number of clicks received: " . $clicks->{clicks}; say "Total number of direct clicks received are: " . ${$clicks->{referrers}->{nodeKeyVal}[0]}->{direct}

FUNCTIONS

Top

new

Create a new bit.ly object using your bit.ly user id and bit.ly api key.

my $bitly = WWW::Shorten::Bitly->new(URL => "http://www.example.com/this_is_one_example.html", USER => "bitly_user_id", APIKEY => "bitly_api_key");

to use bit.ly's new j.mp service, just construct the bitly object like this: my $bitly = WWW::Shorten::Bitly->new(URL => "http://www.example.com/this_is_one_example.html", USER => "bitly_user_id", APIKEY => "bitly_api_key", jmp => 1);

shorten

Shorten a URL using http://bit.ly. Calling the shorten method will return the shortened URL but will also store it in bit.ly object until the next call is made.

my $url = "http://www.example.com"; my $shortstuff = $bitly->shorten(URL => $url);

print "biturl is " . $bitly->{bitlyurl} . "\n"; or print "biturl is $shortstuff\n";

expand

Expands a shortened bit.ly URL to the original long URL.

info

Get info about a shortened bit.ly URL. By default, the method will use the value that's stored in $bitly->{bitlyurl}. To be sure you're getting info on the correct URL, it's a good idea to set this value before getting any info on it.

$bitly->{bitlyurl} = "http://bit.ly/jmv6"; my $info = $bitly->info();

say "City referred to is " . $info->{calais}->{city}->{item}; say "Companies referred to are " . $info->{calais}->{company}->{item}[0] . "and " . $info->{calais}->{company}->{item}[1]; say "Title of the page is " . $info->{htmlTitle};

clicks

Get click thru information for a shortened bit.ly URL. By default, the method will use the value that's stored in $bitly->{bitlyurl}. To be sure you're getting info on the correct URL, it's a good idea to set this value before getting any info on it.

$bitly->{bitlyurl} = "http://bit.ly/jmv6"; my $clicks = $bitly->clicks();

say "Total number of clicks received: " . $clicks->{clicks}; say "Total number of direct clicks received are: " . ${$clicks->{referrers}->{nodeKeyVal}[0]}->{direct}

errors

version

Gets the module version number

FILES

Top

$HOME/.bitly or _bitly on Windows Systems.

You may omit USER and APIKEY in the constructor if you set them in the .bitly config file on separate lines using the syntax:

USER=username APIKEY=apikey

AUTHOR

Top

Pankaj Jain, <pjain at cpan.org>

BUGS

Top

Please report any bugs or feature requests to bug-www-shorten-bitly at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=WWW-Shorten-Bitly. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

Top

You can find documentation for this module with the perldoc command.

perldoc WWW::Shorten::Bitly

You can also look for information at:

* RT: CPAN's request tracker

http://rt.cpan.org/NoAuth/Bugs.html?Dist=WWW-Shorten-Bitly

* AnnoCPAN: Annotated CPAN documentation

http://annocpan.org/dist/WWW-Shorten-Bitly

* CPAN Ratings

http://cpanratings.perl.org/d/WWW-Shorten-Bitly

* Search CPAN

http://search.cpan.org/dist/WWW-Shorten-Bitly/

ACKNOWLEDGEMENTS

Top

http://bit.ly for a wonderful service.
Larry Wall, Damian Conway, and all the amazing folks giving us Perl and continuing to work on it over the years. =item Mizar, <mizar.jp@gmail.com>, Peter Edwards, <pedwards@cpan.org >>, Joerg Meltzer, <joerg@joergmeltzer.de> for great patches.

COPYRIGHT & LICENSE

Top

DISCLAIMER OF WARRANTY

Top

BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.

IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.

SEE ALSO

Top

perl, WWW::Shorten, http://bit.ly.


WWW-Shorten-Bitly documentation Contained in the WWW-Shorten-Bitly distribution.
# $Id: Bitly.pm 110 2009-03-22 21:04:27Z pankaj $
# $Author: pankaj $
# $Date: 2009-03-23 02:34:27 +0530 (Mon, 23 Mar 2009) $
# Author: <a href=mailto:pjain@cpan.org>Pankaj Jain</a>
################################################################################################################################
package WWW::Shorten::Bitly;

use warnings;
use strict;
use Carp;

use base qw( WWW::Shorten::generic Exporter );

use JSON::Any;

require XML::Simple;
require Exporter;

our %EXPORT_TAGS = ( 'all' => [ qw() ] );
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
our @EXPORT = qw(new version);

my @ISA = qw(Exporter);

use vars qw( @ISA @EXPORT );


BEGIN {
    our $VERSION = do { my @r = (q$Revision: 1.14 $ =~ /\d+/g); sprintf "%1d."."%02d" x $#r, @r }; # must be all one line, for MakeMaker
    $WWW::Shorten::Bitly::VERBOSITY = 2;
}

# ------------------------------------------------------------


sub new {
    my ($class) = shift;
    my %args = @_;
    $args{source} ||= "perlteknatusbitly";
    $args{jmp} ||= 0;
    use File::Spec;
    my $bitlyrc = $^O =~/Win32/i ? File::Spec->catfile($ENV{HOME}, "_bitly") : File::Spec->catfile($ENV{HOME}, ".bitly");
    if (-r $bitlyrc){
        open my $fh, "<", $bitlyrc or die "can't open .bitly file $!";
        while(<$fh>){
            $args{USER} ||= $1 if m{^USER=(.*)};
            $args{APIKEY} ||= $1 if m{^APIKEY=(.*)};
        }
        close $fh;
    }
    if (!defined $args{USER} || !defined $args{APIKEY}) {
        carp("USER and APIKEY are both required parameters.\n");
        return -1;
    }
    my $bitly;
    $bitly->{USER} = $args{USER};
    $bitly->{APIKEY} = $args{APIKEY};
    if ( $args{jmp} == 1) {
        $bitly->{BASE} = "http://api.j.mp";
    } else {
        $bitly->{BASE} = "http://api.bit.ly";
    }
    $bitly->{json} = JSON::Any->new;
    $bitly->{browser} = LWP::UserAgent->new(agent => $args{source});
    $bitly->{xml} = new XML::Simple(SuppressEmpty => 1);
    my ($self) = $bitly;
    bless $self, $class;
}


sub makeashorterlink #($;%)
{
    my $url = shift or croak('No URL passed to makeashorterlink');
    my ($user, $apikey) = @_ or croak('No username or apikey passed to makeshorterlink');
    if (!defined $url || !defined $user || !defined $apikey ) {
        croak("url, user and apikey are required for shortening a URL with bit.ly - in that specific order");
        &help();
    }
    my $ua = __PACKAGE__->ua();
    my $bitly;
    $bitly->{json} = JSON::Any->new;
    $bitly->{xml} = new XML::Simple(SuppressEmpty => 1);
    my $biturl = "http://api.bit.ly/shorten";
    $bitly->{response} = $ua->post($biturl, [
        'history' => '1',
        'version' => '2.0.1',
        'longUrl' => $url,
        'login' => $user,
        'apiKey' => $apikey,
    ]);
    $bitly->{response}->is_success || die 'Failed to get bit.ly link: ' . $bitly->{response}->status_line;
    $bitly->{bitlyurl} = $bitly->{json}->jsonToObj($bitly->{response}->{_content})->{results}->{$url}->{shortUrl};
    return unless $bitly->{response}->is_success;
    return $bitly->{bitlyurl};
}

sub makealongerlink #($,%)
{
    my $url = shift or croak('No shortened bit.ly URL passed to makealongerlink');
    my ($user, $apikey) = @_ or croak('No username or apikey passed to makealongerlink');
    my $ua = __PACKAGE__->ua();
    my $bitly;
    my @foo = split(/\//, $url);
    $bitly->{json} = JSON::Any->new;
    $bitly->{xml} = new XML::Simple(SuppressEmpty => 1);
    $bitly->{response} = $ua->post('http://api.bit.ly/expand', [
        'version' => '2.0.1',
        'shortUrl' => $url,
        'login' => $user,
        'apiKey' => $apikey,
    ]);
    $bitly->{response}->is_success || die 'Failed to get bit.ly link: ' . $bitly->{response}->status_line;
    $bitly->{longurl} = $bitly->{json}->jsonToObj($bitly->{response}->{_content})->{results}->{$foo[3]}->{longUrl};
    return undef unless $bitly->{response}->is_success;
    my $content = $bitly->{response}->content;
# return undef if $content eq 'ERROR';
    return $bitly->{longurl};
}


sub shorten {
    my $self = shift;
    my %args = @_;
    if (!defined $args{URL}) {
        croak("URL is required.\n");
        return -1;
    }
    $self->{response} = $self->{browser}->post($self->{BASE} . '/shorten', [
        'history' => '1',
        'version' => '2.0.1',
        'longUrl' => $args{URL},
        'login' => $self->{USER},
        'apiKey' => $self->{APIKEY},
    ]);
    $self->{response}->is_success || die 'Failed to get bit.ly link: ' . $self->{response}->status_line;
    return undef if ( $self->{json}->jsonToObj($self->{response}->{_content})->{errorCode} != 0 );
    $self->{bitlyurl} = $self->{json}->jsonToObj($self->{response}->{_content})->{results}->{$args{URL}}->{shortUrl};
    return $self->{bitlyurl} if ( $self->{json}->jsonToObj($self->{response}->{_content})->{errorCode} == 0 );
}

sub expand {
    my $self = shift;
    my %args = @_;
    if (!defined $args{URL}) {
        croak("URL is required.\n");
        return -1;
    }
    my @foo = split(/\//, $args{URL});
    $self->{response} = $self->{browser}->get($self->{BASE} . '/expand', [
        'history' => '1',
        'version' => '2.0.1',
        'shortUrl' => $args{URL},
        'login' => $self->{USER},
        'apiKey' => $self->{APIKEY},
    ]);
    $self->{response}->is_success || die 'Failed to get bit.ly link: ' . $self->{response}->status_line;
    return undef if ( $self->{json}->jsonToObj($self->{response}->{_content})->{errorCode} != 0 );
    $self->{longurl} = $self->{json}->jsonToObj($self->{response}->{_content})->{results}->{$foo[3]}->{longUrl};
    return $self->{longurl} if ( $self->{json}->jsonToObj($self->{response}->{_content})->{errorCode} == 0 );
}

sub info {
    my $self = shift;
    $self->{response} = $self->{browser}->post($self->{BASE} . '/info', [
        'format' => 'xml',
        'version' => '2.0.1',
        'shortUrl' => $self->{bitlyurl},
        'login' => $self->{USER},
        'apiKey' => $self->{APIKEY},
    ]);
    $self->{response}->is_success || die 'Failed to get bit.ly link: ' . $self->{response}->status_line;
    $self->{$self->{bitlyurl}}->{content} = $self->{xml}->XMLin($self->{response}->{_content});
    $self->{$self->{bitlyurl}}->{errorCode} = $self->{$self->{bitlyurl}}->{content}->{errorCode};
    if ($self->{$self->{bitlyurl}}->{errorCode} == 0 ) {
        $self->{$self->{bitlyurl}}->{info} = $self->{$self->{bitlyurl}}->{content}->{results}->{doc};
        return $self->{$self->{bitlyurl}}->{info};
    } else {
        return;
    }
}

sub clicks {
    my $self = shift;
    $self->{response} = $self->{browser}->post($self->{BASE} . '/stats', [
        'format' => 'xml',
        'version' => '2.0.1',
        'shortUrl' => $self->{bitlyurl},
        'login' => $self->{USER},
        'apiKey' => $self->{APIKEY},
    ]);
    $self->{response}->is_success || die 'Failed to get bit.ly link: ' . $self->{response}->status_line;
    $self->{$self->{bitlyurl}}->{content} = $self->{xml}->XMLin($self->{response}->{_content});
    $self->{$self->{bitlyurl}}->{errorCode} = $self->{$self->{bitlyurl}}->{content}->{errorCode};
    if ($self->{$self->{bitlyurl}}->{errorCode} == 0 ) {
        $self->{$self->{bitlyurl}}->{clicks} = $self->{$self->{bitlyurl}}->{content}->{results};
        return $self->{$self->{bitlyurl}}->{clicks};
    } else {
        return;
    }
}

sub errors {
    my $self = shift;
    $self->{response} = $self->{browser}->post($self->{BASE} . '/errors', [
        'version' => '2.0.1',
        'login' => $self->{USER},
        'apiKey' => $self->{APIKEY},
    ]);
    $self->{response}->is_success || die 'Failed to get bit.ly link: ' . $self->{response}->status_line;
    $self->{$self->{bitlyurl}}->{content} = $self->{xml}->XMLin($self->{response}->{_content});
    $self->{$self->{bitlyurl}}->{errorCode} = $self->{$self->{bitlyurl}}->{content}->{errorCode};
    if ($self->{$self->{bitlyurl}}->{errorCode} == 0 ) {
        $self->{$self->{bitlyurl}}->{clicks} = $self->{$self->{bitlyurl}}->{content}->{results};
        return $self->{$self->{bitlyurl}}->{clicks};
    } else {
        return;
    }
}

sub version {
    my $self = shift;
    my($version) = shift;# not sure why $version isn't being set. need to look at it
    warn "Version $version is later then $WWW::Shorten::Bitly::VERSION. It may not be supported" if (defined ($version) && ($version > $WWW::Shorten::Bitly::VERSION));
    return $WWW::Shorten::Bitly::VERSION;
}#version


1; # End of WWW::Shorten::Bitly