#!/usr/bin/env perl
use strict;
use warnings;
#################
# CONFIGURATION #
#################
my $user = 'username';
my $passwd = 'password';
my $siteid = '10101010';
my $cookiejar = "$ENV{HOME}/.makebnlinks";
my $template =<<'-EOT-';
&sourceid=&bfpid=&bfmtype=" border="0" width="1" height="1" nosave="1" />
&sourceid=&bfpid=&bfmtype=" target="_top">
" border="0" align="center" alt="" />
-EOT-
#### NO NEED TO EDIT ANYTHING BELOW THIS LINE ####
use Getopt::Long;
use HTML::Template;
use LWP::UserAgent;
use Pod::Usage;
my $merchantid = '2181';
my $loginurl = 'http://bn.reporting.net/networks/affiliates/bf_login';
my $makelinkurl = 'http://bn.reporting.net/product/search/XmlProdSearchServlet';
my $help = 0;
my $isbn = '';
Getopt::Long::Configure('gnu_getopt');
GetOptions (
'isbn=s' => \$isbn,
'help|h|?' => \$help,
) or pod2usage(-exitstatus => 2, -verbose => 1);
pod2usage(-exitstatus => 0, -verbose => 2) if $help;
die "Must specify ISBN!\n" unless $isbn;
my $params = getlink($isbn);
my $output = HTML::Template->new_scalar_ref(\$template, die_on_bad_params => 0);
$output->param($params);
print $output->output, "\n";
sub getlink
{
my ($isbn) = @_ or die;
my $ua = LWP::UserAgent->new();
push @{$ua->requests_redirectable}, 'POST';
$ua->cookie_jar({file => $cookiejar, autosave => 1, ignore_discard => 1});
# simulate a login...
my $res = $ua->post($loginurl,
{
merchant_in => $merchantid,
username_in => $user,
password_in => $passwd,
});
#...and get a cookie. We don't need to check the status code.
$res = $ua->post($res->request->uri);
# Get the link
$res = $ua->post($makelinkurl,
{ tag_type_in => 'PRODUCT',
rep_firm_id_in => $merchantid,
merchandise_type_id_in => '102',
site_id_in => $siteid,
product_field_key_in => $isbn,
});
if ($res->is_success)
{
my %params;
my @params = split /&/, $res->request->uri;
foreach my $param (@params)
{
my($key, $value) = split /=/, $param;
$value =~ s/\+/ /g;
$params{$key} = $value unless $key =~ /^http/;
# unescape any URI escape sequences. turn off annoying warnings.
no warnings;
$params{$key} =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
use warnings;
}
return \%params;
}
# something must have gone wrong. Display LWPs' error message
die 'Uh-oh!: ' . $res->request->uri . ' - ' . $res->status_line . "\n";
}
__END__
=pod
=head1 NAME
makebnlinks -- Allow Affiliates to Create Links to Barnes & Noble Merchandise
=head1 SYNOPSIS
B B<--isbn=>I B<[-?|-h]>
=head1 DESCRIPTION
Given a books' ISBN or number, this script will make a link into the
Barnes & Noble website (L) with your
affiliate information. Then if anyone clicks on the link and buys the book,
you will get credit. This is a replacement for the web form Barnes & Noble
have for making links.
=head1 OPTIONS
=over 4
=item B<--isbn>
The ISBN number of the book you wish to make a link for. This option is
required.
=item B<--help>, B<-h>, B<-?>
Display this help text.
=head1 INSTALLATION
Make the script executable. You will also need the B and
B packages from CPAN.
=head1 CONFIGURATION
There are five values you can change in the configuration section at the
top of the script:
=over 4
=item I<$user>
Your Barnes & Noble affiliate user ID.
=item I<$passwd>
Your Barnes & Noble affiliate password.
=item I<$siteid>
Your Barnes & Noble affiliate site id. If you don't know it, you can find
out your site id by logging into the affiliate site. The address bar of your
browser should display a url with a bit that says I and
then some numbers. Those numbers are the site id.
=item I<$cookiejar>
Where you want B to store the cookie the website sends. You need
the cookie in order to create links without getting a "bad login" error.
=item I<$template>
A string containing the template for generating output. It is fed
through B (See L.)
=back
=head2 The Template
The template can contain any text you want but typically it will be HTML for
creating a link to a particular book. Certain variables can be interpolated
into the template using B syntax. These variables are:
=over 4
=item I
Will always be 102 indicating the merchandise is a book.
=item I
A URL to a small image of the books' cover.
=item I
Will always be 'Book'.
=item I
=item I
The first and second lines of the product description. One or both values may
be empty.
=item I
Your affiliate site id.
=item I
The value 2181 (Barnes & Nobles' firm id.)
=item I
The title (and possibly subtitle) of the book.
=item I
The ISBN number of the book.
=item I
Probably the link-producing XML APIs' version number. Always 1.
=back
=head1 BUGS
The only type of merchandise supported so far is books.
=head1 AUTHOR
Jaldhar H. Vyas Ejaldhar@braincells.comE
=head1 LICENSE
This code is free software under the Crowley Public License ("Do what
thou wilt shall be the whole of the license")
=head1 SEE ALSO
L, L, L
=head1 VERSION
1.0 -- Jul 22, 2003
=cut