Skip to content

Instantly share code, notes, and snippets.

@JGaudette
Created October 7, 2010 12:48
Show Gist options
  • Save JGaudette/615047 to your computer and use it in GitHub Desktop.
Save JGaudette/615047 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use Net::Amazon;
use Net::Amazon::Request::Wishlist;
use Net::Amazon::Request::ASIN;
use DBI;
use Product;
use Price;
## Create a record in the "prices" table
sub createRecord{
my $rec = shift;
#print "creating record for: " . $rec->getAsin() . "\n";
my $db='amazon';
my $host="localhost";
my $uid="USERNAME";
my $passwd="PASSWORD";
my $conn="dbi:mysql:$db:$host";
my $asin = $rec->getAsin();
my $price = $rec->getPrice();
my $dbh = DBI->connect($conn,$userid,$passwd);
my $query = "insert into prices(asin,price) values('$asin','$price')";
my $sth = $dbh->prepare($query);
$sth->execute();
}
## Get a Product details from amazon
sub getAmazonPrice{
my $ua = Net::Amazon->new(token=>'YOUR_AMAZON_TOKEN');
my $asin = shift;
my $req = Net::Amazon::Request::ASIN->new(asin=>$asin);
my $resp = $ua->request($req);
foreach my $item ($resp->properties){
my $tPrice = $item->OurPrice();
$tPrice =~ s/\$//g;
return $tPrice;
}
}
my @products;
my @amazonPrices;
my $db='amazon';
my $host="localhost";
my $uid="USERNAME";
my $passwd="PASSWORD";
my $conn="dbi:mysql:$db:$host";
my $dbh = DBI->connect($conn,$userid,$passwd);
my $query = "select asin,price from prices where id in (select max(id) from prices group by asin)";
my $query2 = "select distinct asin from products";
my @prodAsins;
my $sth2 = $dbh->prepare($query2);
my $prodAsin;
$sth2->execute();
$sth2->bind_columns(\$prodAsin);
while($sth2->fetch()){
push(@prodAsins, $prodAsin);
}
my $sth = $dbh->prepare($query);
$sth->execute();
$sth->bind_columns(\$asin, \$price);
while($sth->fetch()){
push(@products, Price->new($asin, $price));
}
foreach(@prodAsins){
push(@amazonPrices, Price->new($_, getAmazonPrice($_)));
}
foreach my $amazonAsin (@amazonPrices){
my $flag = 2;
foreach my $prod (@products){
if($amazonAsin->getAsin() eq $prod->getAsin()){
if($amazonAsin->getPrice() != $prod->getPrice()){
$flag = 1;
}else{
$flag = 0;
}
}
}
if($flag > 0){
print "Creating record for " . $amazonAsin->getAsin() . " | price is now " . $amazonAsin->getPrice() . "\n";
createRecord($amazonAsin);
}
}
exit 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment