| 1 | # $Id: Search.pm,v 1.15 2008/07/27 16:34:27 Martin Exp $ | ||||||||||
| 2 | |||||||||||
| 3 | =head1 NAME | ||||||||||
| 4 | |||||||||||
| 5 | WWW::Search::Search - class for searching www.search.com | ||||||||||
| 6 | |||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||
| 8 | |||||||||||
| 9 | use WWW::Search; | ||||||||||
| 10 | my $oSearch = new WWW::Search('Search'); | ||||||||||
| 11 | my $sQuery = WWW::Search::escape_query("+sushi restaurant +Columbus Ohio"); | ||||||||||
| 12 | $oSearch->native_query($sQuery); | ||||||||||
| 13 | while (my $oResult = $oSearch->next_result()) | ||||||||||
| 14 | { print $oResult->url, "\n"; } | ||||||||||
| 15 | |||||||||||
| 16 | =head1 DESCRIPTION | ||||||||||
| 17 | |||||||||||
| 18 | This class is a search.com specialization of L<WWW::Search>. It handles | ||||||||||
| 19 | making and interpreting searches at F<http://www.search.com>. | ||||||||||
| 20 | |||||||||||
| 21 | This class exports no public interface; all interaction should | ||||||||||
| 22 | be done through L<WWW::Search> objects. | ||||||||||
| 23 | |||||||||||
| 24 | =head1 PRIVATE METHODS | ||||||||||
| 25 | |||||||||||
| 26 | In order to use this module, | ||||||||||
| 27 | you do NOT need to know about these methods; | ||||||||||
| 28 | they are just part of the underlying WWW::Search mechanism. | ||||||||||
| 29 | |||||||||||
| 30 | =over | ||||||||||
| 31 | |||||||||||
| 32 | =cut | ||||||||||
| 33 | |||||||||||
| 34 | package WWW::Search::Search; | ||||||||||
| 35 | |||||||||||
| 36 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 37 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 38 | |||||||||||
| 39 | 2 | ▶ | use base 'WWW::Search'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 40 | |||||||||||
| 41 | our | ||||||||||
| 42 | 2 | $VERSION = do { my @r = (q$Revision: 1.15 $ =~ /\d+/g); sprintf "%d."."%03d" x $#r, @r }; | |||||||||
| 43 | 2 | my $MAINTAINER = 'Martin Thurn <mthurn@cpan.org>'; | |||||||||
| 44 | |||||||||||
| 45 | 2 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 46 | 2 | ▶ | use URI::Escape; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 47 | 2 | ▶ | use WWW::Search; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 48 | # We need the version with the add_sources() method: | ||||||||||
| 49 | 2 | ▶ | use WWW::SearchResult 2.078; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 50 | |||||||||||
| 51 | =item gui_query | ||||||||||
| 52 | |||||||||||
| 53 | In WWW::Search::Search, the default query is the same as the GUI query. | ||||||||||
| 54 | |||||||||||
| 55 | =cut | ||||||||||
| 56 | |||||||||||
| 57 | sub gui_query | ||||||||||
| 58 | 0 | ▶ | { | ||||||||
|
Subroutine
gui_query: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 59 | 0 | my $self = shift; | |||||||||
| 60 | 0 | return $self->native_query(@_); | |||||||||
| 61 | } # gui_query | ||||||||||
| 62 | |||||||||||
| 63 | =item native_setup_search | ||||||||||
| 64 | |||||||||||
| 65 | =cut | ||||||||||
| 66 | |||||||||||
| 67 | sub native_setup_search | ||||||||||
| 68 | 3 | ▶ | { | ||||||||
|
Subroutine
native_setup_search: called CC=8 SCAR=22.2
Pod
documented
| |||||||||||
| 69 | 3 | my ($self, $native_query, $native_options_ref) = @_; | |||||||||
| 70 | 3 | $self->{_debug} = $native_options_ref->{'search_debug'}; | |||||||||
| 71 | 3 | ▶ | $self->{_debug} = 2 if ($native_options_ref->{'search_parse_debug'}); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | 3 | ▶ | $self->{_debug} = 0 if (!defined($self->{_debug})); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 73 | |||||||||||
| 74 | # $self->{agent_e_mail} = 'mthurn@cpan.org'; | ||||||||||
| 75 | 3 | $self->user_agent('non-robot'); | |||||||||
| 76 | |||||||||||
| 77 | 3 | $self->{_next_to_retrieve} = 1; | |||||||||
| 78 | 3 | $self->{'_num_hits'} = 0; | |||||||||
| 79 | # search.com returns 10 hits per page no matter what. | ||||||||||
| 80 | 3 | $self->{'_hits_per_page'} = 10; | |||||||||
| 81 | 3 | ▶ | if (!defined($self->{_options})) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 82 | { | ||||||||||
| 83 | 3 | $self->{'search_base_url'} = 'http://www.search.com'; | |||||||||
| 84 | $self->{_options} = { | ||||||||||
| 85 | 3 | 'search_url' => $self->{'search_base_url'} .'/search', | |||||||||
| 86 | 'q' => $native_query, | ||||||||||
| 87 | }; | ||||||||||
| 88 | } # if | ||||||||||
| 89 | 3 | my $options_ref = $self->{_options}; | |||||||||
| 90 | # Copy in options which were passed in our second argument: | ||||||||||
| 91 | 3 | ▶ | if (defined($native_options_ref)) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 92 | { | ||||||||||
| 93 | 3 | foreach (keys %$native_options_ref) | |||||||||
| 94 | { | ||||||||||
| 95 | 3 | $options_ref->{$_} = $native_options_ref->{$_}; | |||||||||
| 96 | } # foreach | ||||||||||
| 97 | } # if | ||||||||||
| 98 | # Copy in options which were set by a child object: | ||||||||||
| 99 | 3 | ▶ | if (defined($self->{'_child_options'})) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 100 | { | ||||||||||
| 101 | 0 | foreach (keys %{$self->{'_child_options'}}) | |||||||||
| 102 | { | ||||||||||
| 103 | 0 | $self->{'_options'}->{$_} = $self->{'_child_options'}->{$_}; | |||||||||
| 104 | } # foreach | ||||||||||
| 105 | } # if | ||||||||||
| 106 | # Finally, figure out the url. | ||||||||||
| 107 | 3 | $self->{_next_url} = $self->{_options}{'search_url'} .'?'. $self->hash_to_cgi_string($self->{_options}); | |||||||||
| 108 | } # native_setup_search | ||||||||||
| 109 | |||||||||||
| 110 | |||||||||||
| 111 | sub _preprocess_results_page_OFF | ||||||||||
| 112 | 0 | ▶ | { | ||||||||
|
Subroutine
_preprocess_results_page_OFF: not called CC=1 SCAR=6.9
| |||||||||||
| 113 | 0 | my $self = shift; | |||||||||
| 114 | 0 | my $sPage = shift; | |||||||||
| 115 | 0 | print STDERR '='x 10, $sPage, '='x 10, "\n"; | |||||||||
| 116 | 0 | return $sPage; | |||||||||
| 117 | } # preprocess_results_page | ||||||||||
| 118 | |||||||||||
| 119 | |||||||||||
| 120 | sub _parse_tree | ||||||||||
| 121 | 3 | ▶ | { | ||||||||
|
Subroutine
_parse_tree: called CC=21 SCAR=55.5
| |||||||||||
| 122 | 3 | my $self = shift; | |||||||||
| 123 | 3 | my $oTree = shift; | |||||||||
| 124 | 3 | my $hits_found = 0; | |||||||||
| 125 | 3 | ▶ | if (! $self->approximate_result_count) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 126 | { | ||||||||||
| 127 | 3 | my $oTITLE = $oTree->look_down('_tag' => 'div', | |||||||||
| 128 | class => 'statusbar', | ||||||||||
| 129 | id => 'done', | ||||||||||
| 130 | ); | ||||||||||
| 131 | 3 | ▶ | if (ref $oTITLE) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 132 | { | ||||||||||
| 133 | 0 | my $sRC = $oTITLE->as_text; | |||||||||
| 134 | 0 | ▶ | print STDERR " + RC == $sRC\n" if 2 <= $self->{_debug}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 135 | 0 | ▶ | if ($sRC =~ m!RESULTS\s+\d+\s*-\s*\d+\s+OF\s+(?:ABOUT\s+)?([0-9,]+)!i) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 136 | { | ||||||||||
| 137 | 0 | my $sCount = $1; | |||||||||
| 138 | 0 | ▶ | print STDERR " + raw count == $sCount\n" if 2 <= $self->{_debug}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 139 | 0 | $sCount =~ s!,!!g; | |||||||||
| 140 | 0 | ▶ | print STDERR " + cooked count == $sCount\n" if 2 <= $self->{_debug}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 141 | 0 | $self->approximate_result_count($sCount); | |||||||||
| 142 | } # if number pattern matches | ||||||||||
| 143 | } # if found DIV | ||||||||||
| 144 | } # if don't have approx count yet | ||||||||||
| 145 | 3 | my $sScore = ''; | |||||||||
| 146 | 3 | my $sSize = ''; | |||||||||
| 147 | 3 | my $sDate = ''; | |||||||||
| 148 | 3 | my $oDIV = $oTree->look_down('_tag' => 'div', | |||||||||
| 149 | 'id' => 'organic', | ||||||||||
| 150 | ); | ||||||||||
| 151 | 3 | ▶ | goto SKIP_RESULTS_LIST unless ref $oDIV; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 152 | 0 | my $oUL = $oDIV->look_down('_tag' => 'ul', | |||||||||
| 153 | ); | ||||||||||
| 154 | 0 | ▶ | goto SKIP_RESULTS_LIST unless ref $oUL; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 155 | # print STDERR " + oUL is ===". $oUL->as_HTML ."===\n" if 2 <= $self->{_debug}; | ||||||||||
| 156 | # The items in this list are the web search results: | ||||||||||
| 157 | 0 | my @aoLI = $oUL->look_down(_tag => 'li'); | |||||||||
| 158 | LI_TAG: | ||||||||||
| 159 | 0 | foreach my $oLI (@aoLI) | |||||||||
| 160 | { | ||||||||||
| 161 | 0 | ▶ | next LI_TAG unless ref $oLI; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 162 | 0 | ▶ | print STDERR " + oLI is ===". $oLI->as_HTML ."===\n" if 2 <= $self->{_debug}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 163 | 0 | my $oPtitle = $oLI->look_down(_tag => 'p', | |||||||||
| 164 | class => 'title'); | ||||||||||
| 165 | 0 | ▶ | next LI_TAG unless ref $oPtitle; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 166 | 0 | my $oAtitle = $oPtitle->look_down(_tag => 'a'); | |||||||||
| 167 | 0 | ▶ | next LI_TAG unless ref $oAtitle; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 168 | 0 | my $hit = new WWW::SearchResult; | |||||||||
| 169 | 0 | my $sTitle = $oAtitle->as_text; | |||||||||
| 170 | 0 | $hit->title($sTitle); | |||||||||
| 171 | 0 | my $sURL = $oAtitle->attr('href'); | |||||||||
| 172 | 0 | ▶ | if ($sURL =~ m!,(http.+)\Z!) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 173 | { | ||||||||||
| 174 | 0 | $sURL = uri_unescape($1); | |||||||||
| 175 | } # if | ||||||||||
| 176 | 0 | $hit->add_url($sURL); | |||||||||
| 177 | 0 | my $oSPANurl = $oLI->look_down(_tag => 'p', | |||||||||
| 178 | class => 'url'); | ||||||||||
| 179 | 0 | ▶ | if (ref $oSPANurl) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 180 | { | ||||||||||
| 181 | # Parse out the source sites: | ||||||||||
| 182 | 0 | my $sSrc = $oSPANurl->as_text; | |||||||||
| 183 | 0 | ▶ | if ($sSrc =~ m/\[\s*Result\s+from\s+(.+?)\s*\]/) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 184 | { | ||||||||||
| 185 | 0 | my $sSrcs = $1; | |||||||||
| 186 | 0 | my @asSrc = split /,\s*/, $sSrcs; | |||||||||
| 187 | 0 | $hit->add_sources(@asSrc); | |||||||||
| 188 | } # if | ||||||||||
| 189 | } # if | ||||||||||
| 190 | 0 | my $oPdesc = $oLI->look_down(_tag => 'p', | |||||||||
| 191 | class => 'desc'); | ||||||||||
| 192 | 0 | ▶ | if (ref $oPdesc) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 193 | { | ||||||||||
| 194 | 0 | my $sDesc = $oLI->as_text; | |||||||||
| 195 | 0 | ▶ | print STDERR " + found desc ===$sDesc===\n" if 2 <= $self->{_debug}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 196 | 0 | $hit->description(_strip($sDesc)); | |||||||||
| 197 | } # if | ||||||||||
| 198 | 0 | push(@{$self->{cache}}, $hit); | |||||||||
| 199 | 0 | $self->{'_num_hits'}++; | |||||||||
| 200 | 0 | $hits_found++; | |||||||||
| 201 | } # foreach LI_TAG | ||||||||||
| 202 | SKIP_RESULTS_LIST: | ||||||||||
| 203 | # Find the next link, if any: | ||||||||||
| 204 | 3 | my $oLInext = $oTree->look_down('_tag' => 'li', | |||||||||
| 205 | class => 'next'); | ||||||||||
| 206 | 3 | ▶ | if (ref $oLInext) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 207 | { | ||||||||||
| 208 | 0 | my $oAnext = $oLInext->look_down(_tag => 'a'); | |||||||||
| 209 | 0 | ▶ | print STDERR " + oAnext is ===", $oAnext->as_HTML, "===\n" if 2 <= $self->{_debug}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 210 | 0 | $self->{_next_url} = $self->absurl($self->{'_prev_url'}, | |||||||||
| 211 | $oAnext->attr('href')); | ||||||||||
| 212 | } # if | ||||||||||
| 213 | SKIP_NEXT_LINK: | ||||||||||
| 214 | 3 | return $hits_found; | |||||||||
| 215 | } # _parse_tree | ||||||||||
| 216 | |||||||||||
| 217 | |||||||||||
| 218 | sub _strip | ||||||||||
| 219 | 0 | ▶ | { | ||||||||
|
Subroutine
_strip: not called CC=1 SCAR=6.9
| |||||||||||
| 220 | 0 | my $sRaw = shift; | |||||||||
| 221 | 0 | my $s = &WWW::Search::strip_tags($sRaw); | |||||||||
| 222 | # Strip leading whitespace: | ||||||||||
| 223 | 0 | $s =~ s!\A[\240\t\r\n\ ]+ !!x; | |||||||||
| 224 | # Strip trailing whitespace: | ||||||||||
| 225 | 0 | $s =~ s! [\240\t\r\n\ ]+\Z!!x; | |||||||||
| 226 | 0 | return $s; | |||||||||
| 227 | } # _strip | ||||||||||
| 228 | |||||||||||
| 229 | 2 | 1; | |||||||||
| 230 | |||||||||||
| 231 | __END__ | ||||||||||