#!/usr/bin/perl # Copyright 2007 Siva Dirisala # You are free to use this as long as you give credit to this script. # Use it at your own risk, I am not responsible for any outcome of using this script. # use LWP::UserAgent; use XML::XPath; my $ua = LWP::UserAgent->new(keep_alive => 1, timeout => 30); $ua->agent('Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1'); $awsakey = $ENV{AWSKEY}; if(!$awsakey) { print "Please set the AWSKEY environment variable with your Amazon Web Services Access Key and then run this script\n"; exit(-1); } if($#ARGV < 0) { print qq(Usage: amzn_ecs_browse_nodes.pl \n); exit(-1); } %bnodes; sub getNodeInfo { my ($bnode,$pbnode,$recurse) = @_; my $url = "http://ecs.amazonaws.com/onca/xml?Service=AWSECommerceService&Operation=BrowseNodeLookup&AWSAccessKeyId=$awsakey&ResponseGroup=BrowseNodeInfo&BrowseNodeId=$bnode"; my $resp = $ua->get($url); my $xpath = new XML::XPath(xml => $resp->content); my @tbnodes = $xpath->findnodes('//BrowseNodes/BrowseNode'); map { my $tbnode = $_; my $browseNodeId = $xpath->findvalue('BrowseNodeId/text()',$tbnode); my $name = $xpath->findvalue('Name/text()',$tbnode); printf("%s\t%s\t%s\n",$pbnode,$browseNodeId,$name); if(!$bnodes{$browseNodeId}) { $bnodes{$browseNodeId} = [$pbnode,$browseNodeId,$name]; my @cbnodes = $xpath->findnodes('Children/BrowseNode',$tbnode); if(@cbnodes) { my $bnodeIds; my $ind = 0; map { my $cbnode = $_; my $cbrowseNodeId = $xpath->findvalue('BrowseNodeId/text()',$cbnode); $ind++; if($recurse) { $bnodeIds .= ',' if($bnodeIds); $bnodeIds .= $cbrowseNodeId; if($ind == 10) { getNodeInfo($bnodeIds,$browseNodeId,$recurse); sleep(1); $ind = 0; $bnodeIds = ''; } } } @cbnodes; if($recurse and $ind) { getNodeInfo($bnodeIds,$browseNodeId,$recurse); } } } } @tbnodes; } getNodeInfo($ARGV[0],undef,1);