$ cat ./amazon_http.pl #!/usr/local/bin/perl5.8.0 # amazon_http.pl # A typical Amazon Web API Perl script using the XML/HTTP interface # Usage: amazon_http.pl <keyword> #Your Amazon developer's token my $dev_key='insert developer token'; #Your Amazon affiliate code my $af_tag='insert associate tag'; #Take the keyword from the command-line my $keyword =shift @ARGV or die "Usage:perl amazon_http.pl <keyword>\n"; #Assemble the URL my $url = "http://xml.amazon.co.uk/onca/xml3?t=" . $af_tag . "&dev-t=" . $dev_key . "&type=lite&f=xml&mode=books&" . "KeywordSearch=" . $keyword; use strict; #Use the XML::Parser and LWP::Simple Perl modules use XML::Simple; use LWP::Simple; my $content = get($url); die "Could not retrieve $url" unless $content; my $xmlsimple = XML::Simple->new( ); my $response = $xmlsimple->XMLin($content); my $details = $response->{Details}; if (ref($details) eq 'ARRAY') { foreach my $result (@$details){ #Print out the main bits of each result print join "\n", $result->{ProductName}||"no title", "ASIN: " . $result->{Asin} . ", " . $result->{OurPrice} . "\n\n"; } } # sigue la modificación al del libro elsif (ref($details) eq 'HASH') { print join "\n", $details->{ProductName}||"no title", "ASIN: " . $details->{Asin} . ", " . $details->{OurPrice} . "\n\n"; } else { print "No encontré nada\n"; }Véase un ejemplo de ejecución con una sóla respuesta:
$ ./amazon_http.pl 'Perl Medic' Perl Medic: Transforming Legacy Code ASIN: 0201795264, $24.49Un ejemplo con ninguna respuesta:
$ ./amazon_http.pl 'Perl toto' No encontré naday un ejemplo de ejecución con múltiples respuestas:
$ ./amazon_http.pl 'Perl CGI' Programming Perl (3rd Edition) ASIN: 0596000278, $34.96 JavaScript: The Definitive Guide ASIN: 0596000480, $31.46 Learning Perl, Third Edition ASIN: 0596001320, $23.77 JavaScript Bible, Fifth Edition ASIN: 0764557432, $33.99 Perl Cookbook, Second Edition ASIN: 0596003137, $33.97 Mastering Regular Expressions, Second Edition ASIN: 0596002890, $27.17 Dive Into Python ASIN: 1590593561, $27.99 JavaScript & DHTML Cookbook ASIN: 0596004672, $27.17 JavaScript Bible, 4th Edition ASIN: 0764533428, $33.99 Perl CD Bookshelf, Version 4.0 ASIN: 0596006225, $67.97
Casiano Rodríguez León