App-BarnesNoble-WishListMinder
view release on metacpan or search on metacpan
lib/App/BarnesNoble/WishListMinder.pm view on Meta::CPAN
my $book = $dbh->selectrow_hashref(
'SELECT title, author FROM books WHERE ean = ?', undef, $ean
);
print "$book->{title} by $book->{author}\n";
my $history = $dbh->prepare(<<'END HISTORY');
SELECT first_recorded, last_checked, price, list_price, discount
FROM prices WHERE ean = ? ORDER BY first_recorded
END HISTORY
$history->execute($ean);
while (my $row = $history->fetchrow_hashref) {
$_ =~ s/ .+// for @$row{qw(first_recorded last_checked)};
printf("%s - %s %6s%s%s\n", @$row{qw(first_recorded last_checked)},
_format_price($row->{price}),
$row->{list_price}
? " (list " . _format_price($row->{list_price}) . ")"
: '',
$row->{discount} ? " ($row->{discount}% off)" : '');
}
print "\n";
} # end print_price_history
#---------------------------------------------------------------------
sub print_updates
{
my $self = shift;
my $updates = $self->updates;
my @eans = sort {
$updates->{$a}{new}{title} cmp $updates->{$b}{new}{title} or
$updates->{$a}{new}{author} cmp $updates->{$b}{new}{author}
} keys %$updates;
print join("\n", $self->describe_selected_updates(@eans));
} # end print_updates
#---------------------------------------------------------------------
sub have_user_cookie
{
my $have_cookie;
my $min_expires = time() + 30;
shift->mech->cookie_jar->scan(sub {
$have_cookie = 1 if $_[1] eq 'userid'
and $_[4] eq '.barnesandnoble.com'
and $_[8] > $min_expires
});
$have_cookie;
} # end have_user_cookie
#---------------------------------------------------------------------
sub update_wishlists
{
my $self = shift;
my $config = $self->config;
my $m = $self->mech;
# Ensure we can open the database before we start making web requests
$self->dbh;
$self->login unless $self->have_user_cookie;
for my $wishlist (sort keys %$config) {
next if $wishlist eq '_'; # the root INI section
my $response = $m->get( $config->{$wishlist}{wishlist} );
my $books = $self->scrape_response($response);
unless (@$books) {
warn "$config->{$wishlist}{wishlist} has no entries\n";
# Save the response for debugging:
$self->dir->child("empty-$wishlist.html")->spew_utf8($response->content);
}
# path("/tmp/wishlist.html")->spew_utf8($response->content);
$self->write_db($config->{$wishlist}{wishlist}, $response->last_modified // $response->date, $books);
}
} # end update_wishlists
#---------------------------------------------------------------------
sub usage {
my $name = $0;
$name =~ s!^.*[/\\]!!;
shift->close_dbh;
print "$name $VERSION\n";
exit if $_[0] and $_[0] eq 'version';
print <<"END USAGE";
\nUsage: $name [options] [EAN_or_TITLE_or_AUTHOR] ...
-a, --all-history Show price history even when multiple items match
-e, --email Send Price Drop Alert email (implies --update)
-q, --quiet Don't print list of updates
-s, --since=DATE Print books whose price changed on or after DATE
-u, --update Download current prices from wishlist
--configure Create and/or edit the config file
--help Display this help message
--version Display version information
END USAGE
exit;
} # end usage
#---------------------------------------------------------------------
sub run
{
my ($self, @args) = @_;
# Process command line options
my ($all_history, $fetch_wishlist, $quiet, $send_email, $since_date);
{
require Getopt::Long; Getopt::Long->VERSION(2.24); # object-oriented
( run in 1.204 second using v1.01-cache-2.11-cpan-5c0b1e786e0 )