blib/lib/WWW/Search/Teoma.pm

filter: Statementstmt 16.8% 13 / 77 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 60.0% 3 / 5 Podpod 0.0% 0 / 2 totaltotal 9.8% 16 / 163 CC 36 SCAR 63.8
CC
36
Cov
9%
native_retrieve_some
812.0
native_setup_search
90.0
BEGIN
1.0
CRAP 588.7
↑ Summary
1 # Teoma.pm
2 # by Jim Schneider
3 # Copyright (C) 2002, JRCS, Inc.
4 # Originally based on WWW::Search::AltaVista.pm
5  
6 package WWW::Search::Teoma;
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 WWW::Search::Teoma - class for searching www.teoma.com
12  
13  
14 =head1 SYNOPSIS
15  
16 require WWW::Search;
17 $search = new WWW::Search('Teoma');
18  
19  
20 =head1 DESCRIPTION
21  
22 This class is a Teoma specialization of WWW::Search.
23 It handles making and interpreting Teoma searches
24 F<http://www.teoma.com>. It is loosely based on the WWW::Search::AltaVista
25 class written by John Heidemann.
26  
27 This class exports no public interface; all interaction should
28 be done through WWW::Search objects.
29  
30  
31 =head1 SEE ALSO
32  
33 L<WWW::Search>, L<WWW::Search::AltaVista>
34  
35  
36 =head1 AUTHOR
37  
38 C<WWW::Search::Teoma> was hacked by Jim Schneider,
39 <perl@jrcsdevelopment.com>, using C<WWW::Search::AltaVista> as a guide.
40 C<WWW::Search::AltaVista> was written by John Heidemann,
41 <johnh@isi.edu>. C<WWW::Search::AltaVista> is maintained by Martin Thurn,
42 <mthurn@cpan.org>.
43  
44 =head1 COPYRIGHT
45  
46 Copyright (c) 1996-1998 University of Southern California.
47 All rights reserved.
48  
49 Redistribution and use in source and binary forms are permitted
50 provided that the above copyright notice and this paragraph are
51 duplicated in all such forms and that any documentation, advertising
52 materials, and other materials related to such distribution and use
53 acknowledge that the software was developed by the University of
54 Southern California, Information Sciences Institute. The name of the
55 University may not be used to endorse or promote products derived from
56 this software without specific prior written permission.
57  
58 THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
59 WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
60 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
61  
62 Parts are copyright (c) 2002, JRCS, Inc.
63  
64 =cut
65  
66 #####################################################################
67  
68 1 our @ISA = qw(WWW::Search);
69 1 our $VERSION = '0.01';
70  
71 1 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
72 1 ▶use WWW::Search qw( generic_option unescape_query );
Subroutine BEGIN: called CC=1 SCAR=0.0
73 1 require WWW::SearchResult;
74  
75 # private
76 sub native_setup_search
77 0 ▶ {
Subroutine native_setup_search: not called CC=9 SCAR=45.0
Pod undocumented
78 0 my($self, $native_query, $native_options_ref) = @_;
79 0 $self->user_agent('user');
80 0 $self->{_next_to_retrieve} = 0;
81 0 ▶ if (!defined($self->{_options}))
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"_options"}) 0 0 0
82 {
83 $self->{_options} = {
84 0 'search_url' => 'http://s.teoma.com/search',
85 };
86 } # if
87 0 my($options_ref) = $self->{_options};
88 0 ▶ if (defined($native_options_ref))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $native_options_ref) 0 0 0
89 {
90 # Copy in new options.
91 0 foreach (keys %$native_options_ref)
92 {
93 0 $options_ref->{$_} = $native_options_ref->{$_};
94 } # foreach
95 } # if
96 # Process the options.
97 0 my $options = '';
98 0 foreach my $key (keys %$options_ref)
99 {
100 0 ▶ next if (generic_option($key));
Branch: 1 branch
BranchTrueFalseTotal
if generic_option($key) 0 0 0
101 0 $options .= $key . '=' . $options_ref->{$key} . '&';
102 } # foreach
103 0 $self->{_debug} = $options_ref->{'search_debug'};
104 0 ▶ $self->{_debug} = 2 if ($options_ref->{'search_parse_debug'});
Branch: 1 branch
BranchTrueFalseTotal
if $options_ref->{"search_parse_debug"} 0 0 0
105 0 ▶ $self->{_debug} = 0 if (!defined($self->{_debug}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"_debug"} 0 0 0
106  
107 # Finally figure out the url.
108 0 $self->{_base_url} = 'http://s.teoma.com/';
109 $self->{_next_url} =
110 0 $self->{_options}{'search_url'} . "?q=" . $native_query;
111 0 ▶ print STDERR $self->{_next_url} . "\n" if ($self->{_debug});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_debug"} 0 0 0
112 } # native_setup_search
113  
114 # private
115 sub native_retrieve_some
116 0 ▶{
Subroutine native_retrieve_some: not called CC=28 SCAR=67.0
Pod undocumented
117 0 my ($self) = @_;
118  
119 # fast exit if already done
120 0 ▶ return undef if (!defined($self->{_next_url}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"_next_url"} 0 0 0
121  
122 # get some
123 0 ▶ print STDERR "WWW::Search::Teoma::native_retrieve_some: fetching " . $self->{_next_url} . "\n" if ($self->{_debug});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_debug"} 0 0 0
124 0 my($response) = $self->http_request('GET', $self->{_next_url});
125 0 $self->{response} = $response;
126 0 ▶ if (!$response->is_success)
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
127 {
128 0 ▶ print STDERR " + failed: $response\n" if ($self->{_debug});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_debug"} 0 0 0
129 0 return undef;
130 } # if
131  
132 # Clear the next URL first
133 0 $self->{_next_url} = undef;
134 # parse the output
135 0 my($HEADER, $HITS, $INHIT, $DESC, $URL, $TRAILER, $POST_NEXT) = (1..10); # order matters
136 0 my($hits_found) = 0;
137 0 my($state) = ($HEADER);
138 0 my($hit) = undef;
139 0 my($raw) = '';
140 0 foreach ($self->split_lines($response->content()))
141 {
142 0 ▶ next if m@^$@; # short circuit for blank lines
Branch: 1 branch
BranchTrueFalseTotal
if /^$/ 0 0 0
143 0 ▶ print STDERR "PARSE(0:RAW): $_\n" if ($self->{_debug} >= 3);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_debug"} >= 3 0 0 0
144 0 ▶ if (0) { }
Branch: 5 branches
BranchTrueFalseTotal
if (do { $state == $HEADER and /<span id="ResultsControl_estimatedResults">(\d+(,\d+)*)/ }) { } 0 0 0
elsif ($state == $HITS and /<span class="resultTxt">/i) { } 0 0 0
elsif ($state == $INHIT and m[<a\s+href=".*\&u=([^&="]*)">(.*)</a>]i) { } 0 0 0
elsif ($state == $DESC and m[\s+(.*)\s*<br\s*/>]i) { } 0 0 0
elsif ($state == $HITS and m[href="(search?[^"]*)">>></a>]i) { } 0 0 0
145  
146 ######
147 # HEADER PARSING: find the number of hits
148 #
149 ▶ elsif ($state == $HEADER && m|<span id="ResultsControl_estimatedResults">(\d+(,\d+)*)|)
Condition0%0/3
$state == $HEADER and /<span id="ResultsControl_estimatedResults">(\d+(,\d+)*)/
!ll&&!rl&&r
000
MC/DC0%0/2
$state == $HEADER and /<span id="ResultsControl_estimatedResults">(\d+(,\d+)*)/
$state == $HEADER /<span id="ResultsControl_estimatedResults">(\d+(,\d+)*)/
Truth table0%0/3
A and B
A= $state == $HEADERB= /<span id="ResultsControl_estimatedResults">(\d+(,\d+)*)/
ABresult
0X0
100
111
150 {
151 0 my($n) = $1;
152 0 $n =~ s/,//g;
153 0 $self->approximate_result_count($n);
154 0 ▶ print STDERR "PARSE(10:HEADER->HITS): $n documents found.\n" if ($self->{_debug} >= 2);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_debug"} >= 2 0 0 0
155 0 ▶ return 0 unless 0 < $n;
Branch: 1 branch
BranchTrueFalseTotal
unless 0 < $n 0 0 0
156 0 $state = $HITS;
157 }
158  
159 ######
160 # HITS PARSING: find each hit
161 #
162 ▶ elsif ($state == $HITS && m|<span class="resultTxt">|i)
Condition0%0/3
$state == $HITS and /<span class="resultTxt">/i
!ll&&!rl&&r
000
MC/DC0%0/2
$state == $HITS and /<span class="resultTxt">/i
$state == $HITS /<span class="resultTxt">/i
Truth table0%0/3
A and B
A= $state == $HITSB= /<span class="resultTxt">/i
ABresult
0X0
100
111
163 {
164 0 $state = $INHIT;
165 0 ▶ print STDERR "PARSE(HITS): Start of hit.\n" if $self->{_debug}>=2;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_debug"} >= 2 0 0 0
166 0 ▶ push @{$self->{cache}}, $hit if $hit;
Branch: 1 branch
BranchTrueFalseTotal
if $hit 0 0 0
167 0 $hit = new WWW::SearchResult;
168 0 $hits_found++;
169 }
170 ▶ elsif ($state == $INHIT && m|<a\s+href=".*\&u=([^&="]*)">(.*)</a>|i)
Condition0%0/3
$state == $INHIT and m[<a\s+href=".*\&u=([^&="]*)">(.*)</a>]i
!ll&&!rl&&r
000
MC/DC0%0/2
$state == $INHIT and m[<a\s+href=".*\&u=([^&="]*)">(.*)</a>]i
$state == $INHIT m[<a\s+href=".*\&u=([^&="]*)">(.*)</a>]i
Truth table0%0/3
A and B
A= $state == $INHITB= m[<a\s+href=".*\&u=([^&="]*)">(.*)</a>]i
ABresult
0X0
100
111
171 {
172 0 $state = $DESC;
173 0 $hit->title($1);
174 0 $hit->add_url($1);
175 0 ▶ print STDERR "PARSE(INHIT): Title: $1\n" if $self->{_debug}>=2;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_debug"} >= 2 0 0 0
176 }
177 ▶ elsif ($state == $DESC && m|\s+(.*)\s*<br\s*/>|i)
Condition0%0/3
$state == $DESC and m[\s+(.*)\s*<br\s*/>]i
!ll&&!rl&&r
000
MC/DC0%0/2
$state == $DESC and m[\s+(.*)\s*<br\s*/>]i
$state == $DESC m[\s+(.*)\s*<br\s*/>]i
Truth table0%0/3
A and B
A= $state == $DESCB= m[\s+(.*)\s*<br\s*/>]i
ABresult
0X0
100
111
178 {
179 0 $state = $HITS;
180 0 $hit->description($1);
181 0 ▶ print STDERR "PARSE(DESC): Description found.\n" if $self->{_debug}>=2;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_debug"} >= 2 0 0 0
182 }
183 ▶ elsif ($state == $HITS && m|href="(search?[^"]*)">>></a>|i)
Condition0%0/3
$state == $HITS and m[href="(search?[^"]*)">>></a>]i
!ll&&!rl&&r
000
MC/DC0%0/2
$state == $HITS and m[href="(search?[^"]*)">>></a>]i
$state == $HITS m[href="(search?[^"]*)">>></a>]i
Truth table0%0/3
A and B
A= $state == $HITSB= m[href="(search?[^"]*)">>></a>]i
ABresult
0X0
100
111
184 {
185 0 $self->{_next_url} = $self->{_base_url} . $1;
186 0 ▶ print STDERR "PARSE(HITS): Found next URL\n", if $self->{_debug}>=2;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_debug"} >= 2 0 0 0
187 0 last;
188 }
189 else
190 {
191 0 ▶ print STDERR "*** No match ***\n" if $self->{_debug}>=2;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_debug"} >= 2 0 0 0
192 }
193  
194 }
195 # sleep so as to not overload Teoma
196 0 ▶ $self->user_agent_delay if (defined($self->{_next_url}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"_next_url"} 0 0 0
197  
198 0 ▶ push @{$self->{cache}}, $hit if $hit;
Branch: 1 branch
BranchTrueFalseTotal
if $hit 0 0 0
199 0 return $hits_found;
200 } # native_retrieve_some
201  
202 1 1;
203  
↑ Summary