| 1 | # Rambler.pm | ||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||
| 3 | # Copyright (C) 2004 by Artur Penttinen | ||||||||||||||||||||||||||||
| 4 | # $Id: Rambler.pm,v 1.2 2010/08/23 12:29:15 artur Exp artur $ | ||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||
| 6 | # Complete copyright notice follows below. | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | package WWW::Search::Rambler; | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use base "WWW::Search"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use WWW::SearchResult; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use Encode qw( from_to ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 1 | ▶ | use Encode::Byte; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 1 | ▶ | use URI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 1 | ▶ | use URI::Escape; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 1 | ▶ | use HTML::TreeBuilder; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 1 | ▶ | use HTTP::Cookies; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 1 | our $VERSION = qw$Revision: 1.2 $[1]; | |||||||||||||||||||||||||||
| 23 | 1 | our $MAINTAINER = 'Artur Penttinen <artur+perl@niif.spb.su>'; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 1 | our $iMustPause = 1; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 1 | ▶ | sub native_setup_search ($$$) { | ||||||||||||||||||||||||||
|
Subroutine
native_setup_search: called CC=10 SCAR=29.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 28 | 1 | my ( $self,$query,$opt ) = @_; | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | printf STDERR " + native_setup_search('%s','%s')\n",$query,$opt || "" | ||||||||||||||||||||||||||||
| 31 | 1 | ▶ | if ($self->{'_debug'}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$opt || ''
MC/DC0%0/1
$opt || ''
$opt
Truth table0%0/2
A
A= $opt
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 1 | ▶ | unless ($self->{'charset'} =~ m#utf-?8#i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | 1 | from_to ($query,$self->{'charset'},"utf8"); | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 1 | $self->{'native_query'} = uri_escape ($query); | |||||||||||||||||||||||||||
| 38 | 1 | $self->{'_next_to_retrieve'} = 0; | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 1 | $self->{'agent_name'} = "Mozilla/4.0 (compatible; MSIE 5.5; Windows 98)"; | |||||||||||||||||||||||||||
| 41 | 1 | $self->{'agent_e_mail'} = "nobody\@niif.spb.su"; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 1 | ▶ | $self->{'search_base_url'} ||= "http://nova.rambler.ru"; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"search_base_url"} ||= "http://nova.rambler.ru"
MC/DC0%0/1
$self->{"search_base_url"} ||= "http://nova.rambler.ru"
$self->{"search_base_url"}
Truth table50%1/2
A
A= $self->{"search_base_url"}
| |||||||||||||||||||||||||||||
| 44 | 1 | $self->{'search_base_path'} = "/search"; | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 1 | $self->cookie_jar (new HTTP::Cookies); | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 1 | ▶ | unless (defined ($self->{'_options'})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | # We do not clobber the existing _options hash, if there is one; | ||||||||||||||||||||||||||||
| 50 | # e.g. if gui_search () was already called on this object | ||||||||||||||||||||||||||||
| 51 | 1 | $self->{'_options'} = { 'words' => $self->{'native_query'}, | |||||||||||||||||||||||||||
| 52 | 'where' => "1", | ||||||||||||||||||||||||||||
| 53 | 'noglue' => "1", | ||||||||||||||||||||||||||||
| 54 | # 'n' => $self->{_hits_per_page}, | ||||||||||||||||||||||||||||
| 55 | # 'b' => $self->{_next_to_retrieve}-1 | ||||||||||||||||||||||||||||
| 56 | }; | ||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 1 | my $opts = $self->{'_options'}; | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 1 | foreach my $key (keys %$opt) { | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 0 | ▶ | if (WWW::Search::generic_option ($key)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 0 | ▶ | $self->{$key} = $opts->{$key} if (exists ($opts->{$key})); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 0 | delete $opts->{$key}; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | else { | ||||||||||||||||||||||||||||
| 68 | 0 | ▶ | $opts->{$key} = $opt->{$key} if (exists ($opt->{$key})); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | $self->{'_next_url'} = $self->{'search_base_url'} . | ||||||||||||||||||||||||||||
| 74 | 1 | $self->{'search_base_path'} .'?'. | |||||||||||||||||||||||||||
| 75 | $self->hash_to_cgi_string ($opts); | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 1 | return; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 1 | ▶ | sub parse_tree ($$) { | ||||||||||||||||||||||||||
|
Subroutine
parse_tree: called CC=16 SCAR=51.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 81 | 1 | my ( $self,$oTree ) = @_; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | print STDERR " + ::Rambler got a tree $oTree\n" | ||||||||||||||||||||||||||||
| 84 | 1 | ▶ | if ($self->{'_debug'} >= 2); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | # Every time we get a page from rambler.ru, we have to pause before | ||||||||||||||||||||||||||||
| 87 | # fetching another. | ||||||||||||||||||||||||||||
| 88 | 1 | $iMustPause++; | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 1 | my $hits_found = 0; | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | # Only try to parse the hit count if we haven't done so already: | ||||||||||||||||||||||||||||
| 93 | printf STDERR " + start, approx_h_c is ==%d==\n", | ||||||||||||||||||||||||||||
| 94 | 1 | ▶ | $self->approximate_hit_count() if ($self->{'_debug'} >= 2); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 1 | ▶ | if ($self->approximate_hit_count () < 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 1 | return $hits_found; | |||||||||||||||||||||||||||
| 98 | } # if | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | my @aoDIV = $oTree->look_down ('_tag' => "div", | ||||||||||||||||||||||||||||
| 101 | 0 | ▶ | sub { $_[0]->attr ('class') && | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 0 | ▶ | $_[0]->attr ('class') =~ m#^search-results# }) or return 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 0 | my @aoLI = $aoDIV[0]->look_down ('_tag' => "li"); | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | LI_TAG: | ||||||||||||||||||||||||||||
| 107 | 0 | foreach my $oLI (@aoLI) { | |||||||||||||||||||||||||||
| 108 | # Sanity check: | ||||||||||||||||||||||||||||
| 109 | 0 | ▶ | next LI_TAG unless (ref ($oLI)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 0 | my @aoA = $oLI->look_down ('_tag' => "a"); | |||||||||||||||||||||||||||
| 112 | 0 | my $oA = shift @aoA; | |||||||||||||||||||||||||||
| 113 | 0 | ▶ | next LI_TAG unless (ref $oA); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 0 | ▶ | my $sTitle = $oA->as_text || ""; | ||||||||||||||||||||||||||
|
Condition0%0/2
$oA->as_text || ''
MC/DC0%0/1
$oA->as_text || ''
$oA->as_text
Truth table0%0/2
A
A= $oA->as_text
| |||||||||||||||||||||||||||||
| 116 | 0 | ▶ | my $sURL = $oA->attr ("href") || ""; | ||||||||||||||||||||||||||
|
Condition0%0/2
$oA->attr('href') || ''
MC/DC0%0/1
$oA->attr('href') || ''
$oA->attr('href')
Truth table0%0/2
A
A= $oA->attr('href')
| |||||||||||||||||||||||||||||
| 117 | my $sNOTE = $oLI->look_down ('_tag' => "div", | ||||||||||||||||||||||||||||
| 118 | 0 | ▶ | sub { $_[0]->attr ("class") eq "note" }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 0 | ▶ | next LI_TAG unless ($sURL ne ""); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | print STDERR " + raw URL is ==$sURL==\n" | ||||||||||||||||||||||||||||
| 123 | 0 | ▶ | if ($self->{'_debug'} >= 2); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 0 | my $hit = new WWW::SearchResult; | |||||||||||||||||||||||||||
| 126 | 0 | $hit->add_url ($sURL); | |||||||||||||||||||||||||||
| 127 | 0 | $sTitle = $self->strip ($sTitle); | |||||||||||||||||||||||||||
| 128 | 0 | $sNOTE = $self->strip ($sNOTE->as_HTML); | |||||||||||||||||||||||||||
| 129 | 0 | $hit->title ($sTitle); | |||||||||||||||||||||||||||
| 130 | 0 | $hit->description ($sNOTE); | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 0 | push @{ $self->{'cache'} },$hit; | |||||||||||||||||||||||||||
| 133 | 0 | $hits_found++; | |||||||||||||||||||||||||||
| 134 | } # foreach LI_TAG | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | # Now try to find the "next page" link: | ||||||||||||||||||||||||||||
| 137 | my @aoA = $oTree->look_down ('_tag' => "a", | ||||||||||||||||||||||||||||
| 138 | 0 | ▶ | sub { ( $_[0]->attr ("class") || "" ) =~ m#^n_pager_$iMustPause$# }); # | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Condition0%0/2
$_[0]->attr('class') || ''
MC/DC0%0/1
$_[0]->attr('class') || ''
$_[0]->attr('class')
Truth table0%0/2
A
A= $_[0]->attr('class')
| |||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | NEXT_A: | ||||||||||||||||||||||||||||
| 141 | 0 | foreach my $oA (@aoA) { | |||||||||||||||||||||||||||
| 142 | 0 | ▶ | next NEXT_A unless (ref $oA); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 0 | my $sAhtml = $oA->as_HTML (); | |||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | printf STDERR " + next A ==%s==\n", $sAhtml | ||||||||||||||||||||||||||||
| 146 | 0 | ▶ | if ($self->{'_debug'} >= 2); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 0 | ▶ | if ($self->_a_is_next_link ($oA)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | 0 | my $sURL = $oA->attr ("href"); | |||||||||||||||||||||||||||
| 150 | 0 | $self->{'_next_url'} = $self->absurl ($self->{'_prev_url'},$sURL); | |||||||||||||||||||||||||||
| 151 | 0 | last NEXT_A; | |||||||||||||||||||||||||||
| 152 | } # if | ||||||||||||||||||||||||||||
| 153 | } # foreach NEXT_A | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 0 | return $hits_found; | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 1 | ▶ | sub native_retrieve_some ($) { | ||||||||||||||||||||||||||
|
Subroutine
native_retrieve_some: called CC=15 SCAR=32.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 159 | 1 | my ( $self ) = @_; | |||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | printf STDERR " + %s::native_retrieve_some ()\n",__PACKAGE__ | ||||||||||||||||||||||||||||
| 162 | 1 | ▶ | if ($self->{'_debug'}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | # Fast exit if already done: | ||||||||||||||||||||||||||||
| 165 | 1 | ▶ | return unless (defined ($self->{'_next_url'})); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | # If this is not the first page of results, sleep so as to not | ||||||||||||||||||||||||||||
| 168 | # overload the server: | ||||||||||||||||||||||||||||
| 169 | 1 | ▶ | $self->user_agent_delay () if ($self->{'_next_to_retrieve'} > 1 || | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"_next_to_retrieve"} > 1 or $self->need_to_delay
MC/DC0%0/2
$self->{"_next_to_retrieve"} > 1 or $self->need_to_delay
$self->{"_next_to_retrieve"} > 1
$self->need_to_delay
Truth table33%1/3
A or B
A= $self->{"_next_to_retrieve"} > 1B= $self->need_to_delay
| |||||||||||||||||||||||||||||
| 170 | $self->need_to_delay ()); | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | # Get one page of results: | ||||||||||||||||||||||||||||
| 173 | printf STDERR " + submitting URL (%s)\n",$self->{'_next_url'} | ||||||||||||||||||||||||||||
| 174 | 1 | ▶ | if ($self->{'_debug'}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | my $response = $self->http_request ($self->http_method (), | ||||||||||||||||||||||||||||
| 177 | 1 | $self->{'_next_url'}); | |||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | printf STDERR " + got response\n%s\n", $response | ||||||||||||||||||||||||||||
| 180 | 1 | ▶ | if ($self->{'_debug'} >= 2); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 1 | $self->{'_prev_url'} = $self->{'_next_url'}; | |||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | # Assume there are no more results, unless we find out otherwise | ||||||||||||||||||||||||||||
| 185 | # when we parse the html: | ||||||||||||||||||||||||||||
| 186 | 1 | $self->{'_next_url'} = undef; | |||||||||||||||||||||||||||
| 187 | 1 | $self->{'response'} = $response; | |||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | printf STDERR " --- HTTP response is:%s\n", $response->as_string () | ||||||||||||||||||||||||||||
| 190 | 1 | ▶ | if ($self->{'_debug'} >= 5); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 1 | ▶ | unless ($response->is_success ()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 193 | printf STDERR " --- HTTP request failed, response is:\n%s", | ||||||||||||||||||||||||||||
| 194 | 0 | ▶ | $response->as_string if ($self->{'_debug'}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 195 | 0 | return undef; | |||||||||||||||||||||||||||
| 196 | } # if | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | # Pre-process the output: | ||||||||||||||||||||||||||||
| 199 | 1 | my $sPage = $self->preprocess_results_page ($response->content ()); | |||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | # Parse the output: | ||||||||||||||||||||||||||||
| 202 | 1 | my $tree; | |||||||||||||||||||||||||||
| 203 | 1 | ▶ | if (ref ($self->{'_treebuilder'})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 204 | print STDERR " + using existing _treebuilder\n" | ||||||||||||||||||||||||||||
| 205 | 0 | ▶ | if ($self->{_debug} >= 2); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 206 | # Assume that the backend has installed their own TreeBuilder | ||||||||||||||||||||||||||||
| 207 | 0 | $tree = $self->{'_treebuilder'}; | |||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | else { | ||||||||||||||||||||||||||||
| 210 | 1 | ▶ | print STDERR " + creating new _treebuilder\n" if ($self->{_debug}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 211 | 1 | $tree = new HTML::TreeBuilder (); # use all default options | |||||||||||||||||||||||||||
| 212 | 1 | $tree->store_comments ('yes'); | |||||||||||||||||||||||||||
| 213 | 1 | $self->{'_treebuilder'} = $tree; | |||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | # If a reset() method becomes available in HTML::TreeBuilder, | ||||||||||||||||||||||||||||
| 217 | # we can change this: | ||||||||||||||||||||||||||||
| 218 | 1 | $tree->www_search_reset (); | |||||||||||||||||||||||||||
| 219 | # print STDERR " + parsing content, tree is ", Dumper(\$tree) | ||||||||||||||||||||||||||||
| 220 | # if ($self->{'_debug'} >= 2); | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | 1 | $tree->parse ($sPage); | |||||||||||||||||||||||||||
| 223 | 1 | ▶ | print STDERR " + done parsing content.\n" if ($self->{'_debug'} >= 2); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 224 | 1 | $tree->eof (); | |||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | 1 | ▶ | print STDERR " + calling parse_tree...\n" if ($self->{'_debug'} >= 2); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 227 | 1 | return $self->parse_tree ($tree); | |||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | 1 | ▶ | sub http_request ($$$) { | ||||||||||||||||||||||||||
|
Subroutine
http_request: called CC=32 SCAR=50.5
Pod
documented
| |||||||||||||||||||||||||||||
| 231 | 1 | my ( $self,$method,$url ) = @_; | |||||||||||||||||||||||||||
| 232 | 1 | my $response; | |||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | 1 | ▶ | if ($self->{'_debug'} >= 50) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 235 | 0 | eval q{ use LWP::Debug qw(+) }; | |||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | 1 | ▶ | if (defined $self->{'search_from_file'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 239 | 0 | $response = $self->_http_request_from_file ($url); | |||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||
| 241 | else { | ||||||||||||||||||||||||||||
| 242 | # fetch it | ||||||||||||||||||||||||||||
| 243 | 1 | my $ua = $self->user_agent ($self->{'agent_name'}); | |||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | 1 | ▶ | unless (exists ($self->{'_prev_url'})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 246 | # Get cookie from first page | ||||||||||||||||||||||||||||
| 247 | 1 | my $request = new HTTP::Request ($method,"http://www.rambler.ru"); | |||||||||||||||||||||||||||
| 248 | 1 | $self->{'_cookie_jar'}->add_cookie_header ($request); | |||||||||||||||||||||||||||
| 249 | 1 | my $resp = $ua->request($request); | |||||||||||||||||||||||||||
| 250 | 1 | $self->{'_http_referer'} = "http://www.rambler.ru"; | |||||||||||||||||||||||||||
| 251 | 1 | my $cookie = $resp->header ("Set-Cookie"); | |||||||||||||||||||||||||||
| 252 | 1 | ▶ | if (defined ($cookie) && $cookie =~ m#ruid=(\S+);#) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $cookie and $cookie =~ /ruid=(\S+);/
MC/DC0%0/2
defined $cookie and $cookie =~ /ruid=(\S+);/
defined $cookie
$cookie =~ /ruid=(\S+);/
Truth table33%1/3
A and B
A= defined $cookieB= $cookie =~ /ruid=(\S+);/
| |||||||||||||||||||||||||||||
| 253 | 1 | $self->{'_cookie_ruid'} = $1; | |||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | printf STDERR "+ got cookie: %s\n",$self->{'_cookie_ruid'} || "(none)" | ||||||||||||||||||||||||||||
| 257 | 1 | ▶ | if ($self->{'_debug'}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$self->{'_cookie_ruid'} || '(none)'
MC/DC0%0/1
$self->{'_cookie_ruid'} || '(none)'
$self->{'_cookie_ruid'}
Truth table0%0/2
A
A= $self->{'_cookie_ruid'}
| |||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | 1 | sleep 2; # we will enter request :) | |||||||||||||||||||||||||||
| 260 | } | ||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | 1 | my $request = new HTTP::Request ($method,$url); | |||||||||||||||||||||||||||
| 263 | 1 | $request->header ("Content-Type","text/html; charset=utf8"); | |||||||||||||||||||||||||||
| 264 | $request->header ("Cookie",sprintf "ruid=%s",$self->{'_cookie_ruid'}) | ||||||||||||||||||||||||||||
| 265 | 1 | ▶ | if (exists $self->{'_cookie_ruid'}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | 1 | ▶ | $request->proxy_authorization_basic ($self->http_proxy_user, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 268 | $self->http_proxy_pwd) | ||||||||||||||||||||||||||||
| 269 | if ($self->is_http_proxy_auth_data ()); | ||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | $self->{'_cookie_jar'}->add_cookie_header ($request) | ||||||||||||||||||||||||||||
| 272 | 1 | ▶ | if (exists ($self->{'_cookie_jar'})); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | 1 | ▶ | if ($self->{'_http_referer'} && $self->{'_http_referer'} ne "") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"_http_referer"} and $self->{"_http_referer"} ne ""
MC/DC0%0/2
$self->{"_http_referer"} and $self->{"_http_referer"} ne ""
$self->{"_http_referer"}
$self->{"_http_referer"} ne ""
Truth table33%1/3
A and B
A= $self->{"_http_referer"}B= $self->{"_http_referer"} ne ""
| |||||||||||||||||||||||||||||
| 275 | # my $s = uri_escape ($self->{'_http_referer'}); | ||||||||||||||||||||||||||||
| 276 | 1 | my $s = $self->{'_http_referer'}; | |||||||||||||||||||||||||||
| 277 | printf STDERR " + referer(%s), ref(s) = %s\n",$s,ref($s) | ||||||||||||||||||||||||||||
| 278 | 1 | ▶ | if ($self->{'_debug'}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 279 | 1 | ▶ | $s = $s->as_string () if (ref ($s) =~ m#URI#); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 280 | 1 | $request->referer ($s); | |||||||||||||||||||||||||||
| 281 | } # if referer | ||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | printf STDERR " + original HTTP::Request is:\n%s\n", | ||||||||||||||||||||||||||||
| 284 | 1 | ▶ | $request->as_string () if ($self->{'_debug'} >= 3); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | TRY_GET: | ||||||||||||||||||||||||||||
| 287 | 1 | while (1) { | |||||||||||||||||||||||||||
| 288 | 1 | $response = $ua->request ($request); | |||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | printf STDERR " + got HTTP::Response (code=%d):\n%s", | ||||||||||||||||||||||||||||
| 291 | $response->code (),$response->headers ()->as_string () | ||||||||||||||||||||||||||||
| 292 | 1 | ▶ | if ($self->{'_debug'} >= 3); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | 1 | ▶ | if (exists ($self->{'_cookie_jar'})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 295 | 1 | $self->{'_cookie_jar'}->extract_cookies ($response); | |||||||||||||||||||||||||||
| 296 | $self->{'_cookie_jar'}->save () | ||||||||||||||||||||||||||||
| 297 | 1 | ▶ | if ($self->{'_cookie_jar_we_save'}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 298 | print STDERR " + WWW::Search just extracted cookies\n" | ||||||||||||||||||||||||||||
| 299 | 1 | ▶ | if ($self->{'_debug'} > 2); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 300 | print STDERR $self->{'_cookie_jar'}->as_string () | ||||||||||||||||||||||||||||
| 301 | 1 | ▶ | if ($self->{'_debug'} > 2); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 302 | # print STDERR Dumper($self->{'_cookie_jar'}) if DEBUG_COOKIES; | ||||||||||||||||||||||||||||
| 303 | } # if | ||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | 1 | ▶ | if ($self->{'search_to_file'} && $response->is_success ()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"search_to_file"} and $response->is_success
MC/DC0%0/2
$self->{"search_to_file"} and $response->is_success
$self->{"search_to_file"}
$response->is_success
Truth table33%1/3
A and B
A= $self->{"search_to_file"}B= $response->is_success
| |||||||||||||||||||||||||||||
| 306 | 0 | $self->_http_request_to_file ($url,$response); | |||||||||||||||||||||||||||
| 307 | } # if | ||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | 1 | ▶ | last TRY_GET if ($response->is_success ()); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 310 | 0 | ▶ | last TRY_GET if ($response->is_error ()); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 311 | 0 | ▶ | last TRY_GET if ($response->headers ()->header ("Client-Warning") =~ m#redirect loop detected#i); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | 0 | ▶ | if ($response->is_redirect () || | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$response->is_redirect or $response->message =~ /Object moved/i
MC/DC0%0/2
$response->is_redirect or $response->message =~ /Object moved/i
$response->is_redirect
$response->message =~ /Object moved/i
Truth table0%0/3
A or B
A= $response->is_redirectB= $response->message =~ /Object moved/i
| |||||||||||||||||||||||||||||
| 314 | $response->message =~ m#Object moved#i) { | ||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | 0 | my $sURL = $response->request->uri->as_string; | |||||||||||||||||||||||||||
| 317 | 0 | my $sURLredir = $response->headers->header ("Location"); | |||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||
| 319 | # Low-level loop detection: | ||||||||||||||||||||||||||||
| 320 | 0 | ▶ | last TRY_GET if ($sURLredir eq $sURL); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 321 | print STDERR " + 'Object moved' from $sURL to $sURLredir\n" | ||||||||||||||||||||||||||||
| 322 | 0 | ▶ | if ($self->{'_debug'} >= 2); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 323 | # Follow the redirect: | ||||||||||||||||||||||||||||
| 324 | 0 | $request = new HTTP::Request("GET", | |||||||||||||||||||||||||||
| 325 | URI->new_abs($sURLredir, $sURL)); | ||||||||||||||||||||||||||||
| 326 | 0 | $request->referer ($sURL); | |||||||||||||||||||||||||||
| 327 | $self->{'_cookie_jar'}->add_cookie_header ($request) | ||||||||||||||||||||||||||||
| 328 | 0 | ▶ | if (ref($self->{'_cookie_jar'})); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||
| 330 | print STDERR " + 'Object moved', new HTTP::Request is:\n", | ||||||||||||||||||||||||||||
| 331 | 0 | ▶ | $request->as_string () if ($self->{'_debug'} >= 3); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 332 | # Go back and try again | ||||||||||||||||||||||||||||
| 333 | } # if | ||||||||||||||||||||||||||||
| 334 | } # while infinite | ||||||||||||||||||||||||||||
| 335 | } # if not from_file | ||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | 1 | return $response; | |||||||||||||||||||||||||||
| 338 | } # http_request | ||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | 0 | ▶ | sub strip ($$) { | ||||||||||||||||||||||||||
|
Subroutine
strip: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 342 | 0 | my ( $self,$s ) = @_; | |||||||||||||||||||||||||||
| 343 | 0 | $s = WWW::Search::strip_tags ($s); | |||||||||||||||||||||||||||
| 344 | 0 | $s =~ s#\A[\240\t\r\n ]+ ##x; | |||||||||||||||||||||||||||
| 345 | 0 | $s =~ s# [\240\t\r\n ]+\Z##x; | |||||||||||||||||||||||||||
| 346 | 0 | return $s; | |||||||||||||||||||||||||||
| 347 | } | ||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | 0 | ▶ | sub _a_is_next_link ($;$) { | ||||||||||||||||||||||||||
|
Subroutine
_a_is_next_link: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 350 | 0 | my ( $self,$oA ) = @_; | |||||||||||||||||||||||||||
| 351 | 0 | ▶ | return 0 unless (defined $oA); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 352 | 0 | return $oA->attr ("class") =~ m#^n_pager_\d+#; | |||||||||||||||||||||||||||
| 353 | } | ||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||
| 355 | 1 | ▶ | sub preprocess_results_page ($$) { | ||||||||||||||||||||||||||
|
Subroutine
preprocess_results_page: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 356 | 1 | my ( $self,$text ) = @_; | |||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | 1 | ▶ | unless ($self->{'charset'} =~ m#utf-?8#) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 359 | 1 | from_to ($text,"utf8",$self->{'charset'}); | |||||||||||||||||||||||||||
| 360 | } | ||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | 1 | return $text; | |||||||||||||||||||||||||||
| 363 | } | ||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | 2 | ▶ | sub approximate_result_count ($) { | ||||||||||||||||||||||||||
|
Subroutine
approximate_result_count: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||||||||||||||||||||
| 366 | 2 | my ( $self ) = @_; | |||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | 2 | ▶ | if ($self->response->content =~ m#<div class="info">.+?(\d+).+?</div>#sm) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 369 | 0 | return $1; | |||||||||||||||||||||||||||
| 370 | } | ||||||||||||||||||||||||||||
| 371 | else { | ||||||||||||||||||||||||||||
| 372 | 2 | return 0; | |||||||||||||||||||||||||||
| 373 | } | ||||||||||||||||||||||||||||
| 374 | } | ||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||
| 376 | 1 | 1; | |||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||
| 378 | __END__ | ||||||||||||||||||||||||||||