#!/usr/local/bin/perl
# -*- coding: utf-8 -*-
use strict;
use warnings;
use Encode;
use CGI;
use LWP::Simple;
use HTML::Template;
use URI::Escape;
use utf8;
use XML::Simple;
binmode STDOUT, ":utf8";
$| = 1;
my $appid = "gUxspU.xg66pvU6W5OJMz0vH10FYB.FT4sWcQomZrtmPD6sG.14VlAuMdCGoBuIeMyOpRtlJBlc-";
# appid は自分で取得したものをご利用下さい。
# http://e.developer.yahoo.co.jp/webservices/register_application
my $q = new CGI;
my $template = join("", );
my $t = HTML::Template->new(scalarref => \$template,
global_vars => 1,
loop_context_vars => 1,
die_on_bad_params => 0);
my $query = $q->param('q') || "";
$query = Encode::decode_utf8($query) unless utf8::is_utf8($query);
if ($query eq "") {
print $q->header(-charset => "UTF-8");
print $t->output();
exit;
}
print $q->header(-charset => "UTF-8");
my $rvsa_ref = get_results_auc({q => $query});
$t->param(listauc => $rvsa_ref) if $rvsa_ref;
my $rvss_ref = get_results_sis({q => $query});
$t->param(listsis => $rvss_ref) if $rvss_ref;
$t->param(query => $query);
my $out = $t->output();
print $out;
exit;
sub get_results_sis {
my ($args_ref) = @_;
my $ecq = URI::Escape::uri_escape_utf8($args_ref->{q});
my $url = "http://shopping.yahooapis.jp/ShoppingWebService/V1/itemSearch?"
."appid=$appid&query=$ecq"
."&affiliate_type=yid&affiliate_id=Z3ocehYWOqF4aWOqzOG6";
my $response = get($url);
my $xmlsimple = XML::Simple->new();
my $xml = $xmlsimple->XMLin($response, ForceArray => [ 'Hit' ]);
return [] unless $xml->{Result}->{Hit};
my @hits;
foreach my $e (@{$xml->{Result}->{Hit}}) {
push @hits, {
name => Encode::decode_utf8($e->{Name}),
description => (ref($e->{Description}) eq "HASH") ?
"" : Encode::decode_utf8($e->{Description}),
headline => (ref($e->{Headline}) eq "HASH") ?
"" : Encode::decode_utf8($e->{Headline}),
url => $e->{Url},
price => $e->{Price}->{content},
image => $e->{Image}->{Medium},
storename => Encode::decode_utf8($e->{Store}->{Name}),
storeurl => $e->{Store}->{Url},
};
}
return \@hits;
}
sub get_results_auc {
my ($args_ref) = @_;
my $ecq = URI::Escape::uri_escape_utf8($args_ref->{q});
my $url = "http://auctions.yahooapis.jp/AuctionWebService/V2/search?"
."appid=$appid&query=$ecq";
my $response = get($url);
my $xmlsimple = XML::Simple->new();
my $xml = $xmlsimple->XMLin($response, ForceArray => [ 'Item' ]);
return [] unless $xml->{Result}->{Item};
my @hits;
foreach my $e (@{$xml->{Result}->{Item}}) {
push @hits, {
name => Encode::decode_utf8($e->{Title}),
url => $e->{AuctionItemUrl},
price => int($e->{CurrentPrice}),
image => $e->{Image}->{content},
};
}
return \@hits;
}
__DATA__
Yahoo!ショッピング検索結果
Yahoo!オークション検索結果