]> git.donarmstrong.com Git - infobot.git/blob - src/Modules/W3Search.pl
eee5ff0f343855ef3db469c2b96c8b86f71bdde0
[infobot.git] / src / Modules / W3Search.pl
1 # WWWSearch backend, with queries updating the is-db (optionally)
2 # Uses WWW::Search::Google and WWW::Search
3 # originally Google.pl, drastically altered.
4
5 package W3Search;
6
7 use strict;
8 use vars qw(@W3Search_engines $W3Search_regex);
9 @W3Search_engines = qw(AltaVista Dejanews Excite Gopher HotBot Infoseek
10                 Lycos Magellan PLweb SFgate Simple Verity Google);
11 $W3Search_regex = join '|', @W3Search_engines;
12
13 my $maxshow     = 5;
14
15 sub W3Search {
16     my ($where, $what, $type) = @_;
17     my $retval = "$where can't find \002$what\002";
18     my $Search;
19
20     my @matches = grep { lc($_) eq lc($where) ? $_ : undef } @W3Search_engines;
21     if (@matches) {
22         $where = shift @matches;
23     } else {
24         &::msg($::who, "i don't know how to check '$where'");
25         return;
26     }
27
28     return unless &::loadPerlModule("WWW::Search");
29
30     eval {
31         if ($where eq 'Google') {
32             # key is your Google API key.
33             # Get it from http://api.google.com/createkey
34             $Search = new WWW::Search('Google',key => '')
35         }
36         else {
37             $Search     = new WWW::Search($where);
38         }
39         if (!defined $Search) {
40             &::msg($::who, "$where is invalid search.");
41             return;
42         }
43         
44         my $Query       = WWW::Search::escape_query($what);
45         $Search->native_query($Query,{   num => 10,
46                                          search_debug => 2,
47                                          search_parse_debug => 2,
48                                      }
49         );
50         $Search->http_proxy($::param{'httpProxy'}) if (&::IsParam("httpProxy"));
51         #my $max = $Search->maximum_to_retrieve(10);    # DOES NOT WORK.
52
53         my (@results, $count, $r);
54         $count=0;
55         $retval = "$where says \002$what\002 is at ";
56         while ($r = $Search->next_result()) {
57             my $url = $r->url();
58             $retval .= ' or ' if ($count > 0);
59             $retval .= $url;
60             last if ++$count >= $maxshow;
61         }
62         
63         $retval = "$where was unable to find any results for \002$what\002" unless $count > 0;
64         
65         &::performStrictReply($retval);
66     }
67 }    
68 1;
69