App-BarnesNoble-WishListMinder

 view release on metacpan or  search on metacpan

lib/App/BarnesNoble/WishListMinder.pm  view on Meta::CPAN


    $wishlist_id = $dbh->last_insert_id((undef)x4)
        // die "Unable to insert wishlist $wishlist_url";
  }

  $wishlist_id;
} # end get_wishlist_id
#---------------------------------------------------------------------

sub describe_selected_updates
{
  my $self = shift;

  my $updates = $self->updates;

  map {
    my $book = $updates->{$_}{new};
    my $price = _format_price($book->{price});
    if (my $old = $updates->{$_}{old}) {
      $price .= sprintf ' (was %s)', _format_price($old->{price});
    }
    <<"END UPDATE";
Title:  $book->{title}  ($_)
Author: $book->{author}
Price:  $price
END UPDATE
  } @_;
} # end describe_selected_updates

#---------------------------------------------------------------------

sub email_price_drop_alert
{
  my ($self) = @_;

  my @eans = $self->reduced_price_eans or return;

  require Email::Sender::Simple;
  require Email::Simple;
  require Email::Simple::Creator;
  require Encode;

  my $updates = $self->updates;
  my $config  = $self->config->{_};

  my $address = $config->{report} || $config->{email};
  my @body = $self->describe_selected_updates(@eans);

  my $subject = (@eans > 2)
    ? sprintf('%d books', scalar @eans)
    : Encode::encode('MIME-Header',
        join(' & ', map { $updates->{$_}{new}{title} } @eans)
      );

  my $email = Email::Simple->create(
    header => [
      To      => $address,
      From    => qq'"Barnes & Noble Wishlist" <$address>',
      Subject => "Price Drop Alert: $subject",
      'MIME-Version' => '1.0',
      'Content-Type' => 'text/plain; charset=UTF-8',
      'Content-Transfer-Encoding' => '8bit',
    ],
    body => Encode::encode('utf8', join("\n", @body)),
  );

  Email::Sender::Simple->send($email);
} # end email_price_drop_alert
#---------------------------------------------------------------------

sub print_matching_books
{
  my ($self, $search, $all_history) = @_;

  my $books = $self->dbh->selectall_arrayref(<<'END SEARCH', undef, ("%$search%")x2);
SELECT ean, price, title, author FROM books NATURAL JOIN prices
WHERE prices.current AND (title LIKE ? OR author LIKE ?)
ORDER by title, author
END SEARCH

  if ($all_history or @$books == 1) {
    foreach my $row (@$books) {
      print "$row->[0] ";
      $self->print_price_history($row->[0]);
    }
  } else {
    foreach my $row (@$books) {
      $row->[1] = _format_price($row->[1]);
      printf "%s %6s %s by %s\n", @$row;
    }
    print "\n";
  }
} # end print_matching_books
#---------------------------------------------------------------------

sub print_updates_since
{
  my ($self, $since_date) = @_;

  my $s = $self->dbh->prepare(<<'END SEARCH');
SELECT ean, price, title, author FROM books NATURAL JOIN prices
WHERE prices.current AND first_recorded >= ?
ORDER by first_recorded, price, title, author
END SEARCH

  $s->execute($since_date);

  while (my $row = $s->fetch) {
    $row->[1] = _format_price($row->[1]);
    printf "%s %6s %s by %s\n", @$row;
  }

  print "\n";
} # end print_updates_since
#---------------------------------------------------------------------

sub print_price_history
{
  my ($self, $ean) = @_;

  my $dbh = $self->dbh;



( run in 2.941 seconds using v1.01-cache-2.11-cpan-364913b4093 )