| 1 | |||||||||||||
| 2 | # $Id: Yahoo.pm,v 2.380 2009/05/02 13:28:41 Martin Exp $ | ||||||||||||
| 3 | |||||||||||||
| 4 | =head1 NAME | ||||||||||||
| 5 | |||||||||||||
| 6 | WWW::Search::Yahoo - backend for searching www.yahoo.com | ||||||||||||
| 7 | |||||||||||||
| 8 | =head1 SYNOPSIS | ||||||||||||
| 9 | |||||||||||||
| 10 | use WWW::Search; | ||||||||||||
| 11 | my $oSearch = new WWW::Search('Yahoo'); | ||||||||||||
| 12 | my $sQuery = WWW::Search::escape_query("sushi restaurant Columbus Ohio"); | ||||||||||||
| 13 | $oSearch->native_query($sQuery); | ||||||||||||
| 14 | while (my $oResult = $oSearch->next_result()) | ||||||||||||
| 15 | print $oResult->url, "\n"; | ||||||||||||
| 16 | |||||||||||||
| 17 | =head1 DESCRIPTION | ||||||||||||
| 18 | |||||||||||||
| 19 | This class is a Yahoo specialization of L<WWW::Search>. It handles | ||||||||||||
| 20 | making and interpreting Yahoo searches F<http://www.yahoo.com>. | ||||||||||||
| 21 | |||||||||||||
| 22 | This class exports no public interface; all interaction should | ||||||||||||
| 23 | be done through L<WWW::Search> objects. | ||||||||||||
| 24 | |||||||||||||
| 25 | =head1 NOTES | ||||||||||||
| 26 | |||||||||||||
| 27 | The default search is: Yahoo's web-based index (not Directory). | ||||||||||||
| 28 | |||||||||||||
| 29 | =head1 PRIVATE METHODS | ||||||||||||
| 30 | |||||||||||||
| 31 | If you just want to write Perl code to search Yahoo, | ||||||||||||
| 32 | you do NOT need to read any further here. | ||||||||||||
| 33 | Instead, just read the L<WWW::Search> documentation. | ||||||||||||
| 34 | |||||||||||||
| 35 | If you want to write a subclass of this module | ||||||||||||
| 36 | (e.g. create a backend for another branch of Yahoo) | ||||||||||||
| 37 | then please read about the private methods here: | ||||||||||||
| 38 | |||||||||||||
| 39 | =cut | ||||||||||||
| 40 | |||||||||||||
| 41 | package WWW::Search::Yahoo; | ||||||||||||
| 42 | |||||||||||||
| 43 | 4 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 44 | 4 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 45 | |||||||||||||
| 46 | 4 | ▶ | use Carp (); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 47 | 4 | ▶ | use Data::Dumper; # for debugging only | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 48 | 4 | ▶ | use HTML::TreeBuilder; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 49 | 4 | ▶ | use WWW::Search; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 50 | 4 | ▶ | use WWW::SearchResult; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 51 | 4 | ▶ | use URI; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 52 | 4 | ▶ | use URI::Escape; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 53 | |||||||||||||
| 54 | 4 | ▶ | use vars qw( $iMustPause ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 55 | |||||||||||||
| 56 | 4 | ▶ | use base 'WWW::Search'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 57 | our | ||||||||||||
| 58 | 4 | $VERSION = do { my @r = (q$Revision: 2.380 $ =~ /\d+/g); sprintf "%d."."%03d" x $#r, @r }; | |||||||||||
| 59 | 4 | our $MAINTAINER = 'Martin Thurn <mthurn@cpan.org>'; | |||||||||||
| 60 | |||||||||||||
| 61 | # Thanks to the hard work of Gil Vidals and his team at | ||||||||||||
| 62 | # positionresearch.com, we know the following: In early 2004, | ||||||||||||
| 63 | # yahoo.com implemented new robot-blocking tactics that look for | ||||||||||||
| 64 | # frequent requests from the same client IP. One way around these | ||||||||||||
| 65 | # blocks is to slow down and randomize the timing of our requests. We | ||||||||||||
| 66 | # therefore insert a random sleep before every request except the | ||||||||||||
| 67 | # first one. This variable is equivalent to a "first-time" flag for | ||||||||||||
| 68 | # this purpose: | ||||||||||||
| 69 | 4 | $iMustPause = 0; | |||||||||||
| 70 | |||||||||||||
| 71 | =head2 gui_query | ||||||||||||
| 72 | |||||||||||||
| 73 | Yes, Virginia, we do try to emulate stupid-human queries. | ||||||||||||
| 74 | |||||||||||||
| 75 | =cut | ||||||||||||
| 76 | |||||||||||||
| 77 | sub gui_query | ||||||||||||
| 78 | 1 | ▶ | { | ||||||||||
|
Subroutine
gui_query: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 79 | 1 | my ($self, $sQuery, $rh) = @_; | |||||||||||
| 80 | 1 | $self->{'_options'} = { | |||||||||||
| 81 | 'p' => $sQuery, | ||||||||||||
| 82 | # 'hc' => 0, | ||||||||||||
| 83 | # 'hs' => 0, | ||||||||||||
| 84 | 'ei' => 'UTF-8', | ||||||||||||
| 85 | }; | ||||||||||||
| 86 | # print STDERR " + Yahoo::gui_query() is calling native_query()...\n"; | ||||||||||||
| 87 | 1 | $rh->{'search_base_url'} = 'http://search.yahoo.com'; | |||||||||||
| 88 | 1 | $rh->{'search_base_path'} = '/bin/query'; | |||||||||||
| 89 | 1 | return $self->native_query($sQuery, $rh); | |||||||||||
| 90 | } # gui_query | ||||||||||||
| 91 | |||||||||||||
| 92 | sub _native_setup_search | ||||||||||||
| 93 | 7 | ▶ | { | ||||||||||
|
Subroutine
_native_setup_search: called CC=11 SCAR=25.1
| |||||||||||||
| 94 | 7 | my ($self, $native_query, $rhOptsArg) = @_; | |||||||||||
| 95 | # print STDERR " + This is Yahoo::native_setup_search()...\n"; | ||||||||||||
| 96 | # print STDERR " + _options is ", $self->{'_options'}, "\n"; | ||||||||||||
| 97 | |||||||||||||
| 98 | 7 | $self->{'_hits_per_page'} = 100; | |||||||||||
| 99 | # $self->{'_hits_per_page'} = 10; # for debugging | ||||||||||||
| 100 | |||||||||||||
| 101 | # www.yahoo.com refuses robots. | ||||||||||||
| 102 | 7 | $self->user_agent('non-robot'); | |||||||||||
| 103 | # www.yahoo.com completely changes the HTML output depending on the | ||||||||||||
| 104 | # browser! | ||||||||||||
| 105 | # $self->{'agent_name'} = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows 98)'; | ||||||||||||
| 106 | # $self->{agent_e_mail} = 'mthurn@cpan.org'; | ||||||||||||
| 107 | |||||||||||||
| 108 | 7 | $self->{_next_to_retrieve} = 1; | |||||||||||
| 109 | |||||||||||||
| 110 | 7 | ▶ | $self->{'search_base_url'} ||= 'http://search.yahoo.com'; | ||||||||||
|
Condition100%2/2
$self->{"search_base_url"} ||= "http://search.yahoo.com"
MC/DC100%1/1
$self->{"search_base_url"} ||= "http://search.yahoo.com"
$self->{"search_base_url"}
Truth table100%2/2
A
A= $self->{"search_base_url"}
| |||||||||||||
| 111 | 7 | ▶ | $self->{'search_base_path'} ||= '/search'; | ||||||||||
|
Condition100%2/2
$self->{"search_base_path"} ||= "/search"
MC/DC100%1/1
$self->{"search_base_path"} ||= "/search"
$self->{"search_base_path"}
Truth table100%2/2
A
A= $self->{"search_base_path"}
| |||||||||||||
| 112 | 7 | ▶ | if (! defined($self->{'_options'})) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 113 | { | ||||||||||||
| 114 | # We do not clobber the existing _options hash, if there is one; | ||||||||||||
| 115 | # e.g. if gui_search() was already called on this object | ||||||||||||
| 116 | $self->{'_options'} = { | ||||||||||||
| 117 | 'vo' => $native_query, | ||||||||||||
| 118 | 'h' => 'w', # web sites | ||||||||||||
| 119 | 'n' => $self->{_hits_per_page}, | ||||||||||||
| 120 | # 'b' => $self->{_next_to_retrieve}-1, | ||||||||||||
| 121 | 3 | }; | |||||||||||
| 122 | } # if | ||||||||||||
| 123 | 7 | my $rhOptions = $self->{'_options'}; | |||||||||||
| 124 | 7 | ▶ | if (defined($rhOptsArg)) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 125 | { | ||||||||||||
| 126 | # Copy in new options, promoting special ones: | ||||||||||||
| 127 | 7 | foreach my $key (keys %$rhOptsArg) | |||||||||||
| 128 | { | ||||||||||||
| 129 | # print STDERR " + inspecting option $key..."; | ||||||||||||
| 130 | 15 | ▶ | if (WWW::Search::generic_option($key)) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 131 | { | ||||||||||||
| 132 | # print STDERR "promote & delete\n"; | ||||||||||||
| 133 | 15 | ▶ | $self->{$key} = $rhOptsArg->{$key} if defined($rhOptsArg->{$key}); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 134 | 15 | delete $rhOptsArg->{$key}; | |||||||||||
| 135 | } | ||||||||||||
| 136 | else | ||||||||||||
| 137 | { | ||||||||||||
| 138 | # print STDERR "copy\n"; | ||||||||||||
| 139 | 0 | ▶ | $rhOptions->{$key} = $rhOptsArg->{$key} if defined($rhOptsArg->{$key}); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 140 | } | ||||||||||||
| 141 | } # foreach | ||||||||||||
| 142 | # print STDERR " + resulting rhOptions is ", Dumper($rhOptions); | ||||||||||||
| 143 | # print STDERR " + resulting rhOptsArg is ", Dumper($rhOptsArg); | ||||||||||||
| 144 | } # if | ||||||||||||
| 145 | # Finally, figure out the url. | ||||||||||||
| 146 | 7 | $self->{'_next_url'} = $self->{'search_base_url'} . $self->{'search_base_path'} .'?'. $self->hash_to_cgi_string($rhOptions); | |||||||||||
| 147 | |||||||||||||
| 148 | 7 | ▶ | $self->{_debug} = $self->{'search_debug'} || 0; | ||||||||||
|
Condition50%1/2
$self->{'search_debug'} || 0
MC/DC0%0/1
$self->{'search_debug'} || 0
$self->{'search_debug'}
Truth table50%1/2
A
A= $self->{'search_debug'}
| |||||||||||||
| 149 | 7 | ▶ | $self->{_debug} = 2 if ($self->{'search_parse_debug'}); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 150 | } # _native_setup_search | ||||||||||||
| 151 | |||||||||||||
| 152 | |||||||||||||
| 153 | =head2 need_to_delay | ||||||||||||
| 154 | |||||||||||||
| 155 | This method tells the L<WWW::Search> controller code whether we need to | ||||||||||||
| 156 | pause and give the yahoo.com servers a breather. | ||||||||||||
| 157 | |||||||||||||
| 158 | =cut | ||||||||||||
| 159 | |||||||||||||
| 160 | sub need_to_delay | ||||||||||||
| 161 | 7 | ▶ | { | ||||||||||
|
Subroutine
need_to_delay: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 162 | # print STDERR " + this is Yahoo::need_to_delay()\n"; | ||||||||||||
| 163 | 7 | return $iMustPause; | |||||||||||
| 164 | } # need_to_delay | ||||||||||||
| 165 | |||||||||||||
| 166 | |||||||||||||
| 167 | =head2 user_agent_delay | ||||||||||||
| 168 | |||||||||||||
| 169 | This method tells the L<WWW::Search> controller code how many seconds we should pause. | ||||||||||||
| 170 | |||||||||||||
| 171 | =cut | ||||||||||||
| 172 | |||||||||||||
| 173 | sub user_agent_delay | ||||||||||||
| 174 | 2 | ▶ | { | ||||||||||
|
Subroutine
user_agent_delay: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||
| 175 | 2 | my $self = shift; | |||||||||||
| 176 | 2 | my $iSecs = int(30 + rand(30)); | |||||||||||
| 177 | 2 | ▶ | print STDERR " + sleeping $iSecs seconds, to make yahoo.com think we're NOT a robot...\n" if (0 < $self->{_debug}); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 178 | 2 | sleep($iSecs); | |||||||||||
| 179 | } # user_agent_delay | ||||||||||||
| 180 | |||||||||||||
| 181 | |||||||||||||
| 182 | =head2 preprocess_results_page | ||||||||||||
| 183 | |||||||||||||
| 184 | Clean up the Yahoo HTML before we attempt to parse it. | ||||||||||||
| 185 | |||||||||||||
| 186 | =cut | ||||||||||||
| 187 | |||||||||||||
| 188 | sub preprocess_results_page | ||||||||||||
| 189 | 3 | ▶ | { | ||||||||||
|
Subroutine
preprocess_results_page: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||||
| 190 | 3 | my $self = shift; | |||||||||||
| 191 | 3 | my $sPage = shift; | |||||||||||
| 192 | 3 | ▶ | if ($self->{_debug} == 77) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 193 | { | ||||||||||||
| 194 | # For debugging only. Print the page contents and abort. | ||||||||||||
| 195 | 0 | print STDERR $sPage; | |||||||||||
| 196 | 0 | exit 88; | |||||||||||
| 197 | } # if | ||||||||||||
| 198 | # Delete the <BASE> tag that appears BEFORE the <html> tag (because | ||||||||||||
| 199 | # it causes HTML::TreeBuilder to NOT be able to parse it!) | ||||||||||||
| 200 | 3 | $sPage =~ s!<BASE\s[^>]+>!!; | |||||||||||
| 201 | 3 | return $sPage; | |||||||||||
| 202 | } # preprocess_results_page | ||||||||||||
| 203 | |||||||||||||
| 204 | |||||||||||||
| 205 | =head2 _result_list_tags | ||||||||||||
| 206 | |||||||||||||
| 207 | Returns a list, | ||||||||||||
| 208 | which will be passed as arguments to HTML::Element::look_down() | ||||||||||||
| 209 | in order to return a list of HTML::Element which contain the query results. | ||||||||||||
| 210 | |||||||||||||
| 211 | =cut | ||||||||||||
| 212 | |||||||||||||
| 213 | sub _result_list_tags | ||||||||||||
| 214 | 3 | ▶ | { | ||||||||||
|
Subroutine
_result_list_tags: called CC=1 SCAR=0.0
| |||||||||||||
| 215 | 3 | return (_tag => 'div', | |||||||||||
| 216 | class => 'res', | ||||||||||||
| 217 | ); | ||||||||||||
| 218 | } # _result_list_tags | ||||||||||||
| 219 | |||||||||||||
| 220 | |||||||||||||
| 221 | =head2 _result_list_items | ||||||||||||
| 222 | |||||||||||||
| 223 | Given an HTML::TreeBuilder tree, | ||||||||||||
| 224 | returns a list of HTML::Element, | ||||||||||||
| 225 | which contain the query results. | ||||||||||||
| 226 | |||||||||||||
| 227 | =cut | ||||||||||||
| 228 | |||||||||||||
| 229 | sub _result_list_items | ||||||||||||
| 230 | 3 | ▶ | { | ||||||||||
|
Subroutine
_result_list_items: called CC=2 SCAR=7.0
| |||||||||||||
| 231 | 3 | my $self = shift; | |||||||||||
| 232 | 3 | ▶ | my $oTree = shift || die; | ||||||||||
|
Condition50%1/2
shift() || die()
MC/DC0%0/1
shift() || die()
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||
| 233 | 3 | my @ao = $oTree->look_down($self->_result_list_tags); | |||||||||||
| 234 | 3 | return @ao; | |||||||||||
| 235 | } # _result_list_items | ||||||||||||
| 236 | |||||||||||||
| 237 | 4 | my $WS = q{[\t\r\n\240\ ]}; | |||||||||||
| 238 | |||||||||||||
| 239 | sub _parse_tree | ||||||||||||
| 240 | 3 | ▶ | { | ||||||||||
|
Subroutine
_parse_tree: called CC=30 SCAR=59.9
| |||||||||||||
| 241 | 3 | my $self = shift; | |||||||||||
| 242 | 3 | my $oTree = shift; | |||||||||||
| 243 | 3 | ▶ | print STDERR " + ::Yahoo got a tree $oTree\n" if (2 <= $self->{_debug}); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 244 | # Every time we get a page from yahoo.com, we have to pause before | ||||||||||||
| 245 | # fetching another. | ||||||||||||
| 246 | 3 | $iMustPause++; | |||||||||||
| 247 | 3 | my $hits_found = 0; | |||||||||||
| 248 | # Only try to parse the hit count if we haven't done so already: | ||||||||||||
| 249 | 3 | ▶ | print STDERR " + start, approx_h_c is ==", $self->approximate_hit_count(), "==\n" if (2 <= $self->{_debug}); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 250 | 3 | ▶ | if ($self->approximate_hit_count() < 1) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 251 | { | ||||||||||||
| 252 | 3 | my $rh = $self->_where_to_find_count; | |||||||||||
| 253 | 3 | my @aoDIV = $oTree->look_down(%$rh); | |||||||||||
| 254 | DIV_TAG: | ||||||||||||
| 255 | 3 | foreach my $oDIV (@aoDIV) | |||||||||||
| 256 | { | ||||||||||||
| 257 | 0 | ▶ | next unless ref $oDIV; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 258 | 0 | ▶ | print STDERR " + try DIV ==", $oDIV->as_HTML if (2 <= $self->{_debug}); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 259 | 0 | my $s = $oDIV->as_text; | |||||||||||
| 260 | 0 | ▶ | print STDERR " + TEXT ==$s==\n" if (2 <= $self->{_debug}); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 261 | 0 | my $iCount = $self->_string_has_count($s); | |||||||||||
| 262 | 0 | $iCount =~ tr!,\.!!d; | |||||||||||
| 263 | 0 | ▶ | if (0 <= $iCount) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 264 | { | ||||||||||||
| 265 | 0 | $self->approximate_result_count($iCount); | |||||||||||
| 266 | 0 | last DIV_TAG; | |||||||||||
| 267 | } # if | ||||||||||||
| 268 | } # foreach DIV_TAG | ||||||||||||
| 269 | } # if | ||||||||||||
| 270 | 3 | ▶ | print STDERR " + found approx_h_c is ==", $self->approximate_hit_count(), "==\n" if (2 <= $self->{_debug}); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 271 | |||||||||||||
| 272 | 3 | my @aoLI = $self->_result_list_items($oTree); | |||||||||||
| 273 | 3 | ▶ | print STDERR " DDD aoLI has ", scalar(@aoLI), " items...\n" if (2 <= $self->{_debug}); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 274 | LI_TAG: | ||||||||||||
| 275 | 3 | foreach my $oLI (@aoLI) | |||||||||||
| 276 | { | ||||||||||||
| 277 | # Sanity check: | ||||||||||||
| 278 | 0 | ▶ | next LI_TAG unless ref($oLI); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 279 | 0 | ▶ | print STDERR " DDD found oLI is ==", $oLI->as_HTML, "==\n" if (2 <= $self->{_debug}); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 280 | 0 | my $oA = $oLI->look_down(_tag => 'a'); | |||||||||||
| 281 | 0 | ▶ | next LI_TAG unless ref($oA); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 282 | 0 | ▶ | print STDERR " DDD found oA is ==", $oA->as_HTML, "==\n" if (2 <= $self->{_debug}); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 283 | 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
| |||||||||||||
| 284 | 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')
| |||||||||||||
| 285 | 0 | ▶ | next LI_TAG if ($sURL eq ''); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 286 | 0 | ▶ | print STDERR " + raw URL is ==$sURL==\n" if (2 <= $self->{_debug}); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 287 | # Throw out various unwanted Yahoo links: | ||||||||||||
| 288 | 0 | ▶ | next LI_TAG if ($sURL =~ m!\.yahoo\.com/(about|jobseeker|preferences|search)/!); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 289 | 0 | ▶ | next LI_TAG if ($sURL =~ m!//((answers|cgi|cn|de|docs|europe|help|local|myweb\d?|search|searchmarketing|video)\.)+yahoo\.com!); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 290 | # Strip off the yahoo.com redirect part of the URL: | ||||||||||||
| 291 | 0 | $sURL =~ s!\A.*?\*-!!; | |||||||||||
| 292 | 0 | $sURL =~ s!\Ahttp%3A!http:!i; | |||||||||||
| 293 | 0 | ▶ | print STDERR " + cooked URL is ==$sURL==\n" if (2 <= $self->{_debug}); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 294 | 0 | my $hit = new WWW::SearchResult; | |||||||||||
| 295 | 0 | $hit->description(q{}); | |||||||||||
| 296 | 0 | $self->parse_details($oLI, $hit); | |||||||||||
| 297 | 0 | $hit->add_url($sURL); | |||||||||||
| 298 | 0 | $sTitle = $self->strip($sTitle); | |||||||||||
| 299 | 0 | $hit->title($sTitle); | |||||||||||
| 300 | 0 | push(@{$self->{cache}}, $hit); | |||||||||||
| 301 | 0 | $hits_found++; | |||||||||||
| 302 | } # foreach LI_TAG | ||||||||||||
| 303 | # Now try to find the "next page" link: | ||||||||||||
| 304 | 3 | my @aoA = $oTree->look_down('_tag' => 'a'); | |||||||||||
| 305 | NEXT_A: | ||||||||||||
| 306 | 3 | foreach my $oA (reverse @aoA) | |||||||||||
| 307 | { | ||||||||||||
| 308 | 246 | ▶ | next NEXT_A unless ref($oA); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 309 | 246 | my $sAhtml = $oA->as_HTML; | |||||||||||
| 310 | 246 | ▶ | printf STDERR (" + next A ==%s==\n", $sAhtml) if (2 <= $self->{_debug}); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 311 | 246 | ▶ | if ($self->_a_is_next_link($oA)) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 312 | { | ||||||||||||
| 313 | # Here is an example of a raw next URL: | ||||||||||||
| 314 | # http://rds.yahoo.com/_ylt=A0Je5ra.FlVEwsQA1RhXNyoA/SIG=13517q7d2/EXP=1146513470/**http%3a//search.yahoo.com/search%3fn=100%26vo=pokemon%26ei=UTF-8%26pstart=1%26b=101 | ||||||||||||
| 315 | # http://rds.yahoo.com/;_ylt=AutpqXFv9tv2eTXen2Mw_c1XNyoA;_ylu=X3oDMTExN2UzODg3BGNvbG8DdwRzZWMDcGFnaW5hdGlvbgR2dGlkA0RGWDJfOQ--/SIG=19e131ad9/EXP=1130207429/**http%3A%2F%2Fsearch.yahoo.com%2Fsearch%3Fn%3D100%26vo%3Dpokemon%26ei%3DUTF-8%26xargs%3D12KPjg1hVSt4GmmvmnCOObHb%255F%252Dvj0Zlpi3g5UzTYR6a9RL8nQJDqADN%255F2aP%255FdLHL9y7XrQ0JOkvqV2HOs3qODiIxkSdWH8UbKsmJS5%255FIp9DLfdaXlzsbIu0%252Djv3NcQZy8nLl2qbeONz73ZI6L5Hk57%26pstart%3D6%26b%3D101 | ||||||||||||
| 316 | 0 | my $sURL = $oA->attr('href'); | |||||||||||
| 317 | 0 | ▶ | print STDERR " + raw next URL ==$sURL==\n" if (2 <= $self->{_debug}); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 318 | # Delete Yahoo-redirect portion of URL: | ||||||||||||
| 319 | 0 | $sURL =~ s!\A.+?[-*]+(?=http)!!; | |||||||||||
| 320 | 0 | ▶ | print STDERR " + poached next URL ==$sURL==\n" if (2 <= $self->{_debug}); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 321 | 0 | $sURL = WWW::Search::unescape_query($sURL); | |||||||||||
| 322 | 0 | $self->{_next_url} = $self->absurl($self->{'_prev_url'}, $sURL); | |||||||||||
| 323 | 0 | ▶ | print STDERR " + cooked next URL ==$self->{_next_url}==\n" if (2 <= $self->{_debug}); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 324 | 0 | last NEXT_A; | |||||||||||
| 325 | } # if | ||||||||||||
| 326 | } # foreach NEXT_A | ||||||||||||
| 327 | 3 | return $hits_found; | |||||||||||
| 328 | } # _parse_tree | ||||||||||||
| 329 | |||||||||||||
| 330 | |||||||||||||
| 331 | =head2 parse_details | ||||||||||||
| 332 | |||||||||||||
| 333 | Given a (portion of an) HTML::TreeBuilder tree | ||||||||||||
| 334 | and a L<WWW::SearchResult> object, | ||||||||||||
| 335 | parses one result out of the tree and populates the SearchResult. | ||||||||||||
| 336 | |||||||||||||
| 337 | =cut | ||||||||||||
| 338 | |||||||||||||
| 339 | sub parse_details | ||||||||||||
| 340 | 0 | ▶ | { | ||||||||||
|
Subroutine
parse_details: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||||
| 341 | 0 | my $self = shift; | |||||||||||
| 342 | # Required arg1 = (part of) an HTML parse tree: | ||||||||||||
| 343 | 0 | my $oLI = shift; | |||||||||||
| 344 | # Required arg2 = a WWW::SearchResult object to fill in: | ||||||||||||
| 345 | 0 | my $hit = shift; | |||||||||||
| 346 | 0 | my $oDIV = $oLI->look_down(_tag => 'div', | |||||||||||
| 347 | class => 'abstr', | ||||||||||||
| 348 | ); | ||||||||||||
| 349 | 0 | ▶ | if (ref($oDIV)) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 350 | { | ||||||||||||
| 351 | 0 | my $sDesc = $oDIV->as_text; | |||||||||||
| 352 | 0 | $hit->description($self->strip($sDesc)); | |||||||||||
| 353 | } # if | ||||||||||||
| 354 | # Delete the useless human-readable restatement of the URL (first | ||||||||||||
| 355 | # <EM> tag we come across): | ||||||||||||
| 356 | 0 | my $oEM = $oLI->look_down(_tag => 'em'); | |||||||||||
| 357 | 0 | ▶ | if (ref($oEM)) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 358 | { | ||||||||||||
| 359 | 0 | my $sSize = ''; | |||||||||||
| 360 | 0 | ▶ | $sSize = $1 if ($oLI->as_text =~ m!(\d+[kb]?)!gx); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 361 | 0 | $hit->size($sSize); | |||||||||||
| 362 | } # if | ||||||||||||
| 363 | 0 | return; | |||||||||||
| 364 | # Delete any remaining <A> tags: | ||||||||||||
| 365 | 0 | my @aoA = $oLI->look_down(_tag => 'a'); | |||||||||||
| 366 | A_TAG: | ||||||||||||
| 367 | 0 | foreach my $oA (@aoA) | |||||||||||
| 368 | { | ||||||||||||
| 369 | 0 | $oA->detach; | |||||||||||
| 370 | 0 | $oA->delete; | |||||||||||
| 371 | } # foreach A_TAG | ||||||||||||
| 372 | 0 | $oDIV = $oLI->look_down(_tag => 'div'); | |||||||||||
| 373 | 0 | ▶ | if (ref $oDIV) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 374 | { | ||||||||||||
| 375 | 0 | $oDIV->detach; | |||||||||||
| 376 | 0 | $oDIV->delete; | |||||||||||
| 377 | } # if | ||||||||||||
| 378 | 0 | my $sDesc = $oLI->as_text; | |||||||||||
| 379 | 0 | ▶ | print STDERR " + raw sDesc is ==$sDesc==\n" if (2 <= $self->{_debug}); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 380 | # Grab stuff off the end of the description: | ||||||||||||
| 381 | 0 | ▶ | print STDERR " + cooked sDesc is ==$sDesc==\n" if (2 <= $self->{_debug}); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 382 | 0 | $hit->description($self->strip($sDesc)); | |||||||||||
| 383 | } # parse_details | ||||||||||||
| 384 | |||||||||||||
| 385 | |||||||||||||
| 386 | =head2 _where_to_find_count | ||||||||||||
| 387 | |||||||||||||
| 388 | Returns a list, | ||||||||||||
| 389 | which will be passed as arguments to HTML::Element::look_down() | ||||||||||||
| 390 | in order to return an HTML::Element | ||||||||||||
| 391 | which contains the approximate result count. | ||||||||||||
| 392 | |||||||||||||
| 393 | =cut | ||||||||||||
| 394 | |||||||||||||
| 395 | sub _where_to_find_count | ||||||||||||
| 396 | 3 | ▶ | { | ||||||||||
|
Subroutine
_where_to_find_count: called CC=1 SCAR=0.0
| |||||||||||||
| 397 | 3 | my %hash = ( | |||||||||||
| 398 | _tag => 'div', | ||||||||||||
| 399 | # 'class' => 'ygbody', | ||||||||||||
| 400 | id => 'info', | ||||||||||||
| 401 | ); | ||||||||||||
| 402 | 3 | return \%hash; | |||||||||||
| 403 | } # _where_to_find_count | ||||||||||||
| 404 | |||||||||||||
| 405 | |||||||||||||
| 406 | =head2 _string_has_count | ||||||||||||
| 407 | |||||||||||||
| 408 | Given a string, | ||||||||||||
| 409 | returns the approximate result count | ||||||||||||
| 410 | if that string contains the approximate result count. | ||||||||||||
| 411 | |||||||||||||
| 412 | =cut | ||||||||||||
| 413 | |||||||||||||
| 414 | sub _string_has_count | ||||||||||||
| 415 | 0 | ▶ | { | ||||||||||
|
Subroutine
_string_has_count: not called CC=2 SCAR=17.9
| |||||||||||||
| 416 | 0 | my $self = shift; | |||||||||||
| 417 | 0 | my $s = shift; | |||||||||||
| 418 | # print STDERR " DDD Yahoo::string_has_count($s)?\n"; | ||||||||||||
| 419 | 0 | ▶ | return $1 if ($s =~ m!\bof\s+(?:about\s+)?([,0-9]+)!i); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 420 | 0 | return -1; | |||||||||||
| 421 | } # _string_has_count | ||||||||||||
| 422 | |||||||||||||
| 423 | |||||||||||||
| 424 | =head2 _a_is_next_link | ||||||||||||
| 425 | |||||||||||||
| 426 | Given an HTML::Element, | ||||||||||||
| 427 | returns true if it seems to contain the clickable "next page" widget. | ||||||||||||
| 428 | |||||||||||||
| 429 | =cut | ||||||||||||
| 430 | |||||||||||||
| 431 | sub _a_is_next_link | ||||||||||||
| 432 | 246 | ▶ | { | ||||||||||
|
Subroutine
_a_is_next_link: called CC=5 SCAR=16.4
| |||||||||||||
| 433 | 246 | my $self = shift; | |||||||||||
| 434 | 246 | my $oA = shift; | |||||||||||
| 435 | 246 | ▶ | return 0 if (! ref $oA); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 436 | 246 | ▶ | my $sID = $oA->attr('id') || ''; | ||||||||||
|
Condition100%2/2
$oA->attr('id') || ''
MC/DC100%1/1
$oA->attr('id') || ''
$oA->attr('id')
Truth table100%2/2
A
A= $oA->attr('id')
| |||||||||||||
| 437 | 246 | ▶ | return 1 if ($sID eq 'pg-next'); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 438 | 246 | my $s = $oA->as_text; | |||||||||||
| 439 | 246 | ▶ | print STDERR " + next A as_text ==$s==\n" if (2 <= $self->{_debug}); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 440 | 246 | return ($s =~ m!\A$WS*Next$WS+>$WS*\z!i); | |||||||||||
| 441 | } # _a_is_next_link | ||||||||||||
| 442 | |||||||||||||
| 443 | |||||||||||||
| 444 | =head2 strip | ||||||||||||
| 445 | |||||||||||||
| 446 | Given a string, | ||||||||||||
| 447 | strips leading and trailing whitespace off of it. | ||||||||||||
| 448 | |||||||||||||
| 449 | =cut | ||||||||||||
| 450 | |||||||||||||
| 451 | sub strip | ||||||||||||
| 452 | 0 | ▶ | { | ||||||||||
|
Subroutine
strip: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 453 | 0 | my $self = shift; | |||||||||||
| 454 | 0 | my $s = &WWW::Search::strip_tags(shift); | |||||||||||
| 455 | 0 | $s =~ s!\A$WS+ !!x; | |||||||||||
| 456 | 0 | $s =~ s! $WS+\Z!!x; | |||||||||||
| 457 | 0 | return $s; | |||||||||||
| 458 | } # strip | ||||||||||||
| 459 | |||||||||||||
| 460 | 4 | 1; | |||||||||||
| 461 | |||||||||||||
| 462 | __END__ | ||||||||||||