Created
July 22, 2010 08:29
-
-
Save lopnor/485725 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package BrowseNodeLookup; | |
| use LWP::UserAgent; | |
| use Net::Amazon; | |
| use URI; | |
| use XML::LibXML; | |
| sub new { | |
| my ($class, $conf) = @_; | |
| my $self = bless {}, $class; | |
| $self->{amazon} = Net::Amazon->new( | |
| token => $conf->{token}, | |
| secret_key => $conf->{secret_key}, | |
| locale => 'jp', | |
| ); | |
| $self->{token} = $conf->{token}; | |
| $self->{xpc} = XML::LibXML::XPathContext->new; | |
| $self->{xpc}->registerNs( | |
| 'awsecs', | |
| 'http://webservices.amazon.com/AWSECommerceService/2005-10-05' | |
| ); | |
| return $self; | |
| } | |
| sub lookup { | |
| my ($self, $nodeid, $response_group, $verbose) = @_; | |
| my $response_to_xpath = { | |
| BrowseNodeInfo => './awsecs:Children/awsecs:BrowseNode', | |
| TopSellers => './awsecs:TopSellers/awsecs:TopSeller', | |
| NewReleases => './awsecs:NewReleases/awsecs:NewRelease', | |
| }; | |
| exists $response_to_xpath->{$response_group} or die 'unknown response group'; | |
| my $uri = URI->new('http://webservices.amazon.co.jp/onca/xml'); | |
| $uri->query_form( | |
| Service => 'AWSECommerceService', | |
| AWSAccessKeyId => $self->{token}, | |
| Operation => 'BrowseNodeLookup', | |
| BrowseNodeId => $nodeid, | |
| ResponseGroup => $response_group, | |
| ); | |
| $uri = $self->{amazon}->_sign_request($uri); | |
| my $ua = LWP::UserAgent->new; | |
| my $res = $ua->get($uri); | |
| $res->is_success or die; | |
| my $parser = XML::LibXML->new; | |
| my $xml = $parser->load_xml(string => $res->content); | |
| warn $xml->toString(1) if $verbose; | |
| my $node = $self->{xpc}->findnodes('//awsecs:BrowseNodes/awsecs:BrowseNode', $xml)->shift; | |
| my $result = []; | |
| my $result_hash = {}; | |
| my $asin = []; | |
| for my $child ($self->{xpc}->findnodes($response_to_xpath->{$response_group}, $node)) { | |
| my $info = $self->nodeinfo($child); | |
| push @$asin, $info->{ASIN} if $info->{ASIN}; | |
| push @$result, $info; | |
| $result_hash->{$info->{ASIN}} = $info if $info->{ASIN}; | |
| } | |
| my $asin_res = $self->{amazon}->search(asin => $asin); | |
| for my $item ($asin_res->properties) { | |
| $result_hash->{$item->ASIN}->{detail} = $item; | |
| } | |
| return $result; | |
| } | |
| sub nodeinfo { | |
| my ($self, $node) = @_; | |
| my $res; | |
| for my $child ($node->childNodes) { | |
| $res->{$child->localname} = $child->textContent; | |
| } | |
| return $res; | |
| } | |
| 1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment