Google Web API

遅ればせながらGoogle Web APIで遊んでみました。

http://www.google.com/apis/

手順通りに

1. Debeloper's Kitをダウンロード

2. Google Accountを作る

3.発行されたライセンス・キーを使ってプログラムを書く

を順番にやってみることに。今回はJavaAPIを使ってみました。

やってみたのは、以下のクラスを使って

GoogleSearch
GoogleSearchResult
GoogleSearchResultElement

ググッて、結果を得るということ。
以下の例では"Rugby"をググってみるコード

package com.hidekoji.GoogleTest;

import com.google.soap.search.GoogleSearch;
import com.google.soap.search.GoogleSearchResult;
import com.google.soap.search.GoogleSearchFault;
import com.google.soap.search.GoogleSearchResultElement;

public class GoogleTest 
{
  public GoogleTest()
  {
  }

   public GoogleSearchResult doSearch(String queryString)
   {
    GoogleSearch search = new GoogleSearch();
    // Set mandatory attributes
    search.setKey("SET_YOUR_OWN_KEY");
    search.setQueryString(queryString);
    // Set optional attributes
    search.setSafeSearch(true);
    // Invoke the actual search
    try{
    GoogleSearchResult result = search.doSearch();
    return result;
    } catch (GoogleSearchFault e)
     {
       return null; 
     }
   }

   public void printResult(GoogleSearchResult result)
   {
    if (result != null)
    {
     GoogleSearchResultElement[] elements = (GoogleSearchResultElement[]) result.getResultElements();
     for (int i=0;i<elements.length;i++)
     {
      String title = elements[i].getTitle();
      String directoryTitle = elements[i].getDirectoryTitle();
      String snippet  = elements[i].getSnippet();
      
      System.out.print("======= Line#"+i+" ========");
      System.out.print("\n");
      System.out.print(title);
      System.out.print("\n");
      System.out.print(directoryTitle);
      System.out.print("\n");
      System.out.print(snippet);
      System.out.print("\n");
     }
    }else
    {
      System.out.print("no data found");
    }
   }

  public static void main(String[] args)
  {
    GoogleTest googleTest = new GoogleTest();
    GoogleSearchResult result = googleTest.doSearch("Rugby");
    googleTest.printResult(result);
  }
}

これを実行してみると

======= Line#0 ========
Rugby news, results, fixtures and features from Planet-Rugby.com
Planet Rugby
With international rugby rankings and an archive going back to 1871.
======= Line#1 ========
Rugby Football Union - Home
RFU On-line
Contains information about all facets of English rugby and merchandising.
The Rugby Football Union is the governing body of English rugby.
======= Line#2 ========
rugby.com.au |
Rugby.com.au
Rugby union news, fixtures results and articles.
======= Line#3 ========
USA Rugby
USA Rugby - The United States of America Rugby Football Union
Lists players, coaches, referees, administrators and national teams; also includes
calendar and youth rugby information.
======= Line#4 ========
Scrum.com - Rugby News, Rugby Results, Rugby Action from Scrum ...
Scrum.com
World rugby news, results, reports on all major internationals and European club
matches, regular columns, features, and competitions.
======= Line#5 ========
Rugby Borough Council Online - Tel: 01788 533 533
Rugby Borough Council
Rugby.gov.uk is the online resource for Rugby Borough, Warwickshire - with council
services online.
======= Line#6 ========
Irish Rugby
Irish Rugby Football Union
Official IRFU rugby site. About irfu, news, contacts. Irish rugby, 5 nations,
RWC99, and international rugby. Ireland results service New Zealand, ...
======= Line#7 ========
Welsh Rugby Union
Welsh Rugby Union
Information about the WRU, fixture lists, league tables, results and merchandise.
======= Line#8 ========
SaRugby

Both SA Rugby and the Springbok management on Tuesday dismissed ... The rugby
law changes during the past five years all came into effect on the 1st of June ...
======= Line#9 ========
RugbyHeaven - rugbyheaven.smh.com.au

Flavell eyes return to NZ rugby - Wasps sting Leeds - Anger at plan for extra
comp - Young Marlin a big catch, says Wallaby ...

GoogleのサイトでRugbyで検索した結果と比べると

ちゃんと同じ結果が返ってますね。