root/WebService-Google-Suggest/trunk/lib/WebService/Google/Suggest.pm

Revision 1503 (checked in by miyagawa, 16 years ago)

Initial revision

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 package WebService::Google::Suggest;
2
3 use strict;
4 use vars qw($VERSION);
5 $VERSION = '0.01';
6
7 use Carp;
8 use LWP::UserAgent;
9 use URI::Escape;
10
11 use vars qw($CompleteURL);
12 $CompleteURL = "http://www.google.com/complete/search?hl=en&js=true&qu=";
13
14 sub new {
15     my $class = shift;
16     my $ua = LWP::UserAgent->new();
17     $ua->agent("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
18     bless { ua => $ua }, $class;
19 }
20
21 sub ua { $_[0]->{ua} }
22
23 sub complete {
24     my($self, $query) = @_;
25     my $url = $CompleteURL . uri_escape($query);
26
27     my $response = $self->ua->get($url);
28     $response->is_success or croak "Google doesn't respond well: ", $response->code;
29
30     my $content = $response->content();
31     $content =~ /^sendRPCDone\(frameElement, ".*?", new Array\((.*?)\), new Array\((.*?)\), new Array\(""\)\)\;$/
32         or croak "Google returns unrecognized format: $content";
33     my($queries, $results) = ($1, $2);
34     my @queries = map { s/^"(.*?)"$/$1/; $_ } split /, /, $queries;
35     my @results = map { s/^"([\d,]+) results?"$/$1/; tr/,//d; $_+0 }
36         split /, /, $results;
37     return map { +{ query   => $queries[$_],
38                     results => $results[$_] } } 0..$#queries;
39 }
40
41 1;
42 __END__
43
44 =head1 NAME
45
46 WebService::Google::Suggest - Google Suggest as an API
47
48 =head1 SYNOPSIS
49
50   use WebService::Google::Suggest;
51
52   my $suggest = WebService::Google::Suggest->new();
53   my @suggestions = $suggest->complete("goog");
54   for my $suggestion (@suggestions) {
55       print "$suggestion->{query}: $suggestion->{results} results\n";
56   }
57
58 =head1 DESCRIPTION
59
60 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).
61
62 =head1 METHODS
63
64 =over 4
65
66 =item new
67
68   $suggest = WebService::Google::Suggest->new();
69
70 Creates new WebService::Google::Suggest object.
71
72 =item complete
73
74   @suggestions = $suggest->complete($query);
75
76 Sends your C<$query> to Google web server and fetches suggestions for
77 the query. Suggestions are in a list of hashrefs, for example with
78 query "Google":
79
80   @suggestions = (
81     { query => "google", results => 122000000 },
82     { query => "google toolbar", results => 2620000 },
83     ...
84   );
85
86 Note that C<results> value does NOT contain commas and "results" text.
87
88 =item ua
89
90   $ua = $suggest->ua;
91
92 Returns underlying LWP::UserAgent object. It allows you to change
93 User-Agent (Windows IE by default), timeout seconds and various
94 properties.
95
96 =back
97
98 =head1 AUTHOR
99
100 Tatsuhiko Miyagawa E<lt>miyagawa@bulknews.netE<gt>
101
102 This library is free software; you can redistribute it and/or modify
103 it under the same terms as Perl itself.
104
105 This module gives you B<NO WARRANTY>.
106
107 =head1 SEE ALSO
108
109 http://www.adamstiles.com/adam/2004/12/hacking_google_.html
110
111 http://www.google.com/webhp?complete=1&hl=en
112
113 http://labs.google.com/suggest/faq.html
114
115 =cut
Note: See TracBrowser for help on using the browser.