What did you buy at Amazon?

I wanted to get a list of all the books I bought at Amazon. Using Perl and WWW::Mechanize this is straightforward:

# get amazon shopping history
# --md@hudora.de

use WWW::Mechanize;

$username = "youremail@example.com";
$passwort = "yourpassword";

my $mech = WWW::Mechanize->new( autocheck => 1 );
$mech->agent_alias("Mac Safari");

#print "getting session ... ";
$mech->get("http://www.amazon.de" );
$mech->follow_link( url_regex => qr/account-access-login/ );
#print $mech->uri(), "n";

#print "requesting login ... ";
$mech->form_number(2);
$mech->select("ss-order-filter", "all-items");
$mech->submit_form();
#print $mech->uri(), "n";

#print "listing bought books ... ";
$mech->field("password", $passwort);
$mech->field("email", $username);
$mech->field("action", "sign-in");
$mech->submit_form();
#print $mech->uri(), "n";

print "# Amazon Books you boughtn";
foreach my $link ($mech->find_all_links(tag => "a", url_regex => qr|ASIN| ))
{
    my @tmp = $link->url =~ m|^.*/ASIN/([^/]+)/.*$|;
    my $asin = $tmp[0];
    if($asin =~ m|^[0-9].*|) {
        print $asin, " ", $link->text, "n";
    };
};

# getting list of order number
foreach my $link ($mech->find_all_links(tag => "a", url_regex => qr|/display-order-status/| ))
{
    $hash{$link->text} = $link->url;
};

while ( my ($key, $value) = each(%hash)) 
{
    print "# order $key:n";
    $mech->get( $value ), "n";

    foreach my $link ($mech->find_all_links(tag => "a", url_regex => qr|/ASIN/| ))
    {
        my @tmp = $link->url =~ m|^.*/ASIN/([^/]+)/.*$|;
        my $asin = $tmp[0];
        print $asin, " ", $link->text, "n";
    };
}

Kommentar verfassen

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

WordPress.com-Logo

Du kommentierst mit Deinem WordPress.com-Konto. Abmelden / Ändern )

Twitter-Bild

Du kommentierst mit Deinem Twitter-Konto. Abmelden / Ändern )

Facebook-Foto

Du kommentierst mit Deinem Facebook-Konto. Abmelden / Ändern )

Google+ Foto

Du kommentierst mit Deinem Google+-Konto. Abmelden / Ändern )

Verbinde mit %s