summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--Changes4
-rw-r--r--MANIFEST8
-rw-r--r--MANIFEST.SKIP11
-rw-r--r--META.yml12
-rw-r--r--Makefile.PL9
-rw-r--r--README62
-rw-r--r--lib/WebService/Google/Suggest.pm115
-rw-r--r--t/00_compile.t4
-rw-r--r--t/01_suggest.t13
9 files changed, 238 insertions, 0 deletions
diff --git a/Changes b/Changes
new file mode 100644
index 0000000..491af65
--- /dev/null
+++ b/Changes
@@ -0,0 +1,4 @@
+Revision history for Perl extension WebService::Google::Suggest
+
+0.01  Sat Dec 11 22:18:56 2004
+	- original version
diff --git a/MANIFEST b/MANIFEST
new file mode 100644
index 0000000..a024fc7
--- /dev/null
+++ b/MANIFEST
@@ -0,0 +1,8 @@
+Changes
+lib/WebService/Google/Suggest.pm
+Makefile.PL
+MANIFEST			This list of files
+README
+t/00_compile.t
+t/01_suggest.t
+META.yml                                 Module meta-data (added by MakeMaker)
diff --git a/MANIFEST.SKIP b/MANIFEST.SKIP
new file mode 100644
index 0000000..71b1be8
--- /dev/null
+++ b/MANIFEST.SKIP
@@ -0,0 +1,11 @@
+\bRCS\b
+\bCVS\b
+^MANIFEST\.
+^Makefile$
+~$
+\.old$
+^blib/
+^pm_to_blib
+^MakeMaker-\d
+\.gz$
+\.cvsignore
diff --git a/META.yml b/META.yml
new file mode 100644
index 0000000..1c1183c
--- /dev/null
+++ b/META.yml
@@ -0,0 +1,12 @@
+# http://module-build.sourceforge.net/META-spec.html
+#XXXXXXX This is a prototype!!!  It will change in the future!!! XXXXX#
+name:         WebService-Google-Suggest
+version:      0.01
+version_from: lib/WebService/Google/Suggest.pm
+installdirs:  site
+requires:
+    LWP::UserAgent:                0
+    Test::More:                    0.32
+
+distribution_type: module
+generated_by: ExtUtils::MakeMaker version 6.17
diff --git a/Makefile.PL b/Makefile.PL
new file mode 100644
index 0000000..66c30e9
--- /dev/null
+++ b/Makefile.PL
@@ -0,0 +1,9 @@
+use ExtUtils::MakeMaker;
+WriteMakefile(
+    'NAME'      => 'WebService::Google::Suggest',
+    'VERSION_FROM' => 'lib/WebService/Google/Suggest.pm', # finds $VERSION
+    'PREREQ_PM' => {
+	Test::More => 0.32,
+	LWP::UserAgent => 0,
+    },
+);
diff --git a/README b/README
new file mode 100644
index 0000000..4bef20d
--- /dev/null
+++ b/README
@@ -0,0 +1,62 @@
+NAME
+    WebService::Google::Suggest - Google Suggest as an API
+
+SYNOPSIS
+      use WebService::Google::Suggest;
+
+      my $suggest = WebService::Google::Suggest->new();
+      my @suggestions = $suggest->complete("goog");
+      for my $suggestion (@suggestions) {
+          print "$suggestion->{query}: $suggestion->{results} results\n";
+      }
+
+DESCRIPTION
+    WebService::Google::Suggest allows you to use Google Suggest as a Web
+    Service API to retrieve completions to your search query or partial
+    query. This module is based on Adam Stiles' hack
+    (http://www.adamstiles.com/adam/2004/12/hacking_google_.html).
+
+METHODS
+    new
+          $suggest = WebService::Google::Suggest->new();
+
+        Creates new WebService::Google::Suggest object.
+
+    complete
+          @suggestions = $suggest->complete($query);
+
+        Sends your $query to Google web server and fetches suggestions for
+        the query. Suggestions are in a list of hashrefs, for example with
+        query "Google":
+
+          @suggestions = (
+            { query => "google", results => 122000000 },
+            { query => "google toolbar", results => 2620000 },
+            ...
+          );
+
+        Note that "results" value does NOT contain commas and "results"
+        text.
+
+    ua
+          $ua = $suggest->ua;
+
+        Returns underlying LWP::UserAgent object. It allows you to change
+        User-Agent (Windows IE by default), timeout seconds and various
+        properties.
+
+AUTHOR
+    Tatsuhiko Miyagawa <miyagawa@bulknews.net>
+
+    This library is free software; you can redistribute it and/or modify it
+    under the same terms as Perl itself.
+
+    This module gives you NO WARRANTY.
+
+SEE ALSO
+    http://www.adamstiles.com/adam/2004/12/hacking_google_.html
+
+    http://www.google.com/webhp?complete=1&hl=en
+
+    http://labs.google.com/suggest/faq.html
+
diff --git a/lib/WebService/Google/Suggest.pm b/lib/WebService/Google/Suggest.pm
new file mode 100644
index 0000000..33446ba
--- /dev/null
+++ b/lib/WebService/Google/Suggest.pm
@@ -0,0 +1,115 @@
+package WebService::Google::Suggest;
+
+use strict;
+use vars qw($VERSION);
+$VERSION = '0.01';
+
+use Carp;
+use LWP::UserAgent;
+use URI::Escape;
+
+use vars qw($CompleteURL);
+$CompleteURL = "http://www.google.com/complete/search?hl=en&js=true&qu=";
+
+sub new {
+    my $class = shift;
+    my $ua = LWP::UserAgent->new();
+    $ua->agent("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
+    bless { ua => $ua }, $class;
+}
+
+sub ua { $_[0]->{ua} }
+
+sub complete {
+    my($self, $query) = @_;
+    my $url = $CompleteURL . uri_escape($query);
+
+    my $response = $self->ua->get($url);
+    $response->is_success or croak "Google doesn't respond well: ", $response->code;
+
+    my $content = $response->content();
+    $content =~ /^sendRPCDone\(frameElement, ".*?", new Array\((.*?)\), new Array\((.*?)\), new Array\(""\)\)\;$/
+	or croak "Google returns unrecognized format: $content";
+    my($queries, $results) = ($1, $2);
+    my @queries = map { s/^"(.*?)"$/$1/; $_ } split /, /, $queries;
+    my @results = map { s/^"([\d,]+) results?"$/$1/; tr/,//d; $_+0 }
+	split /, /, $results;
+    return map { +{ query   => $queries[$_],
+		    results => $results[$_] } } 0..$#queries;
+}
+
+1;
+__END__
+
+=head1 NAME
+
+WebService::Google::Suggest - Google Suggest as an API
+
+=head1 SYNOPSIS
+
+  use WebService::Google::Suggest;
+
+  my $suggest = WebService::Google::Suggest->new();
+  my @suggestions = $suggest->complete("goog");
+  for my $suggestion (@suggestions) {
+      print "$suggestion->{query}: $suggestion->{results} results\n";
+  }
+
+=head1 DESCRIPTION
+
+WebService::Google::Suggest allows you to use Google Suggest as a Web Service API to retrieve completions to your search query or partial query. This module is based on Adam Stiles' hack (http://www.adamstiles.com/adam/2004/12/hacking_google_.html).
+
+=head1 METHODS
+
+=over 4
+
+=item new
+
+  $suggest = WebService::Google::Suggest->new();
+
+Creates new WebService::Google::Suggest object.
+
+=item complete
+
+  @suggestions = $suggest->complete($query);
+
+Sends your C<$query> to Google web server and fetches suggestions for
+the query. Suggestions are in a list of hashrefs, for example with
+query "Google":
+
+  @suggestions = (
+    { query => "google", results => 122000000 },
+    { query => "google toolbar", results => 2620000 },
+    ...
+  );
+
+Note that C<results> value does NOT contain commas and "results" text.
+
+=item ua
+
+  $ua = $suggest->ua;
+
+Returns underlying LWP::UserAgent object. It allows you to change
+User-Agent (Windows IE by default), timeout seconds and various
+properties.
+
+=back
+
+=head1 AUTHOR
+
+Tatsuhiko Miyagawa E<lt>miyagawa@bulknews.netE<gt>
+
+This library is free software; you can redistribute it and/or modify
+it under the same terms as Perl itself.
+
+This module gives you B<NO WARRANTY>.
+
+=head1 SEE ALSO
+
+http://www.adamstiles.com/adam/2004/12/hacking_google_.html
+
+http://www.google.com/webhp?complete=1&hl=en
+
+http://labs.google.com/suggest/faq.html
+
+=cut
diff --git a/t/00_compile.t b/t/00_compile.t
new file mode 100644
index 0000000..bcaa28f
--- /dev/null
+++ b/t/00_compile.t
@@ -0,0 +1,4 @@
+use strict;
+use Test::More tests => 1;
+
+BEGIN { use_ok 'WebService::Google::Suggest' }
diff --git a/t/01_suggest.t b/t/01_suggest.t
new file mode 100644
index 0000000..7db220e
--- /dev/null
+++ b/t/01_suggest.t
@@ -0,0 +1,13 @@
+use strict;
+use Test::More tests => 4;
+
+BEGIN { use_ok 'WebService::Google::Suggest' }
+
+my $suggest = WebService::Google::Suggest->new();
+
+isa_ok($suggest->ua, "LWP::UserAgent", "ua() retuens LWP");
+
+my @data = $suggest->complete("google");
+is($data[0]->{query}, "google", "google completes to google");
+is_deeply( [ $suggest->complete("udfg67a") ], [ ], "empty list" );
+