Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Updated to work with v3 bitly.com API. A few things are still missing…

… but will be added over time.
  • Loading branch information...
commit 7eaa797209af314cdbc118305747ac0f4b4d8f46 1 parent 4e5b6cc
Pankaj Jain authored
Showing with 253 additions and 150 deletions.
  1. +2 −2 README
  2. +251 −148 lib/WWW/Shorten/Bitly.pm
View
4 README
@@ -40,8 +40,8 @@ You can also look for information at:
COPYRIGHT AND LICENCE
-Copyright (C) 2009 Pankaj Jain
-Copyright (C) 2009 Teknatus Solutions LLC
+Copyright (C) 2009-2011 Pankaj Jain
+Copyright (C) 2009-2011 Teknatus Solutions LLC
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
View
399 lib/WWW/Shorten/Bitly.pm
@@ -10,7 +10,6 @@ use strict;
use Carp;
use base qw( WWW::Shorten::generic Exporter );
-
use JSON::Any;
require XML::Simple;
@@ -24,10 +23,12 @@ my @ISA = qw(Exporter);
use vars qw( @ISA @EXPORT );
+use constant BASE_JMP => 'http://api.j.mp';
+use constant BASE_BLY => 'http://api.bitly.com';
=head1 NAME
-WWW::Shorten::Bitly - Interface to shortening URLs using L<http://bit.ly>
+WWW::Shorten::Bitly - Interface to shortening URLs using L<http://bitly.com>
=head1 VERSION
@@ -45,10 +46,10 @@ BEGIN {
=head1 SYNOPSIS
-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 an easy interface for shortening URLs using http://bitly.com. In addition to shortening URLs, you can pull statistics that bitly.com gathers regarding each shortened
+WWW::Shorten::Bitly uses XML::Simple to convert the xml response and JSON::Any to convert JSON responses 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 C<makeashorterlink> and C<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.
+WWW::Shorten::Bitly provides two interfaces. The first is the common C<makeashorterlink> and C<makealongerlink> that WWW::Shorten provides. However, due to the way the bitly.com API works, additional arguments are required. The second provides a better way of retrieving additional information and statistics about a bitly.com URL.
use WWW::Shorten::Bitly;
@@ -73,25 +74,26 @@ $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};
+say "Title of the page is " . $info->{title};
+say "Created by " . $info->{created_by};
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}
+say "Total number of clicks received: " . $clicks->{user_clicks};
+say "Total number of global clicks received are: " . $clicks->{global_clicks};
+
+Please remember to check out C<http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/v3/info> for more details on V3 of the Bitly.com API
=head1 FUNCTIONS
=head2 new
-Create a new bit.ly object using your bit.ly user id and bit.ly api key.
+Create a new bitly.com object using your bitly.com user id and bitly.com 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:
+to use bitly.com'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",
@@ -121,10 +123,10 @@ sub new {
my $bitly;
$bitly->{USER} = $args{USER};
$bitly->{APIKEY} = $args{APIKEY};
- if ( $args{jmp} == 1) {
- $bitly->{BASE} = "http://api.j.mp";
+ if ($args{jmp} == 1) {
+ $bitly->{BASE} = BASE_JMP;
} else {
- $bitly->{BASE} = "http://api.bit.ly";
+ $bitly->{BASE} = BASE_BLY;
}
$bitly->{json} = JSON::Any->new;
$bitly->{browser} = LWP::UserAgent->new(agent => $args{source});
@@ -136,10 +138,10 @@ sub new {
=head2 makeashorterlink
-The function C<makeashorterlink> will call the bit.ly API site passing it
-your long URL and will return the shorter bit.ly version.
+The function C<makeashorterlink> will call the bitly.com API site passing it
+your long URL and will return the shorter bitly.com version.
-bit.ly requires the use of a user id and API key to shorten links.
+bitly.com requires the use of a user id and API key to shorten links.
j.mp is not currently supported for makeashorterlink
@@ -150,23 +152,24 @@ 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");
+ croak("url, user and apikey are required for shortening a URL with bitly.com - 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";
+ my $biturl = BASE_BLY . '/v3/shorten';
$bitly->{response} = $ua->post($biturl, [
+ 'format' => 'json',
'history' => '1',
- 'version' => '2.0.1',
+ 'version' => '3.0.0',
'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};
+ $bitly->{response}->is_success || die 'Failed to get bitly.com link: ' . $bitly->{response}->status_line;
+ $bitly->{bitlyurl} = $bitly->{json}->jsonToObj($bitly->{response}->{_content})->{data}->{url};
return unless $bitly->{response}->is_success;
return $bitly->{bitlyurl};
}
@@ -174,8 +177,8 @@ sub makeashorterlink #($;%)
=head2 makealongerlink
The function C<makealongerlink> does the reverse. C<makealongerlink>
-will accept as an argument either the full bit.ly URL or just the
-bit.ly identifier. bit.ly requires the use of a user name and API
+will accept as an argument either the full bitly.com URL or just the
+bitly.com identifier. bitly.com requires the use of a user name and API
Key when using the API.
If anything goes wrong, then the function will return C<undef>.
@@ -186,21 +189,21 @@ j.mp is not currently supported for makealongerlink
sub makealongerlink #($,%)
{
- my $url = shift or croak('No shortened bit.ly URL passed to makealongerlink');
+ my $url = shift or croak('No shortened bitly.com 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',
+ $bitly->{response} = $ua->post(BASE_BLY . '/v3/expand', [
+ 'version' => '3.0.0',
'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};
+ $bitly->{response}->is_success || die 'Failed to get bitly.com link: ' . $bitly->{response}->status_line;
+ $bitly->{longurl} = $bitly->{json}->jsonToObj($bitly->{response}->{_content})->{data}->{long_url};
return undef unless $bitly->{response}->is_success;
my $content = $bitly->{response}->content;
# return undef if $content eq 'ERROR';
@@ -209,7 +212,7 @@ sub makealongerlink #($,%)
=head2 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.
+Shorten a URL using http://bitly.com. Calling the shorten method will return the shortened URL but will also store it in bitly.com object until the next call is made.
my $url = "http://www.example.com";
my $shortstuff = $bitly->shorten(URL => $url);
@@ -228,22 +231,22 @@ sub shorten {
croak("URL is required.\n");
return -1;
}
- $self->{response} = $self->{browser}->post($self->{BASE} . '/shorten', [
+ $self->{response} = $self->{browser}->post($self->{BASE} . '/v3/shorten', [
'history' => '1',
- 'version' => '2.0.1',
+ 'version' => '3.0.0',
'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 );
+ $self->{response}->is_success || die 'Failed to get bitly.com link: ' . $self->{response}->status_line;
+ return undef if ( $self->{json}->jsonToObj($self->{response}->{_content})->{status_code} != 200 );
+ $self->{bitlyurl} = $self->{json}->jsonToObj($self->{response}->{_content})->{data}->{url};
+ return $self->{bitlyurl} if ( $self->{json}->jsonToObj($self->{response}->{_content})->{status_code} == 200 );
}
=head2 expand
-Expands a shortened bit.ly URL to the original long URL.
+Expands a shortened bitly.com URL to the original long URL.
=cut
sub expand {
@@ -253,183 +256,282 @@ sub expand {
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',
+# my @foo = split(/\//, $args{URL});
+ $self->{response} = $self->{browser}->post($self->{BASE} . '/v3/expand', [
+ 'history' => '1',
+ 'version' => '3.0.0',
'shortUrl' => $args{URL},
- 'login' => $self->{USER},
- 'apiKey' => $self->{APIKEY},
+ '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 );
+ $self->{response}->is_success || die 'Failed to get bitly.com link: ' . $self->{response}->status_line;
+ return undef if ( $self->{json}->jsonToObj($self->{response}->{_content})->{status_code} != 200 );
+ $self->{longurl} = $self->{json}->jsonToObj($self->{response}->{_content})->{data}->{expand}[0]->{long_url};
+ return $self->{longurl} if ( $self->{json}->jsonToObj($self->{response}->{_content})->{status_code} == 200 );
}
=head2 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.
+Get info about a shortened bitly.com 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";
+$bitly->{bitlyurl} = "http://bitly.com/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};
-
+say "Title of the page is " . $info->{title};
+say "Created by " . $info->{created_by};
=cut
+
sub info {
my $self = shift;
- $self->{response} = $self->{browser}->get($self->{BASE} . '/v3/info?format=xml&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} || $self->{$self->{bitlyurl}}->{errorCode} == 0) || $self->{$self->{bitlyurl}}->{content}->{status_code} == 200 ) {
- return $self->{$self->{bitlyurl}}->{content}->{data}->{info};
+ $self->{response} = $self->{browser}->get($self->{BASE} . '/v3/info?shortUrl=' . $self->{bitlyurl} . '&login=' . $self->{USER} . '&apiKey=' . $self->{APIKEY});
+ $self->{response}->is_success || die 'Failed to get bitly.com link: ' . $self->{response}->status_line;
+ $self->{$self->{bitlyurl}}->{content} = $self->{json}->jsonToObj($self->{response}->{_content});
+ if ($self->{$self->{bitlyurl}}->{content}->{status_code} == 200 ) {
+ $self->{$self->{bitlyurl}}->{info} = $self->{$self->{bitlyurl}}->{content}->{data}->{info}[0];
+ return $self->{$self->{bitlyurl}}->{info};
} else {
return;
}
}
-=head2 infojson
-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.
+=head2 clicks
-$bitly->{bitlyurl} = "http://bit.ly/jmv6";
-my $info = $bitly->info();
+Get click thru information for a shortened bitly.com 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.
-say "City referred to is " . $info->{calais}{city}[0];
-say "Companies referred to are " . $info->{calais}{company}[0] . "and " . $info->{calais}{company}[1];
-say "Title of the page is " . $info->{htmlTitle};
+$bitly->{bitlyurl} = "http://bitly.com/jmv6";
+my $clicks = $bitly->clicks();
+say "Total number of clicks received: " . $clicks->{user_clicks};
+say "Total number of global clicks received are: " . $clicks->{global_clicks};
=cut
-sub infojson {
+sub clicks {
my $self = shift;
- $self->{response} = $self->{browser}->post($self->{BASE} . '/info', [
- 'format' => 'json',
- 'version' => '2.0.1',
- 'shortUrl' => $self->{bitlyurl},
- 'login' => $self->{USER},
- 'apiKey' => $self->{APIKEY},
+ $self->{response} = $self->{browser}->get($self->{BASE} . '/v3/clicks?shortUrl=' . $self->{bitlyurl} . '&login=' . $self->{USER} . '&apiKey=' . $self->{APIKEY});
+ $self->{response}->is_success || die 'Failed to get bitly.com link: ' . $self->{response}->status_line;
+ $self->{$self->{bitlyurl}}->{content} = $self->{json}->jsonToObj($self->{response}->{_content});
+# $self->{$self->{bitlyurl}}->{errorCode} = $self->{$self->{bitlyurl}}->{content}->{status_txt};
+ if ($self->{$self->{bitlyurl}}->{content}->{status_code} == 200 ) {
+# $self->{$self->{bitlyurl}}->{clicks} = $self->{$self->{bitlyurl}}->{content}->{results};
+ $self->{$self->{bitlyurl}}->{clicks} = $self->{$self->{bitlyurl}}->{content}->{data}->{clicks}[0];
+ return $self->{$self->{bitlyurl}}->{clicks};
+ } else {
+ return;
+ }
+}
+
+=head2 errors
+
+=cut
+
+sub errors {
+ my $self = shift;
+ warn "errors - deprecated from BitLy API. It will no longer be supported" if (1.14 > $WWW::Shorten::Bitly::VERSION);
+ return;
+ $self->{response} = $self->{browser}->post($self->{BASE} . '/v3/errors', [
+ 'version' => '3.0.0',
+ 'login' => $self->{USER},
+ 'apiKey' => $self->{APIKEY},
]);
- $self->{response}->is_success || die 'Failed to get bit.ly link: ' . $self->{response}->status_line;
- my $hash = $self->{bitlyurl};
- $hash =~ s/http:\/\/bit\.ly\///g;
- $self->{$self->{bitlyurl}}->{content} = $self->{json}->decode($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}->{$hash};
- return $self->{$self->{bitlyurl}}->{info};
+ $self->{response}->is_success || die 'Failed to get bitly.com link: ' . $self->{response}->status_line;
+ $self->{$self->{bitlyurl}}->{content} = $self->{xml}->XMLin($self->{response}->{_content});
+ $self->{$self->{bitlyurl}}->{errorCode} = $self->{$self->{bitlyurl}}->{content}->{status_txt};
+ if ($self->{$self->{bitlyurl}}->{status_code} == 200 ) {
+ $self->{$self->{bitlyurl}}->{clicks} = $self->{$self->{bitlyurl}}->{content}->{results};
+ return $self->{$self->{bitlyurl}}->{clicks};
} else {
return;
}
}
+=head2 version
-=head2 clicksxml
+Gets the module version number
-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.
+=cut
+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
-$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}
+=head2 referrers
+
+Returns an array of hashes
+
+my @ref = $bitly->referrers();
+say "Referrers for " . $bitly->{bitlyurl};
+foreach my $r (@ref) {
+ foreach my $f (@{$r}) {
+ say $f->{clicks} . ' from ' . $f->{referrer};
+ }
+}
=cut
-sub clicksxml {
+sub referrers {
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};
+ $self->{response} = $self->{browser}->get($self->{BASE} . '/v3/referrers?shortUrl=' . $self->{bitlyurl} . '&login=' . $self->{USER} . '&apiKey=' . $self->{APIKEY});
+ $self->{response}->is_success || die 'Failed to get bitly.com link: ' . $self->{response}->status_line;
+ $self->{$self->{bitlyurl}}->{content} = $self->{json}->jsonToObj($self->{response}->{_content});
+ if ($self->{$self->{bitlyurl}}->{content}->{status_code} == 200 ) {
+ $self->{$self->{bitlyurl}}->{referrers} = $self->{$self->{bitlyurl}}->{content}->{data}->{referrers};
+ return $self->{$self->{bitlyurl}}->{referrers};
} else {
return;
}
}
-=head2 clicks
+=head2 countries {
-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.
+Returns an array of hashesh
-$bitly->{bitlyurl} = "http://bit.ly/jmv6";
-my $clicks = $bitly->clicks();
+my @countries = $bitly->countries();
+foreach my $r (@countries) {
+ foreach my $f (@{$r}) {
+ say $f->{clicks} . ' from ' . $f->{country};
+ }
+}
-say "Total number of clicks received: " . $clicks->{clicks};
-say "Total number of direct clicks received are: " . ${$clicks->{referrers}->{nodeKeyVal}[0]}->{direct}
+=cut
+
+sub countries {
+ my $self = shift;
+ $self->{response} = $self->{browser}->get($self->{BASE} . '/v3/countries?shortUrl=' . $self->{bitlyurl} . '&login=' . $self->{USER} . '&apiKey=' . $self->{APIKEY});
+ $self->{response}->is_success || die 'Failed to get bitly.com link: ' . $self->{response}->status_line;
+ $self->{$self->{bitlyurl}}->{content} = $self->{json}->jsonToObj($self->{response}->{_content});
+ if ($self->{$self->{bitlyurl}}->{content}->{status_code} == 200 ) {
+ $self->{$self->{bitlyurl}}->{countries} = $self->{$self->{bitlyurl}}->{content}->{data}->{countries};
+ return $self->{$self->{bitlyurl}}->{countries};
+ } else {
+ return;
+ }
+}
+
+=head2 clicks_by_day {
+
+Returns an array of hashes
+
+my @c = $bitly->clicks_by_day();
+say "Clicks by Day for " . $bitly->{bitlyurl};
+foreach my $r (@c) {
+ foreach my $f (@{$r}) {
+ say $f->{clicks} . ' on ' . $f->{day_start};
+ }
+}
+
+day_start is the timecode as specified by Bitly.com. You can use the following to turn it into a DateTime Object
+
+use DateTime;
+$dt = DateTime->from_epoch( epoch => $epoch );
=cut
-sub clicks {
+sub clicks_by_day {
my $self = shift;
- $self->{response} = $self->{browser}->post($self->{BASE} . '/stats', [
- 'format' => 'json',
- '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->{json}->decode($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};
+ $self->{response} = $self->{browser}->get($self->{BASE} . '/v3/clicks_by_day?shortUrl=' . $self->{bitlyurl} . '&login=' . $self->{USER} . '&apiKey=' . $self->{APIKEY});
+ $self->{response}->is_success || die 'Failed to get bitly.com link: ' . $self->{response}->status_line;
+ $self->{$self->{bitlyurl}}->{content} = $self->{json}->jsonToObj($self->{response}->{_content});
+ if ($self->{$self->{bitlyurl}}->{content}->{status_code} == 200 ) {
+ $self->{$self->{bitlyurl}}->{clicks_by_day} = $self->{$self->{bitlyurl}}->{content}->{data}->{clicks_by_day}[0]->{clicks};
+ return $self->{$self->{bitlyurl}}->{clicks_by_day};
} else {
return;
}
}
-=head2 errors
+=head2 qr_code
+
+Returns the URL for the QR Code
=cut
+sub qr_code {
+ my $self = shift;
+ my %args = @_;
+ $self->{bitlyurl} ||= $args{shorturl};
+ return $self->{bitlyurl} . '.qrcode';
+}
-sub errors {
+=head2 validate
+
+For any given a bitly user login and apiKey, you can validate that the pair is active.
+
+=cut
+
+sub validate {
my $self = shift;
- $self->{response} = $self->{browser}->post($self->{BASE} . '/errors', [
- '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};
+ my %args = @_;
+ $self->{USER} ||= $args{user};
+ $self->{APIKEY} ||= $args{apikey};
+
+ $self->{response} = $self->{browser}->get($self->{BASE} . '/v3/validate?x_login=' . $self->{USER} . '&x_apiKey=' . $self->{APIKEY}. '&login=' . $self->{USER} . '&apiKey=' . $self->{APIKEY});
+ $self->{response}->is_success || die 'Failed to get bitly.com link: ' . $self->{response}->status_line;
+ $self->{$self->{USER}}->{content} = $self->{json}->jsonToObj($self->{response}->{_content});
+ if ($self->{json}->jsonToObj($self->{response}->{_content})->{data}->{valid} == 1) {
+ $self->{$self->{USER}}->{valid} = $self->{$self->{USER}}->{content}->{data}->{valid};
+ return $self->{$self->{USER}}->{valid};
} else {
return;
}
}
-=head2 version
+=head2 bitly_pro_domain
-Gets the module version number
+Will return true or false whether the URL specified is a Bitly Pro Domain
+
+my $bpd = $bitly->bitly_pro_domain(url => 'http://nyti.ms');
+say "This is a Bitly Pro Domain: " . $bpd;
+
+my $bpd2 = $bitly->bitly_pro_domain(url => 'http://example.com');
+say "This is a Bitly Pro Domain: " . $bpd2;
=cut
-sub version {
+
+sub bitly_pro_domain {
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
+ my %args = @_;
+ $self->{USER} ||= $args{user};
+ $self->{APIKEY} ||= $args{apikey};
+ if ($args{url} !~ /bit\.ly/ || $args{url} !~ /j\.mp/) {
+ my @foo = split(/\//, $args{url});
+ my $domain = $foo[2];
+ $self->{response} = $self->{browser}->get($self->{BASE} . '/v3/bitly_pro_domain?domain=' . $domain . '&login=' . $self->{USER} . '&apiKey=' . $self->{APIKEY});
+ $self->{response}->is_success || die 'Failed to get bitly.com link: ' . $self->{response}->status_line;
+ $self->{$args{url}}->{content} = $self->{json}->jsonToObj($self->{response}->{_content});
+ if ($self->{$args{url}}->{content}->{status_code} == 200 ) {
+ $self->{$args{url}}->{bitly_pro_domain} = $self->{$args{url}}->{content}->{data}->{bitly_pro_domain};
+ return $self->{$args{url}}->{bitly_pro_domain};
+ } else {
+ return;
+ }
+ } else {
+ return 1;
+ }
+}
+
+=head2 lookup
+
+=cut
+sub lookup {
+ my $self = shift;
+}
+
+
+=head2 clicks_by_minute
+
+This part of the Bitly APi isn't being implemented because it's virtually impossible to know exactly which minute a clicks is attributed to. ya know, network lag, etc. I'll implement this when Bitly puts some sort of a time code into the results.
+
+=cut
+
=head1 FILES
@@ -487,12 +589,13 @@ L<http://search.cpan.org/dist/WWW-Shorten-Bitly/>
=over
-=item http://bit.ly for a wonderful service.
+=item http://bitly.com for a wonderful service.
+
+=item Larry Wall, Damian Conway, and all the amazing folks giving us Perl and continuing to work on it over the years.
-=item Larry Wall, Damian Conway, and all the amazing folks giving us Perl and
-continuing to work on it over the years.
=item Mizar, C<< <mizar.jp@gmail.com> >>, Peter Edwards, C<<pedwards@cpan.org> >>, Joerg Meltzer, C<< <joerg@joergmeltzer.de> >> for great patches.
+=item Thai Thanh Nguyen, C<< <thai@thaiandhien.com> >> for patches to support the Bitly.com v3 API
=back
@@ -500,10 +603,10 @@ continuing to work on it over the years.
=over
-=item Copyright (c) 2009 Pankaj Jain, All Rights Reserved L<http://blog.linosx.com>.
+=item Copyright (c) 2009-2010 Pankaj Jain, All Rights Reserved L<http://blog.pjain.me>.
-=item Copyright (c) 2009 Teknatus Solutions LLC, All Rights Reserved
-L<http://www.teknatus.com>.
+=item Copyright (c) 2009-2010 Teknatus Solutions LLC, All Rights Reserved
+L<http://teknatus.com>.
=back
@@ -535,7 +638,7 @@ SUCH DAMAGES.
=head1 SEE ALSO
-L<perl>, L<WWW::Shorten>, L<http://bit.ly>.
+L<perl>, L<WWW::Shorten>, L<http://bitly.com>.
=cut
Please sign in to comment.
Something went wrong with that request. Please try again.