ねもぷらす

ふぁいんでぃんぐねもの日記。プログラミングとか育児とか

googleapi を試食

簡単に検索CGIが作れます!

#!/usr/local/bin/perl
use strict;
use warnings;

use SOAP::Lite;
use Jcode;
use Template;

use CGI;
# $CGI::POST_MAX = 1024 * 100;

my $cgi = CGI->new();
print $cgi->header( -type    => 'text/html',
                    -charset => 'utf8' );

my $query  = $cgi->param('q') || "google";
my $offset = $cgi->param('page') || 0;

my $key=<span style="color:#FF0000;">Googleから届くライセンスキー</span>;
my $api_path = "/ <span style="color:#0000FF;">path to</span> /GoogleSearch.wsdl";
my $googleSearch = SOAP::Lite -> service("file:$api_path");

my $result = $googleSearch -> doGoogleSearch(
    $key,
    jcode($query)->utf8,
    $offset,
    10,
    "false",
    "",
    "false",
    "lang_ja",
    "UTF-8",
    "UTF-8"
);
$result->{search_word} = $query;

eval{
  # search result
  my $tt = Template->new();
     $tt->process(\*DATA, $result) or die $tt->error();
};if($@){
  print "Error: $@\n";
}

__DATA__
<html>
  <head>
  <meta http-equiv="content-type" content="text/html;charset=us-ascii">
  <title>search result</title>
  <style><!--
    body {font-family: arial,sans-serif}
    div.nav {margin-top: 1ex}
    div.nav A {font-size: 10pt; font-family: arial,sans-serif}
    span.nav {font-size: 10pt; font-family: arial,sans-serif; font-weight: bold}
    div.nav A,span.big {font-size: 12pt; color: #0000cc}
    div.nav A {font-size: 10pt; color: black}
    A.l:link {color: #6f6f6f}
    A.u:link {color: green}
  //--></style>
</head>
<body text=#000000 bgcolor=#ffffff>
<table border=0 cellpadding=2 cellspacing=0 width=100%>
  <tr>
    <td rowspan=3 width=1% nowrap><b>
      <font face=times color=#0039b6 size=10>S</font>
      <font face=times color=#c41200 size=10>e</font>
      <font face=times color=#f3c518 size=10>a</font>
      <font face=times color=#0039b6 size=10>r</font>
      <font face=times color=#30a72f size=10>c</font>
      <font face=times color=#c41200 size=10>h</font>
      &nbsp;&nbsp;</b>
    </td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td bgcolor=#3366cc><font face=arial,sans-serif color=#ffffff>
       <b> [% search_word %] Results 10 of about [% estimatedTotalResultsCount %] pages</b>
    </td>
  </tr>
  <tr>
    <td>
      <form name="foo" action="/cgi-bin/google.cgi">
      <script type="text/javascript">
      <!--
        function isFilled() {
          document.foo.g.disabled = (document.foo.q.value == "");
        }
      //-->
      </script>
      <input type="text" name="q" value="[% search_word %]" size=40 onKeyUp="isFilled()" />
      <input type="submit" name="g" value="search" disabled="disabled" />
      </form>
    </td>
  </tr>
</table>
<blockquote>
[% FOREACH i = resultElements %]
   <a href="[% i.URL %]"><p>[% i.title %]</P></a> [% END %]

</blockquote>
<table width=100% cellpadding=0 cellspacing=0><tr><td bgcolor=#3366cc><img alt="" width=1 height=4></td></tr></table>
</body>
</html>


まぁ修正すべき箇所は多いですが、Template-Tool-Kit の使い方の復習にもなりました♪
ちなみにこのコードで表示される画面は Google 様の 404 ページにインスパイアされております(汗