The hack referenced in Publishing Amazon.com Associates Statistics Help (Hack #55 in Spidering Hacks) doesn't work because Amazon changed how things work, so I whipped up a replacement. For giggles I Templatized it. [id://476730;edithistory|node history]
#!/usr/bin/perl =head1 NAME amazon_associates_report =head1 SYNOPSIS # use command line arguments $ amazon_associates_report [-e ACCOUNT_EMAIL_ADDRESS ] [-p ACCOUNT +_PASSWORD ] [-s STORE_NAME ] [-t TEMPLATE_TOOLKIT_TEMPLATE ] $ amazon_associates_report brian.d.foy@gmail.com foobar theperlrev +iew-20 # use environment variables $ export AMAZON_ASSOCIATES_EMAIL=brian.d.foy@gmail.com $ export AMAZON_ASSOCIATES_PASS=foobar $ export AMAZON_ASSOCIATES_STORE=theperlreview-20 $ export AMAZON_ASSOCIATES_TEMPLATE=/Users/brian/.templates/associ +ates.tt $ amazon_associates_report =head1 DESCRIPTION This script logs in to Amazon Associates, selects the right store, and + downloads the data for the current quarter. It hands the data to a Template Toolkit template (using a default if you don't specify one). =head2 Command line arguments =over 4 =item -e EMAIL_ADDRESS This is the email address you use to log into Amazon Associates. Alternately you can use the AMAZON_ASSOCIATES_EMAIL environment variable. =item -p PASSWORD This is the email address you use to log into Amazon Associates. Alternately you can use the AMAZON_ASSOCIATES_PASS environment variable. =item -s STORE This is the name of the Amazon Associates store you want to check. Most people will have a single store and in that case this switch isn't needed. Alternately you can use the AMAZON_ASSOCIATES_STORE environment variable. =item -t TEMPLATE This is the Template Toolkit template file to use for output. Without this switch, the script uses an internal default template. The Template object is configured to handle absolute paths. Alternately you can use the AMAZON_ASSOCIATES_TEMPLATE environment variable. =back =head2 Environment variables Each command line switch has a corresponding environment variable whic +h is checked only if the switch isn't specified. =head1 BUGS None that I know of, yet. =head1 TO DO =over 4 =item prompt for password? =back =head1 AUTHOR brian d foy <bdfoy@cpan.org> =head1 COPYRIGHT Copyright (c) 2005, brian d foy, All Rights Reserved. You may redistribute this under the same terms as Perl itself. =cut use DateTime; use Getopt::Std; use Template; use WWW::Mechanize; my %opts; getopt( 'epst', \%opts ); our $email = $opts{'e'} || $ENV{AMAZON_ASSOCIATES_EMAIL} || die "Sp +ecify an email address!"; our $pass = $opts{'p'} || $ENV{AMAZON_ASSOCIATES_PASS} || die "Sp +ecify your password!"; our $store = $opts{'s'} || $ENV{AMAZON_ASSOCIATES_STORE}; our $file = $opts{'t'} || $ENV{AMAZON_ASSOCIATES_TEMPLATE}; my @items = get_items(); foreach my $item ( @items ) { my %hash = $item =~ m|(\w+)="(.*?)"|g; $Date{ $hash{EDate} }{count}++; $Date{ $hash{EDate} }{earned} += $hash{Earnings}; push @foo, \%hash; } my $tt = Template->new( { ABSOLUTE => 1 } ); my $input = -e $file ? $file : get_default_template(); $tt->process( $input, { items => \@items, dates => \%Date } ) || die $tt->error(), "\n"; # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # +# # # # sub get_items { my $report = get_report(); my @items = $report =~ m| <Item\s+(.*?)\s*/> |xg; } sub get_report { my $mech = WWW::Mechanize->new() or die; $mech->get( 'https://associates.amazon.com/' ); $res_2 = $mech->submit_form( form_name => 'sign-in', fields => { email => $email, password => $pass, } ); my $response = $mech->follow_link( url_regex => qr/earningsReport/ +i ); # ensure we're using the right store, since we can have more than +one # but only do that if we specified a store. Otherwise, assume the # default (or sole) store is the right one. if( $store ) { $mech->form_number(1); $res_2 = $mech->submit_form( fields => { hasChanged => 'store', storeID => $store, } ); } my $form = $mech->form_name( 'htmlReport' ); $mech->field( 'preSelectedPeriod', 'QuarterToDate' ); my $response = $mech->click_button( name => 'submit.download_XML' +); $response->as_string; } sub get_default_template { my $string = <<'TEMPLATE'; [% USE date( format="%m-%d-%y" ) -%] Date Items Earnings ---------------------------- [% FOREACH item IN dates.keys.sort.reverse -%] [% date.format(item) %] [% dates.${item}.count %] [% dates. +${item}.earned %] [% END -%] TEMPLATE \$string; }
In reply to Amazon Associates activity by brian_d_foy
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |