#!/usr/bin/perl -w # # Grab all the values from your Amazon Wish list # and then print it out - surprisingly scary (at # least in my case, it came out over a grand) # # copyright (c) Simon Wistow # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # this software is under no warranty and will # probably destroy your wish list, kill your friends # burn your house and bring about the apocalypse # v1.21 fixed number formatting bug (thanks to 'Evil' Dave Cantrell) # v1.2 fixes bug in grabbing stuff from .co.uk # v1.1 adds ability to grab everything from multipage wishlists use strict; use LWP::UserAgent; use Getopt::Long; # initialise the total my $total = 0; # defined where the resource file is my $res = "$ENV{HOME}/.amazon_wish"; # set up some variables my $uk = 0; my $suffix = 'com'; my $match = '\$'; my $symbol = '$'; my $force = 0; my $save = 0; my $help = 0; my $id; GetOptions ('uk' => \$uk, 'id=s' => \$id, 'force' => \$force, 'save' => \$save, 'help' => \$help); if ($help) { my $name; ($name = $0) =~ s!^.*[\\/]!!; print << "EOH"; % $name [-uk] [-id ID] [-force] [-save] [-help] -uk : get values from amazon.co.uk (default is .com) -id : your amazon wish list id (it's the last bit of the URL after /wishlist/' -force : don't look up values from the resource file -save : save these values to the resource files -help : print this message the first time you run the script you must supply the values but after that it will save them in .amazon_wishlist in your home directory. You can overide the resource file by using -force option or the -save option (which will save the new values into the resource file). So ... % $name -uk -id yakyakyak £100\.00 % $name £100\.00 % $name -id barbarbar -force \$111\.00 % $name £100\.00 % $name -id barbarbar -save \$111\.00 % $name \$111\.00 EOH exit 0; } # check to see if we've already stored the settings or we're forcing if (-e $res && !$force && !$save) { open FILE, $res || die "Couldn't open the resource file '$res' for reading : $!\n"; # grab all the value while () { chomp; # my ($key, $value) =split /\s*=\s*/; if ($key =~ /\s*uk\s*$/) { $uk = $value; } elsif ($key =~ /\s*id\s*$/) { $id = $value; $id =~ s/^\s*([^\s]+)\s*$/$1/; } } close FILE; } # check to see we're valid die "You haven't defined an id\n" unless (defined $id); # if it's a uk version update the variables if ($uk) { $suffix = "co.uk"; $match = "£"; $symbol = "£"; } # and write out to our resource file unless (-e $res && !$save) { open FILE, ">$res" || die "Couldn't open the resource file '$res' for writing : $!\n"; print FILE "id = $id\nuk = $uk\n"; close FILE; } my $page = 1; while (1) { my $url = "http://www.amazon.$suffix/exec/obidos/wishlist/$id/?registry.page-number=$page"; # grab the page (how to check the response, shrug, whatever) my @lines = split /\n/, `lynx -source -dump $url`; my $oldpage = $page; # grab the values foreach (@lines) { if ( /$match(\d+\.\d+)/) { $total += $1 } if (m!\"(/exec/obidos/registry/$id/[^"]+)"!) { if (/registry.page-number=(\d+)/) { $page = $1; } } } last unless ($page > $oldpage); #last; } # dump print sprintf("%s%.2f\n", $symbol, $total); # and exit exit