Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked


by jaldhar (Vicar)
on Jul 22, 2003 at 07:19 UTC ( #276638=sourcecode: print w/replies, xml ) Need Help??
Category: HTML Utility
Author/Contact Info Jaldhar H. Vyas

If you are a Barnes & Noble affiliate, you'll find this script handy. It allows you to make a link to a book on the site without going through their clunky web form. You can also customize the output unlike on the B & N site. This script only does books but I'm going to do a CPAN module which will support all the types of merchandise B & N sells. I'm thinking of calling it Business::Bnaffiliate. Does that sound ok?

#!/usr/bin/env perl
use strict;
use warnings;

my $user = 'username';
my $passwd = 'password';
my $siteid = '10101010';
my $cookiejar = "$ENV{HOME}/.makebnlinks";
my $template =<<'-EOT-';
<p align="center">
<img src="<!-- TMPL_VAR NAM
+E="rep_firm_id_in" -->&sourceid=<!-- TMPL_VAR NAME="site_id_in" -->&b
+fpid=<!-- TMPL_VAR NAME="product_field_key_in" -->&bfmtype=<!-- TMPL_
+VAR NAME="mtype_in" -->" border="0" width="1" height="1" nosave="1" /
<a href="<!-- TMPL_VAR NAME
+="rep_firm_id_in" -->&sourceid=<!-- TMPL_VAR NAME="site_id_in" -->&bf
+pid=<!-- TMPL_VAR NAME="product_field_key_in" -->&bfmtype=<!-- TMPL_V
+AR NAME="mtype_in" -->" target="_top">
<img src="<!-- TMPL_VAR NAME="product_image_url_in" -->" border="0" al
+ign="center" alt="<!-- TMPL_VAR NAME="product_name_in" -->"  /><br />
<!-- TMPL_VAR NAME="product_name_in" --></a>

use Getopt::Long;
use HTML::Template;
use LWP::UserAgent;
use Pod::Usage;

my $merchantid = '2181';
my $loginurl = '';
my $makelinkurl = '
my $help = 0;
my $isbn = '';

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_par
+ams => 0);
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 . "



=head1 NAME

makebnlinks -- Allow Affiliates to Create Links to Barnes & Noble Merc


B<makebnlinks> B<--isbn=>I<isbn> B<[-?|-h]>


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 & 
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 

=item B<--help>, B<-h>, B<-?>

Display this help text.


Make the script executable.  You will also need the B<LWP> and
B<HTML::Template> packages from CPAN.


There are five values you can change in the configuration section at t
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 
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<merchant_in=2181|>
+ and
then some numbers.  Those numbers are the site id.

=item I<$cookiejar>

Where you want B<makebnlinks> to store the cookie the website sends.  
+You need
the cookie in order to create links without getting a "bad login" erro

=item I<$template>

A string containing the template for generating output.  It is fed
through B<HTML::Template> (See L<HTML::Template>.)


=head2 The Template

The template can contain any text you want but typically it will be HT
+ML for
creating a link to a particular book.  Certain variables can be interp
into the template using B<HTML::Template> syntax.  These variables are

=over 4

=item I<merchandise_type_id_in>

Will always be 102 indicating the merchandise is a book.

=item I<product_image_url_in>

A URL to a small image of the books' cover.

=item I<mtype_in>

Will always be 'Book'.

=item I<product_desc_1_in>

=item I<product_desc_2_in>

The first and second lines of the product description.  One or both va
+lues may
be empty.

=item I<site_id_in>

Your affiliate site id.

=item I<rep_firm_id_in>

The value 2181 (Barnes & Nobles' firm id.)

=item I<product_name_in>

The title (and possibly subtitle) of the book.

=item I<product_field_key_in>

The ISBN number of the book.

=item I<xml_api_ind>

Probably the link-producing XML APIs' version number.  Always 1.


=head1 BUGS

The only type of merchandise supported so far is books.

=head1 AUTHOR

Jaldhar H. Vyas E<lt>jaldhar@braincells.comE<gt>

=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<HTML::Template>, L<>, L<http://www.barnesandn>

=head1 VERSION

1.0 -- Jul 22, 2003

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://276638]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (9)
As of 2017-07-26 09:30 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (387 votes). Check out past polls.