]> git.donarmstrong.com Git - infobot.git/blob - src/Modules/Google.pl
Google.pl: Set some Local Variables: for emacs users, according to already set vim...
[infobot.git] / src / Modules / Google.pl
1 # W3Search drastically altered back to GoogleSearch as Search::Google 
2 # was deprecated and requires a key that google no longer provides. 
3 # This new module uses REST::Google::Search 
4 # Modified by db <db@cave.za.net> 12-01-2008. 
5 #
6 # Usage: 'chanset _default +Google' in query window with your bot
7 #        to enable it in all channels
8 #        /msg botnick google <query> OR <addressCharacter>google <query> to use
9
10 package Google;
11
12 use strict;
13
14 my $maxshow = 5;
15
16 sub GoogleSearch {
17     my ( $what, $type ) = @_;
18     # $where set to official google colors ;)
19     my $where  = "\00312G\0034o\0038o\00312g\0033l\0034e\003";
20     my $retval = "$where can't find \002$what\002";
21     my $Search;
22     my $referer = "irc://$::server/$::chan/$::who";
23
24     return unless &::loadPerlModule("REST::Google::Search");
25
26     &::DEBUG( "Google::GoogleSearch->referer = $referer" );
27     &::status( "Google::GoogleSearch> Searching Google for: $what");
28     REST::Google::Search->http_referer( $referer );
29     $Search = REST::Google::Search->new( q => $what );
30
31     if ( !defined $Search ) {
32         &::msg( $::who, "$where is invalid search." );
33         &::WARN( "Google::GoogleSearch> $::who generated an invalid search: $where");
34         return;
35     }
36
37     if ( $Search->responseStatus != 200 ) {
38         &::msg( $::who, "http error returned." );
39         &::WARN( "Google::GoogleSearch> http error returned: $Search->responseStatus");
40         return;
41     }
42
43     # No results found
44     if ( not $Search->responseData->results ) {
45         &::DEBUG( "Google::GoogleSearch> $retval" );
46         &::msg( $::who, $retval);
47         return;
48     }
49
50     my $data    = $Search->responseData;
51     my $cursor  = $data->cursor;
52     my @results = $data->results;
53     my $count;
54
55     $retval = "$where says \"\002$what\002\" is at ";
56     foreach my $r (@results) {
57         my $url = $r->url;
58
59         # Returns a string with each %XX sequence replaced with the actual byte
60         # (octet). From URI::Escape uri_unescape()
61         $url =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
62
63         $retval .= " \002or\002 " if ( $count > 0 );
64         $retval .= $url;
65         last if ++$count >= $maxshow; # Only seems to return max of 4?
66     }
67
68     &::performStrictReply($retval);
69 }
70
71 1;
72  
73 # vim:ts=4:sw=4:expandtab:tw=80 
74 # Local Variables:
75 # mode: cperl
76 # tab-width: 4
77 # fill-column: 80
78 # indent-tabs-mode: nil
79 # End: