1 ################################
3 ################################
5 # Original Author: Max Kanat-Alexander <mkanat@bugzilla.org>
6 # Author: Stephen Lau <steve@grommit.com>
9 # The original version of this module used Net::Google which used the Google
10 # SOAP API. I've updated it to use the REST::Google::Search module which
11 # uses Google's AJAX API
13 # This API requires that you send a valid HTTP_REFERER, which you can set
14 # with the REFERER constant below:
16 package BotModules::Google;
18 @ISA = qw(BotModules);
19 use REST::Google::Search;
21 use constant SEPARATOR => ' -- ';
22 use constant REFERER => 'http://www.mozilla.org/projects/mozbot/';
29 '' => q{Queries Google for specified search terms. },
30 'google' => q{Searches google for the specified terms.}
31 . q{Syntax: 'google <terms>'},
32 'fight' => q{Google fight two terms.}
33 . q{Syntax: 'fight <term1> vs. <term2>'}
37 # RegisterConfig - Called when initialised, should call registerVariables
40 $self->SUPER::RegisterConfig(@_);
41 $self->registerVariables(
42 # [ name, save?, settable? ]
43 ['maxResults', 1, 1, 8],
44 ['maxInChannel', 1, 1, 1],
45 ['safeSearch', 1, 1, 1],
46 ['maxLineLength', 1, 1, 256]
52 my ($event, $message) = @_;
53 # We take anything that occurs at the end of the line,
54 # because Google will ignore punctuation anyway.
55 if ($message =~ /^(\s*google\s+)(.+)$/osi) {
58 my @searchResults = $self->doSearch($terms);
60 if (!@searchResults) {
61 $self->say($event, "Nothing found.");
63 # If we are in a channel, and not a /msg
64 elsif ($event->{'channel'}) {
65 splice(@searchResults, $self->{'maxInChannel'});
69 unshift(@searchResults, scalar(@searchResults) . " results found: ");
72 foreach my $result (@searchResults) {
73 $self->say($event, $event->{'from'} . ': ' . $result);
75 } elsif ($message =~ /^(\s*fight\s+)(.+)\s+vs\.\s+(.+)\s*$/osi) {
78 my $results1 = $self->getNumResults($term1);
79 my $results2 = $self->getNumResults($term2);
81 if ($results1 > $results2) {
82 $self->say($event, "
\ 2$term1
\ 2 beats $term2,
\ 2$results1
\ 2 to $results2!");
83 } elsif ($results2 > $results1) {
84 $self->say($event, "
\ 2$term2
\ 2 beats $term1,
\ 2$results2
\ 2 to $results1!");
86 $self->say($event, "It's a dead tie at
\ 2$results1
\ 2 results!");
89 return $self->SUPER::Told(@_);
91 return 0; # we've dealt with it, no need to do anything else.
98 REST::Google::Search->http_referer(REFERER);
99 my $res = REST::Google::Search->new(
104 if ($res->responseStatus != 200) {
108 my $data = $res->responseData;
109 return $data->cursor->estimatedResultCount;
111 # Performs the actual Google search and returns the
112 # result as an array of lines to say.
117 my @searchLines = ();
118 REST::Google::Search->http_referer(REFERER);
119 my $res = REST::Google::Search->new(
124 if ($res->responseStatus != 200) {
128 my $data = $res->responseData;
129 my @results = $data->results;
131 foreach my $result (@results) {
132 my $title = $result->title;
133 # The Google API puts <b></b> tags into the title if the search
134 # terms appear in the title.
135 $title =~ s|</?b>||g;
136 $title = $self->unescapeXML($title);
137 my $url = $result->url;
138 my $line_size = (length($title) + length($result) + length(SEPARATOR));
139 if ($line_size > $self->{'maxLineLength'} ) {
140 # The 3 is for the '...'
141 my $new_title_size = ($line_size - $self->{'maxLineLength'}) - 3;
142 my $title = substr($title, 0, $new_title_size)
145 my $resultLine = $title . SEPARATOR . $url;
146 push(@searchLines, $resultLine);