| 1 |
|
| 2 |
package WWW::Search::Simple; |
| 3 |
|
| 4 |
use strict; |
| 5 |
use warnings; |
| 6 |
|
| 7 |
=head1 NAME |
| 8 |
|
| 9 |
WWW::Search::Simple - class for searching any web site |
| 10 |
|
| 11 |
=head1 SYNOPSIS |
| 12 |
|
| 13 |
require WWW::Search; |
| 14 |
$search = new WWW::Search('Simple'); |
| 15 |
|
| 16 |
=head1 DESCRIPTION |
| 17 |
|
| 18 |
This class is a specialization of WWW::Search for simple web based |
| 19 |
search indices. It extracts all links from a given page. |
| 20 |
|
| 21 |
This class exports no public interface; all interaction should be done |
| 22 |
through WWW::Search objects. |
| 23 |
|
| 24 |
Note that this module will probably get a lot of false hits. |
| 25 |
|
| 26 |
=head1 AUTHOR |
| 27 |
|
| 28 |
C<WWW::Search::Simple> is written by Paul Lindner, <[email protected]> |
| 29 |
|
| 30 |
=head1 COPYRIGHT |
| 31 |
|
| 32 |
Copyright (c) 1997,98 by the United Nations Administrative Committee |
| 33 |
on Coordination (ACC) |
| 34 |
|
| 35 |
All rights reserved. |
| 36 |
|
| 37 |
=cut |
| 38 |
|
| 39 |
|
| 40 |
use base 'WWW::Search'; |
| 41 |
|
| 42 |
use Carp (); |
| 43 |
use HTML::TreeBuilder; |
| 44 |
use WWW::SearchResult; |
| 45 |
|
| 46 |
my $debug = 0; |
| 47 |
|
| 48 |
sub _native_setup_search |
| 49 |
{ |
| 50 |
my ($self, $native_query, $native_opt) = @_; |
| 51 |
my ($native_url); |
| 52 |
my ($default_native_url) = "http://www.itu.int/cgi-bin/SFgate?application=itu&database=local//usr/local/wais/WWW/www-pages&listenv=table&httppath=/usr/local/www-data/&httpprefix=/&tie=and&maxhits=%n&text=%s"; |
| 53 |
if (defined($native_opt)) |
| 54 |
{ |
| 55 |
#print "Got " . join(' ', keys(%$native_opt)) . "\n"; |
| 56 |
# Process options.. |
| 57 |
# Substitute query terms for %s... |
| 58 |
|
| 59 |
if ($self->{'search_url'} && $native_opt->{'search_args'}) |
| 60 |
{ |
| 61 |
$native_url = $native_opt->{'search_url'} . "?" . $native_opt->{'search_args'}; |
| 62 |
} # if |
| 63 |
} # if |
| 64 |
$native_url = $default_native_url if (!$native_url); |
| 65 |
$native_url =~ s/%s/$native_query/g; # Substitute search terms... |
| 66 |
$self->user_agent(); |
| 67 |
$self->{_next_to_retrieve} = 0; |
| 68 |
$self->{_base_url} = $self->{_next_url} = $native_url; |
| 69 |
} # _native_setup_search |
| 70 |
|
| 71 |
sub _native_retrieve_some |
| 72 |
{ |
| 73 |
my ($self) = @_; |
| 74 |
my ($hit) = (); |
| 75 |
my ($hits_found) = 0; |
| 76 |
|
| 77 |
# fast exit if already done |
| 78 |
return undef if (!defined($self->{_next_url})); |
| 79 |
|
| 80 |
# get some |
| 81 |
print "GET " . $self->{_next_url} . "\n" if ($debug); |
| 82 |
my($response) = $self->http_request($self->{search_method}, |
| 83 |
$self->{_next_url}); |
| 84 |
|
| 85 |
$self->{response} = $response; |
| 86 |
if (!$response->is_success) |
| 87 |
{ |
| 88 |
print "Some problem\n" if ($debug); |
| 89 |
return undef; |
| 90 |
} |
| 91 |
|
| 92 |
my $score = 800; |
| 93 |
my $results = $response->content(); |
| 94 |
|
| 95 |
my($h) = new HTML::TreeBuilder; |
| 96 |
$h->parse($results); |
| 97 |
for (@{ $h->extract_links(qw(a)) }) |
| 98 |
{ |
| 99 |
my($link, $linkelem) = @$_; |
| 100 |
|
| 101 |
my($linkobj) = new URI::URL $link, $self->{_next_url}; |
| 102 |
print "Fixing $link\n" if ($debug); |
| 103 |
|
| 104 |
my($hit) = new WWW::SearchResult; |
| 105 |
$hit->add_url($linkobj->abs->as_string()); |
| 106 |
$hit->title(join(' ',@{$linkelem->content})); |
| 107 |
$hit->score($score); |
| 108 |
$hit->normalized_score($score); |
| 109 |
if ($hit->title !~ /HASH\(0x/) |
| 110 |
{ |
| 111 |
$hits_found++; |
| 112 |
push(@{$self->{cache}}, $hit); |
| 113 |
} # if |
| 114 |
$score = int ($score * .95); |
| 115 |
} # for |
| 116 |
$self->approximate_result_count($hits_found); |
| 117 |
$self->{_next_url} = undef; |
| 118 |
return($hits_found); |
| 119 |
} # _native_retrieve_some |
| 120 |
|
| 121 |
1; |
| 122 |
|