]> git.donarmstrong.com Git - infobot.git/blob - src/Modules/W3Scraper.pl
avoid reassigning to temp upon decode_utf8
[infobot.git] / src / Modules / W3Scraper.pl
1 # WWW::Scraper backend, replaces W3Search.pl functionality.
2 # Uses WWW::Scraper and associated modules instead of WWW::Search;
3
4 # Written by Don Armstrong <don@donarmstrong.com>
5
6 # $Id$
7
8 package W3Scraper;
9
10 =head1 NAME
11
12 W3Scraper - blootbot plugin to do searches using WWW::Scraper
13
14 =head1 SYNOPSIS
15
16
17
18 =head1 DESCRIPTION
19
20
21
22 =cut
23
24 use warnings;
25 use strict;
26 use vars qw ($VERSION);
27
28 my $maxshow = 5;
29
30 sub W3Scraper {
31      my ($where,$what,$type) = @_;
32
33      # rip out the available engines by brute force.
34      my @matches = grep {/$where.pm/i and !/FieldTranslation/i and !/Re(sponse|quest)/i and !/TidyXML/i}
35           split /\n/, qx(ls /usr/share/perl5/WWW/Scraper);
36
37      if (@matches) {
38           $where = shift @matches;
39           $where =~ s/\.pm//;
40      }
41      else {
42           &::msg($::who, "i don't know how to check '$where'");
43           return;
44      }
45
46      return unless &::loadPerlModule("WWW::Scraper");
47
48      my $scraper;
49      eval {
50           $scraper = new WWW::Scraper($where,$what);
51      };
52      if (not defined $scraper) {
53           &::msg($::who,"$where is an invalid search.");
54           return;
55      }
56
57      my $count = 0;
58      my $results = q();
59      while (my $result = $scraper->next_response()) {
60           next if not defined $result->url or not defined ${$result->url};
61           next if ((length ${$result->url}) > 80); #ignore long urls
62           if ($count > 0) {
63                $results .= ' or ';
64           }
65           $results .= ${$result->url};
66           last if ++$count > $maxshow;
67      }
68      if ($count > 0) {
69           $results = qq($where says "$what" is at $results [).
70                $scraper->approximate_result_count().
71                     q( results]);
72      }
73      else {
74           $results = qq($where was unable to find "$what");
75      }
76      &::performStrictReply($results);
77 }
78
79
80 1;