The Code
To run this code, you'll need to set the email address and password
you use to log into your associates account. This script will then
log into the account and download the appropriate sales report. Once
the script has the report, it will reformat it as
HTML.
#!/usr/bin/perl -w
# get_earnings_report.pl
#
# Logs into Amazon, downloads earning report,
# and writes an HTML version for your site.
# Usage: perl get_earnings_report.pl
use warnings;
use strict;
use URI::Escape;
use HTTP::Cookies;
use LWP::UserAgent;
# Set your associates account info.
my $email = 'insert Associates email address';
my $pass = 'insert Associates password';
my $aftag = 'insert affiliate tag';
# Create a user agent object.
# and fake the agent string.
my $ua = LWP::UserAgent->new;
$ua->agent("(compatible; MSIE 4.01; MSN 2.5; AOL 4.0; Windows 98)");
$ua->cookie_jar({}); # in-memory cookie jar.
# Request earning reports, logging in as one pass.
my $rpturl = "http://associates.amazon.com/exec/panama/login/".
"attempt/customer/associates/no-customer-id/25/".
"associates/resources/reporting/earnings/";
my $rptreq = HTTP::Request->new(POST => $rpturl);
my $rptdata = "report-type=shipments-by-item". # get individual items
"&date-selection=qtd". # all earnings this quarter
"&login_id=".uri_escape($email). # our email address.
"&login_password=".uri_escape($pass). # and password.
"&submit.download=Download my report". # get downloadble.
"&enable-login-post=true"; # login and post at once.
$rptreq->content_type('application/x-www-form-urlencoded');
$rptreq->content($rptdata); my $report = $ua->request($rptreq);
# Uncomment the following line to see
# the report if you need to debug
# print $report->content;
# Set the report to array.
my @lines = split(/\n/, $report->content);
# Get the time period.
my @fromdate = split(/ /, $lines[1]);
my @todate = split(/ /, $lines[2]);
my $from = $fromdate[1];
my $to = $todate[1];
# Print header...
print "<html><body>";
print "<h2>Items Purchased Through This Site</h2>";
print "from $from to $to <br><br>\n";
print "<ul>";
# Loop through the
# rest of the report
splice(@lines,0,5);
foreach my $line (@lines) {
my @fields = split(/ /, $line);
my $title = $fields[1];
my $asin = $fields[2];
my $edition = $fields[4];
my $items = $fields[8];
# Format items as HTML for display
print "<li><a href='http://www.amazon.com/o/ASIN/$asin/ref=nosim/".
"$aftag'>$title</a> ($items) $edition <br>\n";
}
print "</ul></body></html>";
You'll notice that $rpturl holds a standard
http:// URL that's used to log into Amazon. It's
not encrypted, so be sure you're comfortable with sending your
associates password over a standard connection before running this
script. If you'd like the connection to be encrypted, you'll need a
Perl package called Net::SSL (http://search.cpan.org/author/CHAMAS/Crypt-SSLeay-0.51/).
This package won't be referenced specifically in the script, but it
provides SSL support to the LWP package making the request. Not all
ISPs have Net::SSL installed, so check with them to see if it's
available. Once you've confirmed that Net::SSL is present, change the
URL in $rpturl to begin with
https://. This will keep your entire request and
response encrypted.
1) use \t instead of " " for tab. I have emacs set to expand tabs to spaces, so of course when I pasted the script into emacs, my split no longer worked:
my @fields = split(/\t/, $line);
2)
One slight drawback of the associates reports is that for some reason it shows many rows of the same ASIN.
I added some consolidation code so it would sum up the total number of each ASIN sold, then print the summary instead of many repeated lines.
Code changes here:
my %asinSum;
my %asinTitle;
my %asinEdition;
my @asinOrder; # use @asinOrder to track the order they were displayed in
foreach my $line (@lines) {
my @fields = split(/\t/, $line);
my $title = $fields[1];
my $asin = $fields[2];
my $edition = $fields[4];
my $items = $fields[8];
if(!defined($asinTitle{$asin})) {
$asinTitle{$asin}=$title;
push(@asinOrder, $asin);
}
if(!defined($asinEdition{$asin})) { $asinEdition{$asin}=$edition; }
if(!defined($asinSum{$asin})) { $asinSum{$asin}=0; }
$asinSum{$asin} += $items;
# Format items as HTML for display
# print "\<li\>\$title ($items) $edition [sum=$asinSum{$asin}]\<br\>\n";
}
print "\</ul\><hr>Totals:\<br\>\n";
for my $asin (@asinOrder) {
# Format items as HTML for display
print "\<li\>\$asinTitle{$asin} ($asinSum{$asin}) $asinEdition{$asin} \<br\>\n";
}
print "\</body\>\</html\>";