http://qs321.pair.com?node_id=276638
Category: HTML Utility
Author/Contact Info Jaldhar H. Vyas jaldhar@braincells.com
Description:

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;


#################
# CONFIGURATION #
#################
my $user = 'username';
my $passwd = 'password';
my $siteid = '10101010';
my $cookiejar = "$ENV{HOME}/.makebnlinks";
my $template =<<'-EOT-';
<p align="center">
<img src="http://service.bfast.com/bfast/serve?bfmid=<!-- 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="http://service.bfast.com/bfast/click?bfmid=<!-- 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>
</p>
-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/XmlProdSearc
+hServlet';
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_par
+ams => 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 Merc
+handise

=head1 SYNOPSIS

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

=head1 DESCRIPTION

Given a books' ISBN or number, this script will make a link into the
Barnes & Noble website (L<http://www.barnesandnoble.com>)  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<LWP> and
B<HTML::Template> packages from CPAN.

=head1 CONFIGURATION

There are five values you can change in the configuration section at t
+he
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<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
+r.

=item I<$template>

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

=back

=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
+olated
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.

=back

=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<http://bn.reporting.net>, L<http://www.barnesandn
+oble.com>

=head1 VERSION

1.0 -- Jul 22, 2003

=cut