| 1 |
#!perl -w |
| 2 |
# |
| 3 |
# WebSearch |
| 4 |
# Copyright (C) 1996-1997 by USC/ISI |
| 5 |
# $Id: WebSearch,v 1.1 2008/07/14 03:28:13 Martin Exp $ |
| 6 |
# |
| 7 |
# Complete copyright notice follows below. |
| 8 |
|
| 9 |
sub usage |
| 10 |
{ |
| 11 |
my $msg = shift; |
| 12 |
defined($msg) && ($msg ne '') && print STDERR "$0: $msg\n"; |
| 13 |
print STDERR <<"END"; |
| 14 |
usage: WebSearch [--engine <name>] [--gui] [--max <integer>] [--options <key=value>]... [--count] [--terse] [--all] [--raw] [--list] [--verbose] [--VERSION] [--help] [--host <hostname>] [--port <portnum>] [--username bbunny --password c4rr0t5] [--lwpdebug] [--debug] query |
| 15 |
|
| 16 |
Make a query to a web search engine, printing to STDOUT the URLs which match (one per line). |
| 17 |
|
| 18 |
See `perldoc WebSearch` for details. |
| 19 |
|
| 20 |
END |
| 21 |
exit 1; |
| 22 |
} # usage |
| 23 |
|
| 24 |
|
| 25 |
=head1 NAME |
| 26 |
|
| 27 |
WebSearch - a web-searching application demonstrating WWW::Search |
| 28 |
|
| 29 |
|
| 30 |
=head1 SYNOPSIS |
| 31 |
|
| 32 |
B<WebSearch [-m MaxCount] [-e SearchEngine] [-o option] [-o option...] [-ardvV] query> |
| 33 |
|
| 34 |
|
| 35 |
=head1 DESCRIPTION |
| 36 |
|
| 37 |
This program is provides a command-line interface to web search engines, |
| 38 |
listing all URLs found for a given query. This program also provides |
| 39 |
a simple demonstration of the WWW::Search Perl library for web searches. |
| 40 |
|
| 41 |
The program supports a number of search engines; |
| 42 |
use WebSearch --list to see which backends are installed. |
| 43 |
|
| 44 |
A more sophisticated client is L<AutoSearch> |
| 45 |
which maintains a change list of found objects. |
| 46 |
|
| 47 |
For examples and hints about searches, |
| 48 |
see L<AutoSearch>. |
| 49 |
|
| 50 |
|
| 51 |
=head1 OPTIONS |
| 52 |
|
| 53 |
WebSearch uses Getopt::Long, so you can use double-minus with long |
| 54 |
option names, or single-minus with one-letter abbreviations. |
| 55 |
|
| 56 |
=over 8 |
| 57 |
|
| 58 |
=item --engine e_name, -e e_name |
| 59 |
|
| 60 |
The string e_name is the name of (the module for) the desired search |
| 61 |
engine. Capitalization matters. See `perldoc WWW::Search` to find |
| 62 |
out what the default is (probably Null). |
| 63 |
|
| 64 |
Use --list to get a list of installed backends. |
| 65 |
|
| 66 |
=item --gui, -g |
| 67 |
|
| 68 |
Perform the search to mimic the default browser-based search. |
| 69 |
Not implemented for all backends, see the documentation for each backend. |
| 70 |
|
| 71 |
=item --list |
| 72 |
|
| 73 |
Prints to STDERR a \n-separated list of installed backends. |
| 74 |
|
| 75 |
=item --max max_count, -m max_count |
| 76 |
|
| 77 |
Specify the maximum number of hits to retrieve. |
| 78 |
|
| 79 |
=item --option o_string, -o o_string |
| 80 |
|
| 81 |
Specify a search-engine option in the form 'key=value' (or just |
| 82 |
'key'). Can be repeated for as many options are needed. Keys can be |
| 83 |
repeated. |
| 84 |
|
| 85 |
=item --count, -c |
| 86 |
|
| 87 |
As the first line of output, print the approximate hit count. |
| 88 |
As the last line of output, print the actual hit count. |
| 89 |
|
| 90 |
=item --terse, -t |
| 91 |
|
| 92 |
Do not print any URLs. |
| 93 |
Only useful if you also specify --count. |
| 94 |
If you specify --terse but not --count, |
| 95 |
there will be no output no matter how many hits are found! |
| 96 |
|
| 97 |
=item --all, -a |
| 98 |
|
| 99 |
For each hit result, print all the URLs that the search engine |
| 100 |
indicated were equivalent to it. (Some URLs may refer to the same |
| 101 |
object.) Can be combined with --verbose; can not be combined with |
| 102 |
--raw. |
| 103 |
|
| 104 |
=item --raw, -r |
| 105 |
|
| 106 |
For each hit result, print the raw HTML. |
| 107 |
Not implemented for all backends. |
| 108 |
|
| 109 |
=item --verbose, -v |
| 110 |
|
| 111 |
Verbose mode. Enumerate the returned URLs and show the description, |
| 112 |
score, date, etc. for each. |
| 113 |
|
| 114 |
=item --VERSION, -V |
| 115 |
|
| 116 |
Print version information and exit immediately. |
| 117 |
|
| 118 |
=item --debug <i>, -d <i> |
| 119 |
|
| 120 |
Display back-end debugging information (with debug level <i>) |
| 121 |
|
| 122 |
=item --host <hostname.sub.domain> |
| 123 |
|
| 124 |
Set the _host option for the WWW::Search object (backend-dependent). |
| 125 |
|
| 126 |
=item --port <i> |
| 127 |
|
| 128 |
Set the _port option for the WWW::Search object (backend-dependent). |
| 129 |
|
| 130 |
=item --username <bbunny> |
| 131 |
|
| 132 |
Set the username with which to login to the backend. |
| 133 |
|
| 134 |
=item --password <c4rr0t5> |
| 135 |
|
| 136 |
Set the password with which to login to the backend. |
| 137 |
|
| 138 |
=item --lwpdebug, -l |
| 139 |
|
| 140 |
Display low-level libwww-perl debugging information |
| 141 |
|
| 142 |
=back |
| 143 |
|
| 144 |
|
| 145 |
=head1 ENVIRONMENT VARIABLES |
| 146 |
|
| 147 |
The environment variable F<http_proxy> (or F<HTTP_PROXY>) |
| 148 |
specifies a proxy, if any. |
| 149 |
|
| 150 |
|
| 151 |
=head1 SEE ALSO |
| 152 |
|
| 153 |
For the library, see L<WWW::Search>. |
| 154 |
|
| 155 |
For a more sophisticated client, see L<AutoSearch>. |
| 156 |
|
| 157 |
|
| 158 |
=head1 AUTHOR |
| 159 |
|
| 160 |
C<WebSearch> was written by John Heidemann, <[email protected]>. |
| 161 |
C<WebSearch> is maintained by Martin Thurn, <[email protected]>. |
| 162 |
|
| 163 |
|
| 164 |
=head1 COPYRIGHT |
| 165 |
|
| 166 |
Copyright (c) 1996-1997 University of Southern California. |
| 167 |
All rights reserved. |
| 168 |
|
| 169 |
Redistribution and use in source and binary forms are permitted |
| 170 |
provided that the above copyright notice and this paragraph are |
| 171 |
duplicated in all such forms and that any documentation, advertising |
| 172 |
materials, and other materials related to such distribution and use |
| 173 |
acknowledge that the software was developed by the University of |
| 174 |
Southern California, Information Sciences Institute. The name of the |
| 175 |
University may not be used to endorse or promote products derived from |
| 176 |
this software without specific prior written permission. |
| 177 |
|
| 178 |
THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED |
| 179 |
WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF |
| 180 |
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
=cut |
| 185 |
|
| 186 |
use strict; |
| 187 |
|
| 188 |
&usage('no arguments given') if ($#ARGV == -1); |
| 189 |
# &usage if ($#ARGV >= 0 && $ARGV[0] eq '-?'); |
| 190 |
|
| 191 |
use Getopt::Long; |
| 192 |
use WWW::Search; |
| 193 |
|
| 194 |
use vars qw( $VERSION ); |
| 195 |
$VERSION = '2.14'; |
| 196 |
|
| 197 |
use vars qw( $sEngine $all $verbose $raw $iMax $print_version $debuglwp $debug ); |
| 198 |
use vars qw( @options $_host $_port $help $iShowCount $iGui $opt_list ); |
| 199 |
use vars qw( $opt_username $opt_password ); |
| 200 |
# Set default values: |
| 201 |
$iShowCount = 0; |
| 202 |
$iGui = 0; |
| 203 |
my $iTerse = 0; |
| 204 |
$_host = ''; |
| 205 |
$_port = ''; |
| 206 |
$sEngine = ''; |
| 207 |
undef $debug; |
| 208 |
$debuglwp = 0; |
| 209 |
$opt_list = 0; |
| 210 |
# Get the command-line options: |
| 211 |
&Getopt::Long::config(qw(no_ignore_case no_getopt_compat)); |
| 212 |
&usage('getopt failed') unless &GetOptions('all', \$all, |
| 213 |
'count', \$iShowCount, |
| 214 |
'debug:i', \$debug, |
| 215 |
'engine=s', \$sEngine, |
| 216 |
'gui', \$iGui, |
| 217 |
'help', \$help, |
| 218 |
'host=s', \$_host, |
| 219 |
'list', |
| 220 |
'lwpdebug', \$debuglwp, |
| 221 |
'max=i', \$iMax, |
| 222 |
'options=s@', \@options, |
| 223 |
'password=s', |
| 224 |
'port=s', \$_port, |
| 225 |
'raw', \$raw, |
| 226 |
'terse', \$iTerse, |
| 227 |
'username=s', |
| 228 |
'verbose', \$verbose, |
| 229 |
'VERSION', \$print_version, |
| 230 |
); |
| 231 |
&usage('user requested help') if $help; |
| 232 |
$debug = 1 if (defined($debug) and ($debug < 1)); |
| 233 |
|
| 234 |
&print_version() if defined($print_version); |
| 235 |
&list_engines() if ($opt_list); |
| 236 |
# there MUST be some argument(s) left, the query: |
| 237 |
&usage('no query found on command line') if (scalar(@ARGV) <= 0); |
| 238 |
|
| 239 |
&main(join(" ", @ARGV)); |
| 240 |
|
| 241 |
exit 0; |
| 242 |
|
| 243 |
sub print_version |
| 244 |
{ |
| 245 |
print "$0 version $VERSION\nWWW::Search version $WWW::Search::VERSION\n"; |
| 246 |
if ($sEngine ne '') |
| 247 |
{ |
| 248 |
my $mod = "WWW::Search::$sEngine"; |
| 249 |
if (eval "use $mod; 1;") |
| 250 |
{ |
| 251 |
my $e = '$'. $mod .'::VERSION'; |
| 252 |
# print STDERR " + e is ===$e===\n"; |
| 253 |
my $iVersion = eval $e; |
| 254 |
$iVersion ||= 'unknown'; |
| 255 |
print "$mod version $iVersion\n"; |
| 256 |
} # if |
| 257 |
} # if |
| 258 |
exit 0; |
| 259 |
} # print_version |
| 260 |
|
| 261 |
sub list_engines |
| 262 |
{ |
| 263 |
my @as = WWW::Search::installed_engines(); |
| 264 |
$, = "\n"; |
| 265 |
print STDERR (sort @as), ''; |
| 266 |
exit 0; |
| 267 |
} # list_engines |
| 268 |
|
| 269 |
my $verbose_code; |
| 270 |
|
| 271 |
sub print_result { |
| 272 |
my($result, $count) = @_; |
| 273 |
return if $iTerse; |
| 274 |
my($prefix) = ""; |
| 275 |
if ($verbose) { |
| 276 |
my(@attribs) = (); |
| 277 |
$prefix = "$count. "; |
| 278 |
if (!defined($verbose_code)) { |
| 279 |
$verbose_code = ""; |
| 280 |
foreach (qw(title description score normalized_score size change_date index_date)) { |
| 281 |
$verbose_code .= "push(\@attribs, \"$_: \" . \$result->$_())\n" . |
| 282 |
"\tif (defined(\$result->$_()));\n"; |
| 283 |
}; |
| 284 |
}; |
| 285 |
eval $verbose_code; |
| 286 |
$prefix .= "(" . join(",\n\t", @attribs) . ")\n\t" |
| 287 |
if ($#attribs >= 0); |
| 288 |
}; |
| 289 |
|
| 290 |
if (defined($all)) { |
| 291 |
foreach ($result->urls()) { |
| 292 |
print "$prefix$_\n"; |
| 293 |
$prefix = "\t"; |
| 294 |
}; |
| 295 |
} else { |
| 296 |
if (defined($raw)) { |
| 297 |
print $result->raw(), "\n"; |
| 298 |
} else { |
| 299 |
print $prefix, $result->url, "\n"; |
| 300 |
}; |
| 301 |
}; |
| 302 |
} |
| 303 |
|
| 304 |
sub print_error { |
| 305 |
my ($msg, $count) = @_; |
| 306 |
my $error = $verbose ? sprintf("[%3d] ", $count) : ''; |
| 307 |
$error .= $msg; |
| 308 |
print STDERR $error, "\n" if $error ne ''; |
| 309 |
} |
| 310 |
|
| 311 |
sub main { |
| 312 |
my $query = shift; |
| 313 |
my $count = 0; |
| 314 |
my $search = new WWW::Search($sEngine); |
| 315 |
$search->{_host} = $_host if $_host ne ''; |
| 316 |
$search->{_port} = $_port if $_port ne ''; |
| 317 |
my %hsOptions = (); |
| 318 |
|
| 319 |
if (0 < $debuglwp) { |
| 320 |
require LWP::Debug; |
| 321 |
LWP::Debug::level('+'); |
| 322 |
} |
| 323 |
|
| 324 |
$search->http_proxy($ENV{'HTTP_PROXY'}) if ($ENV{'HTTP_PROXY'}); |
| 325 |
$search->http_proxy($ENV{'http_proxy'}) if ($ENV{'http_proxy'}); |
| 326 |
|
| 327 |
if (0 < scalar(@options)) |
| 328 |
{ |
| 329 |
foreach my $sPair (@options) |
| 330 |
{ |
| 331 |
if ($sPair =~ m/^([^=]+)=(.*)$/) |
| 332 |
{ |
| 333 |
my ($key, $value) = ($1, $2); |
| 334 |
# This is a bit of a hack. A set of CGI options is not |
| 335 |
# strictly a hash, because multiple values for the same key |
| 336 |
# can be specified. To get around this, we rely on the fact |
| 337 |
# that this hash of options is only used to construct a CGI |
| 338 |
# parameter list. If we see multiple values for the same key, |
| 339 |
# we append the multiple values onto the value of the key in |
| 340 |
# CGI '?key=value' format. |
| 341 |
if (exists($hsOptions{$key}) && $hsOptions{$key} ne '') |
| 342 |
{ |
| 343 |
# There was already an option of this key given; append |
| 344 |
# multiple values as CGI arguments: |
| 345 |
$hsOptions{$key} .= '&'.$key.'='.WWW::Search::escape_query($value); |
| 346 |
} # if exists |
| 347 |
else |
| 348 |
{ |
| 349 |
# This is the only instance of this key; just insert the |
| 350 |
# hash value: |
| 351 |
$hsOptions{$key} = WWW::Search::escape_query($value); |
| 352 |
} |
| 353 |
} # if option is of the form key=value |
| 354 |
else |
| 355 |
{ |
| 356 |
$hsOptions{$sPair} = ''; |
| 357 |
} |
| 358 |
} # foreach $sPair |
| 359 |
} # if @options |
| 360 |
|
| 361 |
if (defined($iMax)) |
| 362 |
{ |
| 363 |
$search->maximum_to_retrieve($iMax); |
| 364 |
} |
| 365 |
else |
| 366 |
{ |
| 367 |
$iMax = 10000; |
| 368 |
} |
| 369 |
|
| 370 |
$hsOptions{'search_debug'} = $debug if (defined($debug) and (0 < $debug)); |
| 371 |
|
| 372 |
$iGui |
| 373 |
? $search->gui_query(WWW::Search::escape_query($query), \%hsOptions) |
| 374 |
: $search->native_query(WWW::Search::escape_query($query), \%hsOptions); |
| 375 |
$search->login($opt_username, $opt_password); |
| 376 |
|
| 377 |
my($way) = 0; # 0=piecemeal, 1=all at once |
| 378 |
my($result); |
| 379 |
if (($iShowCount) && defined($search->approximate_result_count)) |
| 380 |
{ |
| 381 |
print "There are approximately " if $verbose; |
| 382 |
print $search->approximate_result_count; |
| 383 |
print " results." if $verbose; |
| 384 |
print "\n"; |
| 385 |
} # if |
| 386 |
my $iNumPrinted = 0; |
| 387 |
if ($way) # return all at once. |
| 388 |
{ |
| 389 |
foreach $result ($search->results()) |
| 390 |
{ |
| 391 |
print_result($result, ++$count); |
| 392 |
$iNumPrinted++; |
| 393 |
last if ($iMax < $count); |
| 394 |
}; |
| 395 |
} |
| 396 |
else # return page by page |
| 397 |
{ |
| 398 |
while ($result = $search->next_result()) |
| 399 |
{ |
| 400 |
print_result($result, ++$count); |
| 401 |
$iNumPrinted++; |
| 402 |
last if ($iMax < $count); |
| 403 |
} # while |
| 404 |
} # else page by page |
| 405 |
if ($iShowCount) |
| 406 |
{ |
| 407 |
print "There are exactly " if $verbose; |
| 408 |
print $iNumPrinted; |
| 409 |
print " results (maximum of $iMax were requested)." if $verbose; |
| 410 |
print "\n"; |
| 411 |
} # if |
| 412 |
|
| 413 |
# handle errors |
| 414 |
if ($count == 0) { |
| 415 |
my($response) = $search->response(); |
| 416 |
my $nothing = $verbose ? "Nothing found." : ''; |
| 417 |
if ($response->is_success) { |
| 418 |
print_error($nothing, $count); |
| 419 |
} else { |
| 420 |
print_error("Error: " . $response->as_string(), $count); |
| 421 |
}; |
| 422 |
}; |
| 423 |
|
| 424 |
}; |
| 425 |
|