CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Perl NET::Amazon

 



mistermickster
New User

Jan 20, 2015, 4:34 AM

Post #1 of 3 (2918 views)
Perl NET::Amazon Can't Post

I've just started experimenting with Net::Amazon and can easily retrieve some data with a search by ASIN as in the example code below:-

Code
#!/usr/bin/perl -w 

use strict;
use warnings;
use Data::Dumper;
$Data::Dumper::Indent = 1;
$Data::Dumper::Sortkeys = 1;
use Net::Amazon;

open DUMPFILE, '>', '/tmp/amazondump';

my $ua = Net::Amazon->new(
associate_tag => 'MY_ASSC_TAG',
token => 'MY_AMAZON_TOKEN',
secret_key => 'MY_AMAZON_SECRETKEY',
locale => 'uk');

# Get a request object
my $response = $ua->search(asin => 'B00PXMMHMY');

print DUMPFILE Dumper($response);

if($response->is_success()) {
print $response->as_string(), "\n";
} else {
print "Error: ", $response->message(), "\n";
}

close DUMPFILE;

I've also output the contents of the response into a text file so that I can see what is returned.


Code
$VAR1 = bless( { 
'items' => [],
'messages' => [],
'status' => 1,
'total_results' => 1,
'xmlref' => {
'Items' => [
{
'ASIN' => 'B00PXMMHMY',
'BrowseNodes' => {
'BrowseNode' => [
{
'Ancestors' => {
'BrowseNode' => {
'BrowseNodeId' => '3146281',
'Name' => 'Home & Garden'
}
},

...
...
...

}
}
}
}
]
}
, 'Net::Amazon::Response::ASIN' );


Is there a way programmatically that I can query the response to show all keys => values at all levels so that I can select/print/do something with them - eg. Description, Price etc.?

Thanks in advance.


Laurent_R
Veteran / Moderator

Jan 20, 2015, 10:02 AM

Post #2 of 3 (2912 views)
Re: [mistermickster] Perl NET::Amazon [In reply to] Can't Post

The keys function gives you a list of all keys of a hash.

Update: I had originally not paid attention to it but this is an object. Net::Amazon provides methods to access the content of the object (search). Use these methods if you can for your purpose. If the available methods don't provide what you need, write new methods within the module or in a subclass.


(This post was edited by Laurent_R on Jan 20, 2015, 10:27 AM)


Laurent_R
Veteran / Moderator

Jan 20, 2015, 2:26 PM

Post #3 of 3 (2907 views)
Re: [mistermickster] Perl NET::Amazon [In reply to] Can't Post

If you want to know how to traverse such a nested data structure, I can show you how to access the inner data by giving you a very rough, very sketchy and very simplistic (20-minute coding) prototype implementation of a dumper subroutine somewhat similar to the Dumper function of the Data::Dumper module.

I have not looked at all how Data::Dumper is implemented (it may be somewhat similar or very different, I do not know), but if it did not exist and if I were to try to implement one, the program below is the possible bare-bones squeletton of what I would try to do. My only aim here is to show how to walk through the data, the output is really minimal.

The dumper subroutine below is essentially a recursive function which prints the leaves and recurses through the nodes of the data tree (in other words, if the data found is a reference to another piece of data, the function follows the reference to go to the underlying data by calling itself with the new reference, and if it is not a reference, it simply prints the data. For your purpose, you would have to replace the print statements with something more useful.


Code
use strict; 
use warnings;


my $response = {
'items' => [],
'messages' => [],
'status' => 1,
'total_results' => 1,
'xmlref' => {
'Items' => [
{
'ASIN' => 'B00PXMMHMY',
'BrowseNodes' => {
'BrowseNode' => [
{
'Ancestors' => {
'BrowseNode' => {
'BrowseNodeId' => '3146281',
'Name' => 'Home & Garden'
}
},
},
{
'Ancestors2' => {
'BrowseNode2' => {
'BrowseNodeId' => '3146282',
'Name' => 'Garden & home'
}
},
}
]
}
}
]
}
};

dumper (0, ref $response, $response);

sub dumper {
my ($level , $ref_type_in, $reference) = @_;
if ($ref_type_in eq 'ARRAY' ) {
for my $value (@$reference) {
my $ref_type_out = ref $value;
if ($ref_type_out) {
dumper ($level + 1, $ref_type_out, $value)
} else {
print "\t" x $level; print $value, "\n";
}
}
} elsif ($ref_type_in eq 'HASH' ) {
for my $key (keys %$reference) {
print "\t" x $level, $key, " => ";
my $value = $reference->{$key};
my $ref_type_out = ref $value;
if ($ref_type_out) {
print "\n";
dumper ($level + 1, $ref_type_out, $value)
} else {
print $value, "\n";
}
}
} else {
print "Unknown reference type\n";
}
}


This gives the following output:


Code
$ perl amazon.pl 
xmlref =>
Items =>
ASIN => B00PXMMHMY
BrowseNodes =>
BrowseNode =>
Ancestors =>
BrowseNode =>
BrowseNodeId => 3146281
Name => Home & Garden
Ancestors2 =>
BrowseNode2 =>
BrowseNodeId => 3146282
Name => Garden & home
messages =>
total_results => 1
status => 1
items =>


This is just to show you how to access the data in a nested data structure, I have no intention whatsoever to compete with Data::Dumper.

And, as I said earlier, in your specific case, I would really recommend to use the methods of the Net::Amazon class rather than fiddling around in the object's data structure. I just thought that this example illustrates how you could add methods to this class if you need.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives