App-BarnesNoble-WishListMinder

 view release on metacpan or  search on metacpan

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

      $price = int($price * 100 + 0.5);
    }

    { no warnings 'uninitialized';  $book->{discount} =~ s/\%// }

    # Update or add the prices entry
    if ($current_price_row and
        _numEq($current_price_row->{price}, $book->{price}) and
        _numEq($current_price_row->{list_price}, $book->{list_price}) and
        _numEq($current_price_row->{discount}, $book->{discount})) {
      $dbh->do(<<'', undef, $time_fetched, $ean, $current_price_row->{first_recorded});
        UPDATE prices SET last_checked = ?
        WHERE ean = ? AND first_recorded = ?

    } else {
      if ($current_price_row) {
        $dbh->do(<<'', undef, $ean, $current_price_row->{first_recorded});
          UPDATE prices SET current = 0 WHERE ean = ? AND first_recorded = ?

      }
      $updates->{$ean} = {
        old => $current_price_row,
        new => $book,
      };
###   Inserting: $ean
      $dbh->do(<<'', undef, @$book{qw(ean price list_price discount)}, ($time_fetched)x2);
        INSERT INTO prices (ean, price, list_price, discount, first_recorded, last_checked)
        VALUES (?,?,?,?,?,?)

    }
  } # end for each $book in @$books

  $dbh->commit;
} # end write_db

sub reduced_price_eans
{
  my $updates = shift->updates;

  sort {
    $updates->{$a}{new}{price} <=> $updates->{$b}{new}{price} or
    $updates->{$a}{new}{title} cmp $updates->{$b}{new}{title}
  } grep {
    my ($old, $new) = @{$updates->{$_}}{qw(old new)};
    $old and defined($new->{price})
        and (!defined($old->{price}) or $new->{price} < $old->{price});
  } keys %$updates;
} # end reduced_price_eans

sub get_existing_books
{
  my ($self, $wishlist_id) = @_;

  my %existing_priority;

  my $s = $self->dbh->prepare(<<'');
    SELECT ean, priority FROM wishlist_books
    WHERE wishlist_id = ? AND date_removed IS NULL

  $s->execute($wishlist_id);
  $s->bind_columns( \( my ($ean, $priority) ) );
  while ($s->fetch) {
    $existing_priority{$ean} = $priority;
  }

  \%existing_priority;
} # end get_existing_books

sub get_wishlist_id
{
  my ($self, $wishlist_url) = @_;

  my $dbh = $self->dbh;

  my ($wishlist_id) = $dbh->selectrow_array(<<'', undef, $wishlist_url);
    SELECT wishlist_id FROM wishlists WHERE url = ?

  unless (defined $wishlist_id) {
    $dbh->do(<<'', undef, $wishlist_url);
      INSERT INTO wishlists (url) VALUES (?)

    $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;



( run in 0.542 second using v1.01-cache-2.11-cpan-2398b32b56e )