/[perlscripts]/makebnlinks
ViewVC logotype

Contents of /makebnlinks

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2 - (show annotations)
Mon Feb 14 10:57:44 2005 UTC (12 years, 7 months ago) by jaldhar
File size: 6099 byte(s)
Added makebnlinks.
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4
5
6 #################
7 # CONFIGURATION #
8 #################
9 my $user = 'username';
10 my $passwd = 'password';
11 my $siteid = '10101010';
12 my $cookiejar = "$ENV{HOME}/.makebnlinks";
13 my $template =<<'-EOT-';
14 <p align="center">
15 <img src="http://service.bfast.com/bfast/serve?bfmid=<!-- TMPL_VAR NAME="rep_firm_id_in" -->&sourceid=<!-- TMPL_VAR NAME="site_id_in" -->&bfpid=<!-- TMPL_VAR NAME="product_field_key_in" -->&bfmtype=<!-- TMPL_VAR NAME="mtype_in" -->" border="0" width="1" height="1" nosave="1" />
16 <a href="http://service.bfast.com/bfast/click?bfmid=<!-- TMPL_VAR NAME="rep_firm_id_in" -->&sourceid=<!-- TMPL_VAR NAME="site_id_in" -->&bfpid=<!-- TMPL_VAR NAME="product_field_key_in" -->&bfmtype=<!-- TMPL_VAR NAME="mtype_in" -->" target="_top">
17 <img src="<!-- TMPL_VAR NAME="product_image_url_in" -->" border="0" align="center" alt="<!-- TMPL_VAR NAME="product_name_in" -->" /><br />
18 <!-- TMPL_VAR NAME="product_name_in" --></a>
19 </p>
20 -EOT-
21 #### NO NEED TO EDIT ANYTHING BELOW THIS LINE ####
22
23
24 use Getopt::Long;
25 use HTML::Template;
26 use LWP::UserAgent;
27 use Pod::Usage;
28
29 my $merchantid = '2181';
30 my $loginurl = 'http://bn.reporting.net/networks/affiliates/bf_login';
31 my $makelinkurl = 'http://bn.reporting.net/product/search/XmlProdSearchServlet';
32 my $help = 0;
33 my $isbn = '';
34
35 Getopt::Long::Configure('gnu_getopt');
36 GetOptions (
37 'isbn=s' => \$isbn,
38 'help|h|?' => \$help,
39 ) or pod2usage(-exitstatus => 2, -verbose => 1);
40 pod2usage(-exitstatus => 0, -verbose => 2) if $help;
41
42 die "Must specify ISBN!\n" unless $isbn;
43
44 my $params = getlink($isbn);
45
46 my $output = HTML::Template->new_scalar_ref(\$template, die_on_bad_params => 0);
47 $output->param($params);
48 print $output->output, "\n";
49
50 sub getlink
51 {
52 my ($isbn) = @_ or die;
53
54 my $ua = LWP::UserAgent->new();
55 push @{$ua->requests_redirectable}, 'POST';
56 $ua->cookie_jar({file => $cookiejar, autosave => 1, ignore_discard => 1});
57
58 # simulate a login...
59 my $res = $ua->post($loginurl,
60 {
61 merchant_in => $merchantid,
62 username_in => $user,
63 password_in => $passwd,
64 });
65 #...and get a cookie. We don't need to check the status code.
66 $res = $ua->post($res->request->uri);
67
68 # Get the link
69 $res = $ua->post($makelinkurl,
70 { tag_type_in => 'PRODUCT',
71 rep_firm_id_in => $merchantid,
72 merchandise_type_id_in => '102',
73 site_id_in => $siteid,
74 product_field_key_in => $isbn,
75 });
76
77 if ($res->is_success)
78 {
79 my %params;
80 my @params = split /&/, $res->request->uri;
81 foreach my $param (@params)
82 {
83 my($key, $value) = split /=/, $param;
84 $value =~ s/\+/ /g;
85 $params{$key} = $value unless $key =~ /^http/;
86 # unescape any URI escape sequences. turn off annoying warnings.
87 no warnings;
88 $params{$key} =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
89 use warnings;
90 }
91 return \%params;
92 }
93
94 # something must have gone wrong. Display LWPs' error message
95 die 'Uh-oh!: ' . $res->request->uri . ' - ' . $res->status_line . "\n";
96 }
97
98 __END__
99
100 =pod
101
102 =head1 NAME
103
104 makebnlinks -- Allow Affiliates to Create Links to Barnes & Noble Merchandise
105
106 =head1 SYNOPSIS
107
108 B<makebnlinks> B<--isbn=>I<isbn> B<[-?|-h]>
109
110 =head1 DESCRIPTION
111
112 Given a books' ISBN or number, this script will make a link into the
113 Barnes & Noble website (L<http://www.barnesandnoble.com>) with your
114 affiliate information. Then if anyone clicks on the link and buys the book,
115 you will get credit. This is a replacement for the web form Barnes & Noble
116 have for making links.
117
118 =head1 OPTIONS
119
120 =over 4
121
122 =item B<--isbn>
123
124 The ISBN number of the book you wish to make a link for. This option is
125 required.
126
127 =item B<--help>, B<-h>, B<-?>
128
129 Display this help text.
130
131 =head1 INSTALLATION
132
133 Make the script executable. You will also need the B<LWP> and
134 B<HTML::Template> packages from CPAN.
135
136 =head1 CONFIGURATION
137
138 There are five values you can change in the configuration section at the
139 top of the script:
140
141 =over 4
142
143 =item I<$user>
144
145 Your Barnes & Noble affiliate user ID.
146
147 =item I<$passwd>
148
149 Your Barnes & Noble affiliate password.
150
151 =item I<$siteid>
152
153 Your Barnes & Noble affiliate site id. If you don't know it, you can find
154 out your site id by logging into the affiliate site. The address bar of your
155 browser should display a url with a bit that says I<merchant_in=2181|> and
156 then some numbers. Those numbers are the site id.
157
158 =item I<$cookiejar>
159
160 Where you want B<makebnlinks> to store the cookie the website sends. You need
161 the cookie in order to create links without getting a "bad login" error.
162
163 =item I<$template>
164
165 A string containing the template for generating output. It is fed
166 through B<HTML::Template> (See L<HTML::Template>.)
167
168 =back
169
170 =head2 The Template
171
172 The template can contain any text you want but typically it will be HTML for
173 creating a link to a particular book. Certain variables can be interpolated
174 into the template using B<HTML::Template> syntax. These variables are:
175
176 =over 4
177
178 =item I<merchandise_type_id_in>
179
180 Will always be 102 indicating the merchandise is a book.
181
182 =item I<product_image_url_in>
183
184 A URL to a small image of the books' cover.
185
186 =item I<mtype_in>
187
188 Will always be 'Book'.
189
190 =item I<product_desc_1_in>
191
192 =item I<product_desc_2_in>
193
194 The first and second lines of the product description. One or both values may
195 be empty.
196
197 =item I<site_id_in>
198
199 Your affiliate site id.
200
201 =item I<rep_firm_id_in>
202
203 The value 2181 (Barnes & Nobles' firm id.)
204
205 =item I<product_name_in>
206
207 The title (and possibly subtitle) of the book.
208
209 =item I<product_field_key_in>
210
211 The ISBN number of the book.
212
213 =item I<xml_api_ind>
214
215 Probably the link-producing XML APIs' version number. Always 1.
216
217 =back
218
219 =head1 BUGS
220
221 The only type of merchandise supported so far is books.
222
223
224 =head1 AUTHOR
225
226 Jaldhar H. Vyas E<lt>jaldhar@braincells.comE<gt>
227
228 =head1 LICENSE
229
230 This code is free software under the Crowley Public License ("Do what
231 thou wilt shall be the whole of the license")
232
233 =head1 SEE ALSO
234
235 L<HTML::Template>, L<http://bn.reporting.net>, L<http://www.barnesandnoble.com>
236
237 =head1 VERSION
238
239 1.0 -- Jul 22, 2003
240
241 =cut

Properties

Name Value
svn:executable *

webmaster@braincells.com
ViewVC Help
Powered by ViewVC 1.1.26