]> git.donarmstrong.com Git - infobot.git/blob - src/Modules/W3Search.pl
fixed/added global factoid SAR
[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
9 my $maxshow     = 3;
10
11 sub W3Search {
12     my ($where, $what, $type) = @_;
13     my $retval = "$where can't find \002$what\002";
14
15     return unless &main::loadPerlModule("WWW::Search");
16
17     my @matches = grep { lc($_) eq lc($where) ? $_ : undef } @main::W3Search_engines;
18     if (@matches) {
19         $where = shift @matches;
20     } else {
21         &main::msg($main::who, "i don't know how to check '$where'");
22     }
23
24     my $Search  = new WWW::Search($where);
25     my $Query   = WWW::Search::escape_query($what);
26     $Search->native_query($Query,
27 #       {
28 #               search_debug => 2,
29 #               search_parse_debug => 2,
30 #       }
31     );
32     $Search->http_proxy($main::param{'httpProxy'}) if (&main::IsParam("httpProxy"));
33     my $max = $Search->maximum_to_retrieve(10); # DOES NOT WORK.
34
35     my (%results, $count, $r);
36     while ($r = $Search->next_result()) {
37         my $url = $r->url();
38
39         ### TODO: fix regex.
40         ### TODO: use array to preserve order.
41         if ($url =~ /^http:\/\/([\w\.]*)/) {
42             my $hostname = $1;
43             next if (exists $results{$hostname});
44             $results{$hostname} = $url;
45         } else {
46             &main::DEBUG("W3S: url isn't good? ($url).");
47         }
48
49         last if ++$count >= $maxshow;
50     }
51
52     if (scalar keys %results) {
53         $retval = "$where says \002$what\002 is at ".
54                 join(' or ', map { $results{$_} } sort keys %results);
55     }
56
57     &main::performStrictReply($retval);
58 }
59
60 1;