#! /usr/bin/perl use Net::Amazon; use Jcode; use CGI; ############################################## # Predifined $cgi_url ='/cgi-bin/barcode.cgi'; my $id = 'nakamuraminor-22'; my $token = 'AWS-3.0 のサブスクリプション ID で埋めてね'; ############################################## # $counter = 1; select(STDOUT); $|=1; select(STDOUT); ############################################## # $query = new CGI; local $input_list = $query->param("list"); local $submit = $query->param("submit"); my $tmp_id = $query->param("aid"); if ($tmp_id) { ($id) = ($tmp_id =~ /(\S+)/); } if ($submit =~ /Large/) { $submit = 'large'; } elsif ($submit =~ /Medium/) { $submit = 'medium'; } else { $submit = 'small'; } $ua = Net::Amazon->new(affiliate_id => $id, token => $token, locale => 'jp'); @ASINs = (); $html_text = ""; $csv_text = ""; foreach $line (split("\n",$input_list)) { my ($key) = ($line =~ /(\S+)/); my $barcode = tran_barcode($key); if ($barcode == 0) { if ($key =~ /(\S{8})/) { push @ASINs, $key; } } else { push @ASINs, $barcode; } } ############################################## # Body print $query->header("text/html; charset=utf-8"); print < バーコードやISBNコードによる書籍の検索

バーコードやISBNコードによる書籍の検索







Results EOF foreach $asin (@ASINs) { aws_query($asin); sleep(1); } print <
HTML code
CSV Code

To homge page EOF exit(0); sub tran_barcode { my ($code) = @_; ($isbn) = ($code =~ /^978(\d{9})\d$/); if ($isbn) { my ($n1,$n2,$n3,$n4,$n5,$n6,$n7,$n8,$n9) = ($isbn =~/(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)/); my $sum = 1*$n1+2*$n2+3*$n3+4*$n4+5*$n5+6*$n6+7*$n7+8*$n8+9*$n9; my $rem = $sum % 11; if ($rem == 10) { return sprintf('%9dX', $isbn); } else { return sprintf('%9d%1d', $isbn, $rem); } } return 0; } sub aws_query { my ($asin) = @_; # print STDERR "$counter\t$asin\n"; my $response = $ua->search(asin => $asin); # my $response = $ua->search(asin => \@list); if($response->is_success()) { foreach $_ ($response->properties){ unless ($_->Catalog() eq 'Book') { next; } my $url = $_->url(); my $productname = $_->ProductName(); my $image_large = $_->ImageUrlLarge(); my $image_medium = $_->ImageUrlMedium(); my $image_small = $_->ImageUrlSmall(); my $release_date = $_->ReleaseDate(); # my $catalog = $_->Catalog(); my $manufacturer = $_->Manufacturer(); my $listprice = $_->ListPrice(); my $ourprice = $_->OurPrice(); my $usedprice = $_->UsedPrice(); my $availability = $_->Availability(); my $salesrank = $_->SalesRank(); my $title = $_->title(); my $publisher = $_->publisher(); my $year = $_->year(); my $isbn = $_->isbn(); my @authors = $_->authors(); $authors = join(",", @authors); my $text = ""; if ($submit eq 'large') { $text = "
"; } elsif ($submit eq 'medium') { $text = ""; } else { $text = ""; } # print "" . $text . "\n"; my $line = "" . $text . "\n"; print $line; $html_text .= $line; $csv_text .= "\"$asin\",\"\",\"$title\",\"$authors\",\"$publisher\",\"$year\",\"$release_date\",\"$listprice\",\"$url\",\"$image_large\",\"$image_medium\",\"$image_small\"\n"; $counter++; } } else { my $line = "\n"; print $line; $html_text .= $line; $csv_text .= "\"$asin\"\n"; $counter++; } }
$counter$asin$title$authors$publisher$year$release_date$listprice$salesrank
$counter$asin$title$authors$publisher$year$release_date$listprice
$counter$asin