#!/usr/bin/perl -T
use strict;
use warnings;
use LWP::Simple;
use XML::Simple;
use JSON;
my $key = do {no warnings 'uninitialized'; $ENV{'QUERY_STRING'} || ""};
my $url = "http://search.yahooapis.jp/ImageSearchService/V1/"
."imageSearch?appid=YahooDemo&query=$key&results=10";
my $r = get_images($url);
my @urls = map {{Url => $_->{Url}, TUrl => $_->{Thumbnail}{Url}}} (@$r);
print "Content-Type: text/javascript; charset=UTF-8\n\n";
print objToJson(\@urls), "\n";
sub get_images {
my ($url) = @_;
my $yahoo_response = get($url);
return [] unless $yahoo_response;
my $xmlsimple = XML::Simple->new();
my $yahoo_xml = $xmlsimple->XMLin($yahoo_response);
if (ref($yahoo_xml->{Result}) eq "ARRAY") { # found: many
return $yahoo_xml->{Result};
} elsif (ref($yahoo_xml->{Result}) eq "HASH") { # found: 1
return [$yahoo_xml->{Result}];
}
return []; # not found
}
<html lang="ja">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="prototype-1.4.0.js" type="text/javascript"></script>
<script><!--
function get_json(key) {
var url = 'hack_json_image.cgi?' + key;
new Ajax.Request (url, {method: 'GET', onComplete: showResponse});
}
var images;
function showResponse(req, json) {
eval("images = " + req.responseText);
show_image();
}
var cur = 0;
function show_image() {
$('show').innerHTML = "<h1>slide " + (cur + 1) + "</h1>";
if (cur > 0)
$('show').innerHTML += '<img src="' + images[cur -1].TUrl + '">';
$('show').innerHTML
+= '<a href="' + images[cur].Url + '"><img src="'
+ images[cur].Url + '"></a>';
if (++cur >= images.length) cur = 0;
$('show').innerHTML += '<img src="' + images[cur].TUrl + '">';
}
// --></script>
</head>
<body>
<input type="text" id="key" size="20">
<input type="button" value="start" onclick="get_json($('key').value);">
<input type="button" value="next" onclick="show_image();">
<hr>
<div id="show">Slide Show!</div>
</body>
</html>




