| 1 | package WWW::Scraper::Sherlock; | ||
| 2 | |||
| 3 | =pod | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | WWW::Scraper::Sherlock - Scrapes search engines via Sherlock plugins. | ||
| 8 | |||
| 9 | |||
| 10 | =head1 SYNOPSIS | ||
| 11 | |||
| 12 | require WWW::Scraper; | ||
| 13 | $search = new WWW::Scraper('Sherlock'); | ||
| 14 | $search->sherlockPlugin($pluginURI); | ||
| 15 | |||
| 16 | # then proceed as any normal WWW::Search module. | ||
| 17 | $result = $search->next_result(); | ||
| 18 | |||
| 19 | # The result objects include additional methods specifically for Sherlock. | ||
| 20 | $result->name(); | ||
| 21 | $result->url(); | ||
| 22 | $result->relevance(); | ||
| 23 | $result->price(); | ||
| 24 | $result->avail(); | ||
| 25 | $result->email(); | ||
| 26 | $result->detail(); | ||
| 27 | $result->banner(); | ||
| 28 | $result->browserResultType(); | ||
| 29 | |||
| 30 | # Attributes of the <SEARCH> and <BROWSER> blocks of the plugin | ||
| 31 | # can be accessed via a hash in the object named 'sherlockSearchParam'. | ||
| 32 | $search->{'sherlockSearchParam'}{'name'} # name | ||
| 33 | . . . {...}{'description'} # description | ||
| 34 | . . . {...}{'method'} # method | ||
| 35 | . . . {...}{'action'} # action | ||
| 36 | . . . {...}{'routeType'} # routeType | ||
| 37 | . . . {...}{'update'} # update | ||
| 38 | . . . {...}{'updateCheckDays'} # updateCheckDays | ||
| 39 | |||
| 40 | =head1 DESCRIPTION | ||
| 41 | |||
| 42 | Performs WWW::Scraper-style searches on search engines, given a Sherlock plugin | ||
| 43 | to define the request and response | ||
| 44 | (as defined in F<http://developer.apple.com/technotes/tn/tn1141.html> | ||
| 45 | and enhanced by F<http://www.mozilla.org/projects/search/technical.html>). | ||
| 46 | |||
| 47 | The plugin is named by a URI, such as "file:yahoo.src" or "http://sherlock.mozdev.org/yahoo.src". | ||
| 48 | |||
| 49 | This version does not automatically update plugins; it ignores the 'update' and 'updateCheckDays' attributes | ||
| 50 | of the <SEARCH> block. | ||
| 51 | |||
| 52 | Getchur plugins red-hot from F<http://sherlock.mozdev.org/source/browse/sherlock/www/>. | ||
| 53 | |||
| 54 | Also ignored in this version are the <INTERPRET> attributes of 'skipLocal' (partially implemented), | ||
| 55 | 'charset', 'resultEncoding', 'resultTranslationEncoding' and 'resultTranslation'. | ||
| 56 | |||
| 57 | =head1 OPTIONS | ||
| 58 | |||
| 59 | $search->sherlockPlugin(pluginURI, { 'option' => $value }); | ||
| 60 | |||
| 61 | You may supply any of the options available to WWW::Scraper objects (which are, in turn, | ||
| 62 | WWW::Search objects). Options may also be passed to new Sherlock object via the C<sherlockPlugin()> method, just as they | ||
| 63 | would be in WWW::Search's C<next_result()>. New Sherlock options include | ||
| 64 | |||
| 65 | B<noUpdate> - boolean, do not fetch an updated plugin, even if that is called for by updateCheckDays. | ||
| 66 | |||
| 67 | =head1 EXAMPLE | ||
| 68 | |||
| 69 | This sample is a complete script that runs Sherlock against Yahoo.com. | ||
| 70 | The query is "Greeting Cards". It lists all the harvested fields to STDOUT. | ||
| 71 | Note that WWW::Scraper('Sherlock') loads WWW::Scraper::Sherlock, so you don't have to. | ||
| 72 | |||
| 73 | use WWW::Scraper; | ||
| 74 | |||
| 75 | my $scraper = new WWW::Scraper('Sherlock'); | ||
| 76 | $scraper->sherlockPlugin('http://sherlock.mozdev.org/yahoo.src'); # or 'file:Sherlock/yahoo.src'; | ||
| 77 | |||
| 78 | $scraper->native_query('Greeting Cards', {'search_debug' => 1}); | ||
| 79 | |||
| 80 | while ( my $result = $scraper->next_result() ) { | ||
| 81 | print "NAME: '".$result->name()."'\n"; | ||
| 82 | print "URL: '".$result->url()."'\n"; | ||
| 83 | print "RELEVANCE: '".$result->relevance()."'\n"; | ||
| 84 | print "PRICE: '".$result->price()."'\n"; | ||
| 85 | print "AVAIL: '".$result->avail()."'\n"; | ||
| 86 | print "EMAIL: '".$result->email()."'\n"; | ||
| 87 | print "DETAIL: '".$result->detail()."'\n"; | ||
| 88 | } | ||
| 89 | |||
| 90 | =head1 SEE ALSO | ||
| 91 | |||
| 92 | =over 4 | ||
| 93 | |||
| 94 | =item B<Apple's Introduction to Sherlock plugin development> | ||
| 95 | |||
| 96 | F<http://www.apple.com/sherlock/plugindev.html> | ||
| 97 | |||
| 98 | =item B<Sherlock Specification Technote TN1141> | ||
| 99 | |||
| 100 | F<http://developer.apple.com/technotes/tn/tn1141.html> | ||
| 101 | |||
| 102 | =item B<Mozilla Enhancements> | ||
| 103 | |||
| 104 | F<http://www.mozilla.org/projects/search/technical.html> | ||
| 105 | |||
| 106 | =item B<Mozdev Plugins Library> | ||
| 107 | |||
| 108 | F<http://sherlock.mozdev.org/source/browse/sherlock/www/> | ||
| 109 | |||
| 110 | =back | ||
| 111 | |||
| 112 | =head1 AUTHOR | ||
| 113 | |||
| 114 | C<WWW::Scraper::Sherlock> is written and maintained | ||
| 115 | by Glenn Wood, F<glenwood@alumni.caltech.com>. | ||
| 116 | |||
| 117 | =head1 COPYRIGHT | ||
| 118 | |||
| 119 | Copyright (c) 2001 Glenn Wood | ||
| 120 | All rights reserved. | ||
| 121 | |||
| 122 | This program is free software; you can redistribute it and/or | ||
| 123 | modify it under the same terms as Perl itself. | ||
| 124 | |||
| 125 | |||
| 126 | =cut | ||
| 127 | |||
| 128 | |||
| 129 | ##################################################################### | ||
| 130 | |||
| 131 | require Exporter; | ||
| 132 | @EXPORT = qw(); | ||
| 133 | @EXPORT_OK = qw(trimTags); | ||
| 134 | @ISA = qw(WWW::Scraper Exporter); | ||
| 135 | $VERSION = sprintf("%d.%02d", q$Revision: 1.0 $ =~ /(\d+)\.(\d+)/); | ||
| 136 | |||
| 137 | use Carp (); | ||
| 138 | use WWW::Scraper(qw(2.12 addURL trimTags)); | ||
| 139 | my $isNowTesting; | ||
| 140 | |||
| 141 | use HTML::Form; | ||
| 142 | use HTTP::Cookies; | ||
| 143 | use Text::ParseWords; | ||
| 144 | |||
| 145 | use strict; | ||
| 146 | |||
| 147 | sub generic_option | ||
| 148 | { | ||
| 149 | my ($option) = @_; | ||
| 150 | return 1 if $option =~ /^sherlock/; | ||
| 151 | return WWW::Scraper::generic_option($option); | ||
| 152 | } | ||
| 153 | |||
| 154 | |||
| 155 | sub sherlockPlugin { | ||
| 156 | my ($self, $sherlockPlugin, $options_ref) = @_; | ||
| 157 | |||
| 158 | $self->{'sherlockPlugin'} = $sherlockPlugin; | ||
| 159 | |||
| 160 | # Transport the parameter options into the object options. | ||
| 161 | map { $self->{_options}->{$_} = $options_ref->{$_} } keys %$options_ref | ||
| 162 | if defined($options_ref); | ||
| 163 | |||
| 164 | # Unfortunately, we can't get and parse the plugin right now, | ||
| 165 | # since we'd prefer to rely on WWW::Search for our URI access, | ||
| 166 | # and it hasn't set that up for us yet. | ||
| 167 | # See native_setup_search(), where this process will continue. | ||
| 168 | } | ||
| 169 | |||
| 170 | |||
| 171 | |||
| 172 | sub native_setup_search | ||
| 173 | { | ||
| 174 | my($self, $native_query, $native_options_ref) = @_; | ||
| 175 | $self->user_agent('user'); | ||
| 176 | $self->{_next_to_retrieve} = 0; | ||
| 177 | |||
| 178 | $self->{'scraperRequest'} = | ||
| 179 | { | ||
| 180 | 'type' => 'SHERLOCK' # This is a WWW::Search module - notify native_setup_search_NULL() of that. | ||
| 181 | # This is the basic URL on which to build the query. | ||
| 182 | ,'url' => 'http://' | ||
| 183 | # names the native input field to recieve the query string. | ||
| 184 | ,'nativeQuery' => 'query' | ||
| 185 | # specify defaults, by native field names | ||
| 186 | ,'nativeDefaults' => { 'query' => undef } | ||
| 187 | ,'fieldTranslations' => undef # This gives us a null %inputsHash, so WWW::Scraper will ignore that functionality (hopefully) | ||
| 188 | , 'cookies' => 0 # The WWW::Search module must maintain its own cookies. | ||
| 189 | }; | ||
| 190 | |||
| 191 | $self->{'sherlockPluginRes'} = $self->http_request('GET', $self->{'sherlockPlugin'}); | ||
| 192 | |||
| 193 | my $plugin = $self->{'sherlockPluginRes'}->content(); | ||
| 194 | $plugin =~ s/\r//gs; | ||
| 195 | |||
| 196 | my $interpretCount = 0; | ||
| 197 | $self->{'sherlockInterprets'} = []; | ||
| 198 | for ( "ewords('<|/?>', 1, $plugin) ) { | ||
| 199 | if ( $_ ) { | ||
| 200 | do { s/\s+$// } while ( chomp ); | ||
| 201 | } | ||
| 202 | next unless $_; | ||
| 203 | |||
| 204 | m/^search/i && do { | ||
| 205 | for ( split /\n/ ) { | ||
| 206 | do { s/\s+$// } while ( chomp ); next unless $_; | ||
| 207 | my ($x,$y) = parseString(\$_); | ||
| 208 | $self->{'sherlockSearchParam'}{$x} = $y if $x; | ||
| 209 | } | ||
| 210 | next; | ||
| 211 | }; | ||
| 212 | |||
| 213 | m/^browser/i && do { | ||
| 214 | for ( split /\n/ ) { | ||
| 215 | do { s/\s+$// } while ( chomp ); next unless $_; | ||
| 216 | my ($x,$y) = parseString(\$_); | ||
| 217 | $self->{'sherlockSearchParam'}{$x} = $y if $x; | ||
| 218 | } | ||
| 219 | next; | ||
| 220 | }; | ||
| 221 | |||
| 222 | m/^input/i && do { | ||
| 223 | # Translate ' name="NAME" value="VALUE" ' into a mini-hash-table; | ||
| 224 | s/^input\s+//i; | ||
| 225 | my %nameVal; | ||
| 226 | my $isUser = 0; | ||
| 227 | while ( my ($x,$y) = parseString(\$_) ) { | ||
| 228 | last unless $x; | ||
| 229 | $x = lc $x; | ||
| 230 | $nameVal{$x} = $y; | ||
| 231 | } | ||
| 232 | # In our context, 'user' means $native_query will to into this field. | ||
| 233 | if ( m/user/ ) { | ||
| 234 | $isUser = 1; | ||
| 235 | } | ||
| 236 | if ( $isUser ) { | ||
| 237 | $self->{'sherlockNativeQuery'} = $nameVal{'name'}; | ||
| 238 | } else { | ||
| 239 | $self->{'sherlockInput'}{$nameVal{'name'}} = $nameVal{'value'} if $nameVal{'name'}; | ||
| 240 | } | ||
| 241 | next; | ||
| 242 | }; | ||
| 243 | |||
| 244 | m/^interpret/i && do { | ||
| 245 | for ( split /\n/ ) { | ||
| 246 | do { s/\s+$// } while ( chomp ); next unless $_; | ||
| 247 | my ($x,$y) = parseString(\$_); | ||
| 248 | $self->{'sherlockInterprets'}[$interpretCount]{$x} = $y if $x; | ||
| 249 | } | ||
| 250 | $interpretCount += 1; | ||
| 251 | next; | ||
| 252 | }; | ||
| 253 | } | ||
| 254 | |||
| 255 | # Translate Sherlock's 'update' to the required action. | ||
| 256 | if ( $self->{'sherlockSearchParam'}{'update'} and not $self->{'_options'}{'noUpdate'} ) { | ||
| 257 | unless ( $self->{'isTesting'} ) { | ||
| 258 | print STDERR "Update method for Sherlock Plugin is not implemented, yet.\n"; | ||
| 259 | print STDERR "See ".$self->{'sherlockSearchParam'}{'update'}." for an update.\n"; | ||
| 260 | } | ||
| 261 | } | ||
| 262 | # Translate Sherlock's 'action' and 'method' to WWW::Search's parameters. | ||
| 263 | $self->{'_options'}{'search_url'} = $self->{'sherlockSearchParam'}{'action'}; | ||
| 264 | $self->{'_http_method'} = $self->{'sherlockSearchParam'}{'method'}; | ||
| 265 | |||
| 266 | # Translate Sherlock's 'input' name/values to our parameters list. | ||
| 267 | for ( keys %{$self->{'sherlockInput'}} ) { | ||
| 268 | $self->{'_options'}{$_} = $self->{'sherlockInput'}{$_}; | ||
| 269 | } | ||
| 270 | |||
| 271 | # Translage Sherlock's 'interpret' elements into our scraper frame format. | ||
| 272 | my @allResultList; # Handle multiple <interpret> elements. | ||
| 273 | for my $interpret ( @{$self->{'sherlockInterprets'}} ) { | ||
| 274 | my @results; | ||
| 275 | for ( qw(relevance price avail date name email) ) { | ||
| 276 | if ( $$interpret{$_.'Start'} or | ||
| 277 | $$interpret{$_.'End'} ) { | ||
| 278 | my @rslts = ( 'CALLBACK', \&resultData, | ||
| 279 | $$interpret{$_.'Start'}, | ||
| 280 | $$interpret{$_.'End'}, "$_" ); | ||
| 281 | push @results, \@rslts; | ||
| 282 | } | ||
| 283 | } | ||
| 284 | my $resultItem; | ||
| 285 | if ( $$interpret{'resultItemStart'} or | ||
| 286 | $$interpret{'resultItemEnd'} ) { | ||
| 287 | $resultItem = [ [ 'CALLBACK', \&resultItem, | ||
| 288 | $$interpret{'resultItemStart'}, | ||
| 289 | $$interpret{'resultItemEnd'}, \@results, $$interpret{'skipLocal'} ] ]; | ||
| 290 | } else | ||
| 291 | { | ||
| 292 | $resultItem = \@results; | ||
| 293 | } | ||
| 294 | $resultItem = [ [ 'HIT*', 'Sherlock', $resultItem, $$interpret{'browserResultType'} ] ]; | ||
| 295 | |||
| 296 | my $resultList; | ||
| 297 | if ( $$interpret{'resultListStart'} or | ||
| 298 | $$interpret{'resultListEnd'} ) { | ||
| 299 | $resultList = [ 'CALLBACK', \&resultList, | ||
| 300 | $$interpret{'resultListStart'}, | ||
| 301 | $$interpret{'resultListEnd'}, $resultItem ]; | ||
| 302 | } else | ||
| 303 | { | ||
| 304 | $resultList = $resultItem; | ||
| 305 | } | ||
| 306 | push @allResultList, $resultList; | ||
| 307 | } | ||
| 308 | $self->{'_options'}{'scrapeFrame'} = [ 'HTML', [ @allResultList ] ]; | ||
| 309 | # whew! | ||
| 310 | #use Data::Dumper; print Dumper($self->{'_options'}{'scrapeFrame'}); if $self->ScraperTrace('X'); | ||
| 311 | |||
| 312 | # Ok, we'll add anything other inputs the user wants to throw at the search engine, too. | ||
| 313 | my($options_ref) = $self->{_options}; | ||
| 314 | if (defined($native_options_ref)) { | ||
| 315 | # Copy in new options. | ||
| 316 | foreach (keys %$native_options_ref) { | ||
| 317 | $options_ref->{$_} = $native_options_ref->{$_}; | ||
| 318 | }; | ||
| 319 | }; | ||
| 320 | # Process the options. | ||
| 321 | # (Now in sorted order for consistency regarless of hash ordering.) | ||
| 322 | my($options) = ''; | ||
| 323 | foreach (sort keys %$options_ref) { | ||
| 324 | # printf STDERR "option: $_ is " . $options_ref->{$_} . "\n"; | ||
| 325 | next if (generic_option($_)); | ||
| 326 | $options .= $_ . '=' . $options_ref->{$_} . '&'; | ||
| 327 | }; | ||
| 328 | $self->{_debug} = $options_ref->{'search_debug'}; | ||
| 329 | $self->{_debug} = 2 if ($options_ref->{'search_parse_debug'}); | ||
| 330 | $self->{_debug} = 0 if (!defined($self->{_debug})); | ||
| 331 | |||
| 332 | $self->{'_next_url'} = $self->{'_options'}{'search_url'} .'?'. $options . | ||
| 333 | $self->{'sherlockNativeQuery'} . '=' .$native_query; | ||
| 334 | |||
| 335 | $self->SetRequest( new WWW::Scraper::Request({$self->{'scraperRequest'}->{'nativeQuery'} => $native_query}) ); | ||
| 336 | print STDERR $self->{_base_url} . "\n" if $self->ScraperTrace('U'); | ||
| 337 | } | ||
| 338 | |||
| 339 | |||
| 340 | sub testParameters { | ||
| 341 | my ($self) = @_; | ||
| 342 | |||
| 343 | if ( ref $self ) { | ||
| 344 | $self->{'isTesting'} = 1; | ||
| 345 | $self->sherlockPlugin('http://sherlock.mozdev.org/yahoo.src'); # Set our plugin for eg/test.pl | ||
| 346 | } | ||
| 347 | |||
| 348 | return { | ||
| 349 | 'SKIP' => "Sherlock.pm is not working today; many changes out on the frontier that I haven't caught up with yet" | ||
| 350 | ,'testNativeQuery' => 'Greeting Cards' | ||
| 351 | ,'expectedOnePage' => 9 | ||
| 352 | ,'expectedMultiPage' => 11 | ||
| 353 | ,'expectedBogusPage' => 0 | ||
| 354 | }; | ||
| 355 | } | ||
| 356 | |||
| 357 | sub scraperRequest { return $_[0]->{'scraperRequest'}; } | ||
| 358 | |||
| 359 | ########################################################################## | ||
| 360 | # Handles "attribute" specifications of the form: | ||
| 361 | # | ||
| 362 | # name = "string" or | ||
| 363 | # name = 'string' or | ||
| 364 | # name = word | ||
| 365 | # | ||
| 366 | # returns (name, string) | ||
| 367 | # | ||
| 368 | # (does not handle escaped quotes) | ||
| 369 | sub parseString { | ||
| 370 | $_ = ${$_[0]}; | ||
| 371 | my $w = $_[1]; | ||
| 372 | $w = '\w+' unless $w; | ||
| 373 | return ($1,$2) if s/^\s*($w)\s*=\s*"([^"]*)"\s*//i ; | ||
| 374 | return ($1,$2) if s/^\s*($w)\s*=\s*'([^']*)'\s*//i ; | ||
| 375 | return ($1,$2) if s/^\s*($w)\s*=\s*(\w+)\s*//i ; | ||
| 376 | return (undef,undef); | ||
| 377 | } | ||
| 378 | |||
| 379 | ########################################################################## | ||
| 380 | # | ||
| 381 | # CALLBACK function: resultList | ||
| 382 | # | ||
| 383 | # Find the resultList at the scaffold location; return that to Scraper.pm | ||
| 384 | # | ||
| 385 | sub resultList { | ||
| 386 | my ($self, $hit, $content, $scaffold) = @_; | ||
| 387 | my $next_scaffold = $$scaffold[4]; | ||
| 388 | |||
| 389 | my $sub_content = ''; | ||
| 390 | my $found_sub_content = 0; | ||
| 391 | if ( $$scaffold[2] and $$scaffold[3] ) { | ||
| 392 | if ( $$content =~ m-($$scaffold[2].*?$$scaffold[3])-si ) { | ||
| 393 | $sub_content = $1; | ||
| 394 | $found_sub_content = 1; | ||
| 395 | } elsif ( $$content =~ m-($$scaffold[2].*$)-si ) { | ||
| 396 | $sub_content = $1; | ||
| 397 | $found_sub_content = 1; | ||
| 398 | } | ||
| 399 | }; | ||
| 400 | # Sherlock's very loose logic requires a match even if resultListEnd can not be found, at all. | ||
| 401 | if ( $$scaffold[2] and not $found_sub_content ) { | ||
| 402 | if ( $$content =~ m-($$scaffold[2].*$)-si ) { | ||
| 403 | $sub_content = $1; | ||
| 404 | $found_sub_content = 1; | ||
| 405 | } | ||
| 406 | } | ||
| 407 | # Sherlock's very loose logic requires a match even if resultListStart can not be found, at all. | ||
| 408 | if ( $$scaffold[3] and not $found_sub_content ) { | ||
| 409 | if ( $$content =~ m-^(.*?$$scaffold[3])-si ) { | ||
| 410 | $sub_content = $1; | ||
| 411 | $found_sub_content = 1; | ||
| 412 | } | ||
| 413 | } | ||
| 414 | $next_scaffold = undef unless $found_sub_content; | ||
| 415 | |||
| 416 | $hit->{'browserResultType'} = $$scaffold[5]; | ||
| 417 | return ($sub_content, $next_scaffold); | ||
| 418 | } | ||
| 419 | |||
| 420 | ########################################################################## | ||
| 421 | # | ||
| 422 | # CALLBACK function: resultItem | ||
| 423 | # | ||
| 424 | # Find the resultItem at the scaffold location; return that to Scraper.pm | ||
| 425 | # Very similar to resultList, except that since Scraper.pm will continue | ||
| 426 | # scanning the same $content string until all items are harvested, resultItem() | ||
| 427 | # must remove each harvested item from the $content string. | ||
| 428 | # | ||
| 429 | # And finally, resultItem() harvests the "links immediately following the text pattern". | ||
| 430 | # | ||
| 431 | sub resultItem { | ||
| 432 | my ($self, $hit, $content, $scaffold, $total_hits_found, $skipLocal) = @_; | ||
| 433 | my $next_scaffold = $$scaffold[4]; | ||
| 434 | my $skip_local = $$scaffold[5]; | ||
| 435 | |||
| 436 | my $sub_content = ''; | ||
| 437 | my $found_sub_content = 0; | ||
| 438 | if ( $$scaffold[2] and $$scaffold[3] ) { | ||
| 439 | if ( $$content =~ s-($$scaffold[2].*?$$scaffold[3])--si ) { | ||
| 440 | $sub_content = $1; | ||
| 441 | $found_sub_content = 1; | ||
| 442 | } elsif ( $$content =~ s-($$scaffold[2].*$)--si ) { | ||
| 443 | $sub_content = $1; | ||
| 444 | $found_sub_content = 1; | ||
| 445 | } | ||
| 446 | }; | ||
| 447 | # Sherlock's very loose logic requires a match even if resultItemEnd can not be found, at all. | ||
| 448 | if ( $$scaffold[2] and not $found_sub_content ) { | ||
| 449 | if ( $$content =~ s-($$scaffold[2].*$)--si ) { | ||
| 450 | $sub_content = $1; | ||
| 451 | $found_sub_content = 1; | ||
| 452 | } | ||
| 453 | } | ||
| 454 | # Sherlock's very loose logic requires a match even if resultItemStart can not be found, at all. | ||
| 455 | if ( $$scaffold[3] and not $found_sub_content ) { | ||
| 456 | if ( $$content =~ s-^(.*?$$scaffold[3])--si ) { | ||
| 457 | $sub_content = $1; | ||
| 458 | $found_sub_content = 1; | ||
| 459 | } | ||
| 460 | } | ||
| 461 | if ( $found_sub_content ) { | ||
| 462 | $$total_hits_found += 1; | ||
| 463 | my $url; | ||
| 464 | if ( $sub_content =~ s-<A\s+HREF="([^"]+)"[^>]*>--si ) { | ||
| 465 | $url = $1; | ||
| 466 | } | ||
| 467 | elsif ( $sub_content =~ s-<A\s+HREF='([^']+)'[^>]*>--si ) { | ||
| 468 | $url = $1; | ||
| 469 | } | ||
| 470 | elsif ( $sub_content =~ s-<A\s+HREF='([^']+)'[^>]*>--si ) { | ||
| 471 | $url = $1; | ||
| 472 | } | ||
| 473 | if ( $skipLocal ) { | ||
| 474 | # $url = '' if $url is a local href. | ||
| 475 | }; | ||
| 476 | if ( $url ) { | ||
| 477 | $url = new URI::URL($url, $self->{_base_url}); | ||
| 478 | $url = $url->abs; | ||
| 479 | $hit->plug_url($url); | ||
| 480 | } | ||
| 481 | $hit->plug_elem('detail', $sub_content); | ||
| 482 | } else { | ||
| 483 | $next_scaffold = undef; | ||
| 484 | } | ||
| 485 | |||
| 486 | return ($sub_content, $next_scaffold); | ||
| 487 | } | ||
| 488 | |||
| 489 | |||
| 490 | ########################################################################## | ||
| 491 | # | ||
| 492 | # CALLBACK function: resultData | ||
| 493 | # | ||
| 494 | # Find the resultData at the scaffold location; add the data to the $hit, and return | ||
| 495 | # undef to Scraper.pm (this is the appropriate return for a leaf of the scaffold tree.) | ||
| 496 | # | ||
| 497 | sub resultData { | ||
| 498 | my ($self, $hit, $content, $scaffold, $total_hits_found) = @_; | ||
| 499 | my $next_scaffold = $$scaffold[4]; | ||
| 500 | |||
| 501 | my $sub_content = ''; | ||
| 502 | my $found_sub_content = 0; | ||
| 503 | if ( $$scaffold[2] and $$scaffold[3] ) { | ||
| 504 | if ( $$content =~ m-$$scaffold[2](.*)?$$scaffold[3]-si ) { | ||
| 505 | $sub_content = $1; | ||
| 506 | $found_sub_content = 1; | ||
| 507 | } elsif ( $$content =~ m-$$scaffold[2](.*)$-si ) { | ||
| 508 | $sub_content = $1; | ||
| 509 | $found_sub_content = 1; | ||
| 510 | } | ||
| 511 | }; | ||
| 512 | # Sherlock's very loose logic requires a match even if resultDataEnd can not be found, at all. | ||
| 513 | if ( $$scaffold[2] and not $found_sub_content ) { | ||
| 514 | if ( $$content =~ m-$$scaffold[2](.*)$-si ) { | ||
| 515 | $sub_content = $1; | ||
| 516 | $found_sub_content = 1; | ||
| 517 | } | ||
| 518 | } | ||
| 519 | # Sherlock's very loose logic requires a match even if resultDataStart can not be found, at all. | ||
| 520 | if ( $$scaffold[3] and not $found_sub_content ) { | ||
| 521 | if ( $$content =~ m-^(.*?)$$scaffold[3]-si ) { | ||
| 522 | $sub_content = $1; | ||
| 523 | $found_sub_content = 1; | ||
| 524 | } | ||
| 525 | } | ||
| 526 | |||
| 527 | if ( $found_sub_content) { | ||
| 528 | $hit->plug_elem($$scaffold[4], $sub_content); | ||
| 529 | $$total_hits_found = 1; | ||
| 530 | } | ||
| 531 | return (undef, undef); | ||
| 532 | } | ||
| 533 | |||
| 534 | 1; |