/usr/local/bin/WebSearch

filter: Statementstmt 29.6% 40 / 135 Branchbran 7.1% 6 / 84 Conditioncond 5.8% 1 / 17 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 61.5% 8 / 13 Podpod n/a totaltotal 21.1% 55 / 260 CC 45 SCAR 62.3
CC
45
Cov
20%
main
930.0
print_result
90.0
usage
12.0
CRAP 505.8
« blib/lib/WWW/Search/Test.pm ↑ Summary blib/lib/WWW/Search.pm »
1 #!/usr/local/bin/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 0 ▶ my $msg = shift;
Subroutine usage: not called CC=3 SCAR=24.8
12 0 ▶ defined($msg) && ($msg ne '') && print STDERR "$0: $msg\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $msg and $msg ne "" and print STDERR "$0: $msg\n" 0 0 0
Condition0%0/3
defined $msg and $msg ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $msg and $msg ne ""
defined $msg $msg ne ""
Truth table0%0/3
A and B
A= defined $msgB= $msg ne ""
ABresult
0X0
100
111
13 0 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 0 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, <johnh@isi.edu>.
161 C<WebSearch> is maintained by Martin Thurn, <mthurn@cpan.org>.
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 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
187  
188 1 ▶&usage('no arguments given') if ($#ARGV == -1);
Branch: 1 branch
BranchTrueFalseTotal
if $#ARGV == -1 0 1 1
189 # &usage if ($#ARGV >= 0 && $ARGV[0] eq '-?');
190  
191 1 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
192 1 ▶use WWW::Search;
Subroutine BEGIN: called CC=1 SCAR=0.0
193  
194 1 ▶use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
195 1 $VERSION = '2.14';
196  
197 1 ▶use vars qw( $sEngine $all $verbose $raw $iMax $print_version $debuglwp $debug );
Subroutine BEGIN: called CC=1 SCAR=0.0
198 1 ▶use vars qw( @options $_host $_port $help $iShowCount $iGui $opt_list );
Subroutine BEGIN: called CC=1 SCAR=0.0
199 1 ▶use vars qw( $opt_username $opt_password );
Subroutine BEGIN: called CC=1 SCAR=0.0
200 # Set default values:
201 1 $iShowCount = 0;
202 1 $iGui = 0;
203 1 my $iTerse = 0;
204 1 $_host = '';
205 1 $_port = '';
206 1 $sEngine = '';
207 1 undef $debug;
208 1 $debuglwp = 0;
209 1 $opt_list = 0;
210 # Get the command-line options:
211 1 &Getopt::Long::config(qw(no_ignore_case no_getopt_compat));
212 1 ▶&usage('getopt failed') unless &GetOptions('all', \$all,
Branch: 1 branch
BranchTrueFalseTotal
unless &GetOptions("all", \$main::all, "count", \$main::iShowCount, "debug:i", \$main::debug, "engine=s", \$main::sEngine, "gui", \$main::iGui, "help", \$main::help, "host=s", \$main::_host, "list", "lwpdebug", \$main::debuglwp, "max=i", \$main::iMax, "options=s\@", \@main::options, "password=s", "port=s", \$main::_port, "raw", \$main::raw, "terse", \$iTerse, "username=s", "verbose", \$main::verbose, "VERSION", \$main::print_version) 0 1 1
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 1 ▶&usage('user requested help') if $help;
Branch: 1 branch
BranchTrueFalseTotal
if $main::help 0 1 1
232 1 ▶$debug = 1 if (defined($debug) and ($debug < 1));
Branch: 1 branch
BranchTrueFalseTotal
if defined $main::debug and $main::debug < 1 0 1 1
Condition33%1/3
defined $main::debug and $main::debug < 1
!ll&&!rl&&r
100
MC/DC0%0/2
defined $main::debug and $main::debug < 1
defined $main::debug $main::debug < 1
Truth table33%1/3
A and B
A= defined $main::debugB= $main::debug < 1
ABresult
0X0
100
111
233  
234 1 ▶&print_version() if defined($print_version);
Branch: 1 branch
BranchTrueFalseTotal
if defined $main::print_version 1 0 1
235 0 ▶&list_engines() if ($opt_list);
Branch: 1 branch
BranchTrueFalseTotal
if $main::opt_list 0 0 0
236 # there MUST be some argument(s) left, the query:
237 0 ▶&usage('no query found on command line') if (scalar(@ARGV) <= 0);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @ARGV <= 0 0 0 0
238  
239 0 &main(join(" ", @ARGV));
240  
241 0 exit 0;
242  
243 sub print_version
244 {
245 1 ▶ print "$0 version $VERSION\nWWW::Search version $WWW::Search::VERSION\n";
Subroutine print_version: called CC=4 SCAR=23.3
246 1 ▶ if ($sEngine ne '')
Branch: 1 branch
BranchTrueFalseTotal
if ($main::sEngine ne "") 0 1 1
247 {
248 0 my $mod = "WWW::Search::$sEngine";
249 0 ▶ if (eval "use $mod; 1;")
Branch: 1 branch
BranchTrueFalseTotal
if (eval "use $mod; 1;") 0 0 0
250 {
251 0 my $e = '$'. $mod .'::VERSION';
252 # print STDERR " + e is ===$e===\n";
253 0 my $iVersion = eval $e;
254 0 ▶ $iVersion ||= 'unknown';
Condition0%0/2
$iVersion ||= "unknown"
l!l
00
MC/DC0%0/1
$iVersion ||= "unknown"
$iVersion
Truth table0%0/2
A
A= $iVersion
Aresult
11
00
255 0 print "$mod version $iVersion\n";
256 } # if
257 } # if
258 1 exit 0;
259 } # print_version
260  
261 sub list_engines
262 {
263 0 ▶ my @as = WWW::Search::installed_engines();
Subroutine list_engines: not called CC=1 SCAR=6.9
264 0 $, = "\n";
265 0 print STDERR (sort @as), '';
266 0 exit 0;
267 } # list_engines
268  
269 0 my $verbose_code;
270  
271 sub print_result {
272 0 ▶ my($result, $count) = @_;
Subroutine print_result: not called CC=9 SCAR=45.0
273 0 ▶ return if $iTerse;
Branch: 1 branch
BranchTrueFalseTotal
if $iTerse 0 0 0
274 0 my($prefix) = "";
275 0 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main::verbose) 0 0 0
276 0 my(@attribs) = ();
277 0 $prefix = "$count. ";
278 0 ▶ if (!defined($verbose_code)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $verbose_code) 0 0 0
279 0 $verbose_code = "";
280 0 foreach (qw(title description score normalized_score size change_date index_date)) {
281 0 $verbose_code .= "push(\@attribs, \"$_: \" . \$result->$_())\n" .
282 "\tif (defined(\$result->$_()));\n";
283 };
284 };
285 0 eval $verbose_code;
286 0 ▶ $prefix .= "(" . join(",\n\t", @attribs) . ")\n\t"
Branch: 1 branch
BranchTrueFalseTotal
if $#attribs >= 0 0 0 0
287 if ($#attribs >= 0);
288 };
289  
290 0 ▶ if (defined($all)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $main::all) { } 0 0 0
291 0 foreach ($result->urls()) {
292 0 print "$prefix$_\n";
293 0 $prefix = "\t";
294 };
295 } else {
296 0 ▶ if (defined($raw)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $main::raw) { } 0 0 0
297 0 print $result->raw(), "\n";
298 } else {
299 0 print $prefix, $result->url, "\n";
300 };
301 };
302 }
303  
304 sub print_error {
305 0 ▶ my ($msg, $count) = @_;
Subroutine print_error: not called CC=3 SCAR=24.8
306 0 ▶ my $error = $verbose ? sprintf("[%3d] ", $count) : '';
Branch: 1 branch
BranchTrueFalseTotal
$main::verbose ? : 0 0 0
307 0 $error .= $msg;
308 0 ▶ print STDERR $error, "\n" if $error ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $error ne "" 0 0 0
309 }
310  
311 sub main {
312 0 ▶ my $query = shift;
Subroutine main: not called CC=30 SCAR=68.4
313 0 my $count = 0;
314 0 my $search = new WWW::Search($sEngine);
315 0 ▶ $search->{_host} = $_host if $_host ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $main::_host ne "" 0 0 0
316 0 ▶ $search->{_port} = $_port if $_port ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $main::_port ne "" 0 0 0
317 0 my %hsOptions = ();
318  
319 0 ▶ if (0 < $debuglwp) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 < $main::debuglwp) 0 0 0
320 0 require LWP::Debug;
321 0 LWP::Debug::level('+');
322 }
323  
324 0 ▶ $search->http_proxy($ENV{'HTTP_PROXY'}) if ($ENV{'HTTP_PROXY'});
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"HTTP_PROXY"} 0 0 0
325 0 ▶ $search->http_proxy($ENV{'http_proxy'}) if ($ENV{'http_proxy'});
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"http_proxy"} 0 0 0
326  
327 0 ▶ if (0 < scalar(@options))
Branch: 1 branch
BranchTrueFalseTotal
if (0 < scalar @main::options) 0 0 0
328 {
329 0 foreach my $sPair (@options)
330 {
331 0 ▶ if ($sPair =~ m/^([^=]+)=(.*)$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($sPair =~ /^([^=]+)=(.*)$/) { } 0 0 0
332 {
333 0 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 0 ▶ if (exists($hsOptions{$key}) && $hsOptions{$key} ne '')
Branch: 1 branch
BranchTrueFalseTotal
if (exists $hsOptions{$key} and $hsOptions{$key} ne '') { } 0 0 0
Condition0%0/3
exists $hsOptions{$key} and $hsOptions{$key} ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
exists $hsOptions{$key} and $hsOptions{$key} ne ""
exists $hsOptions{$key} $hsOptions{$key} ne ""
Truth table0%0/3
A and B
A= exists $hsOptions{$key}B= $hsOptions{$key} ne ""
ABresult
0X0
100
111
342 {
343 # There was already an option of this key given; append
344 # multiple values as CGI arguments:
345 0 $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 0 $hsOptions{$key} = WWW::Search::escape_query($value);
352 }
353 } # if option is of the form key=value
354 else
355 {
356 0 $hsOptions{$sPair} = '';
357 }
358 } # foreach $sPair
359 } # if @options
360  
361 0 ▶ if (defined($iMax))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $main::iMax) { } 0 0 0
362 {
363 0 $search->maximum_to_retrieve($iMax);
364 }
365 else
366 {
367 0 $iMax = 10000;
368 }
369  
370 0 ▶ $hsOptions{'search_debug'} = $debug if (defined($debug) and (0 < $debug));
Branch: 1 branch
BranchTrueFalseTotal
if defined $main::debug and 0 < $main::debug 0 0 0
Condition0%0/3
defined $main::debug and 0 < $main::debug
!ll&&!rl&&r
000
MC/DC0%0/2
defined $main::debug and 0 < $main::debug
defined $main::debug 0 < $main::debug
Truth table0%0/3
A and B
A= defined $main::debugB= 0 < $main::debug
ABresult
0X0
100
111
371  
372 0 ▶ $iGui
Branch: 1 branch
BranchTrueFalseTotal
$main::iGui ? : 0 0 0
373 ? $search->gui_query(WWW::Search::escape_query($query), \%hsOptions)
374 : $search->native_query(WWW::Search::escape_query($query), \%hsOptions);
375 0 $search->login($opt_username, $opt_password);
376  
377 0 my($way) = 0; # 0=piecemeal, 1=all at once
378 0 my($result);
379 0 ▶ if (($iShowCount) && defined($search->approximate_result_count))
Branch: 1 branch
BranchTrueFalseTotal
if ($main::iShowCount and defined $search->approximate_result_count) 0 0 0
Condition0%0/3
$main::iShowCount and defined $search->approximate_result_count
!ll&&!rl&&r
000
MC/DC0%0/2
$main::iShowCount and defined $search->approximate_result_count
$main::iShowCount defined $search->approximate_result_count
Truth table0%0/3
A and B
A= $main::iShowCountB= defined $search->approximate_result_count
ABresult
0X0
100
111
380 {
381 0 ▶ print "There are approximately " if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $main::verbose 0 0 0
382 0 print $search->approximate_result_count;
383 0 ▶ print " results." if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $main::verbose 0 0 0
384 0 print "\n";
385 } # if
386 0 my $iNumPrinted = 0;
387 0 ▶ if ($way) # return all at once.
Branch: 1 branch
BranchTrueFalseTotal
if ($way) { } 0 0 0
388 {
389 0 foreach $result ($search->results())
390 {
391 0 print_result($result, ++$count);
392 0 $iNumPrinted++;
393 0 ▶ last if ($iMax < $count);
Branch: 1 branch
BranchTrueFalseTotal
if $main::iMax < $count 0 0 0
394 };
395 }
396 else # return page by page
397 {
398 0 ▶ while ($result = $search->next_result())
Branch: 1 branch
BranchTrueFalseTotal
if ($result = $search->next_result) 0 0 0
399 {
400 0 print_result($result, ++$count);
401 0 $iNumPrinted++;
402 0 ▶ last if ($iMax < $count);
Branch: 1 branch
BranchTrueFalseTotal
if $main::iMax < $count 0 0 0
403 } # while
404 } # else page by page
405 0 ▶ if ($iShowCount)
Branch: 1 branch
BranchTrueFalseTotal
if ($main::iShowCount) 0 0 0
406 {
407 0 ▶ print "There are exactly " if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $main::verbose 0 0 0
408 0 print $iNumPrinted;
409 0 ▶ print " results (maximum of $iMax were requested)." if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $main::verbose 0 0 0
410 0 print "\n";
411 } # if
412  
413 # handle errors
414 0 ▶ if ($count == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count == 0) 0 0 0
415 0 my($response) = $search->response();
416 0 ▶ my $nothing = $verbose ? "Nothing found." : '';
Branch: 1 branch
BranchTrueFalseTotal
$main::verbose ? : 0 0 0
417 0 ▶ if ($response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) { } 0 0 0
418 0 print_error($nothing, $count);
419 } else {
420 0 print_error("Error: " . $response->as_string(), $count);
421 };
422 };
423  
424 };
425  
« blib/lib/WWW/Search/Test.pm ↑ Summary blib/lib/WWW/Search.pm »