blib/lib/URI/ParseSearchString/More.pm

filter: Statementstmt 88.8% 127 / 143 Branchbran 65.6% 21 / 32 Conditioncond 16.6% 1 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 90.9% 20 / 22 Podpod 100% 8 / 8 totaltotal 82.3% 177 / 215 CC 24 SCAR 19.7
CC
24
Cov
82%
guess
30.0
parse_search_string
9.8
parse_more
7.1
CRAP 7.2
↑ Summary Build »
1 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package URI::ParseSearchString::More;
5 4 $URI::ParseSearchString::More::VERSION = '0.17';
6 4 ▶use base qw( URI::ParseSearchString );
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 ▶use List::Compare ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Params::Validate qw( validate SCALAR );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use URI::Heuristic qw(uf_uristr);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use URI::QueryParam ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use WWW::Mechanize::Cached ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 4 my %search_regex = (
17 answers => [qr{(.*)}],
18 aol => [qr{(.*) - AOL Search Results}],
19 as => [qr{Starware (.*) Search Results}],
20 dogpile => [qr{(.*) - Dogpile Web Search}],
21 );
22  
23 4 my %url_regex = (
24 answers => qr{answers.yahoo.com},
25 aol => qr{aol.com/(?:aol|aolcom)/search\?encquery=},
26 as => qr{as.\w+.com/dp/search\?x=},
27 dogpile => qr{http://www.dogpile},
28 );
29  
30 # local.yahoo.com should come before Yahoo as it has different
31 # params
32 # ie. this list is for ordering some of the engines in the regex. if an
33 # engine has no reason to be at a certain point in the list, it should
34 # just be listed in the %query_lookup table
35  
36 4 my @engines = (
37 'local.google', 'maps.google',
38 'googlesyndication', 'google',
39 'local.yahoo.com', 'search.yahoo.com',
40 'shopping.yahoo.com', 'yahoo',
41 'alltheweb.com', 'errors.aol.com',
42 'sucheaol.aol.de', 'aol',
43 'ask.*', 'fastbrowsersearch.com',
44 'as.*.com', 'att.net',
45 'trustedsearch.com',
46 );
47  
48 4 my %query_lookup = (
49  
50 'abcsok.no' => ['q'],
51 'about.com' => ['terms'],
52 'alltheweb.com' => ['q'],
53 'answers.com' => ['s'],
54 'aol' => [ 'query', 'q' ],
55 'as.*.com' => ['qry'],
56 'ask.*' => ['q'],
57 'att.net' => [ 'qry', 'q' ],
58 'baidu.com' => ['bs'],
59 'bing.com' => ['q'],
60 'blingo.com' => ['q'],
61 'citysearch.com' => ['query'],
62 'clicknow.org.uk' => ['q'],
63 'clusty.com' => ['query'],
64 'comcast.net' => [ 'query', 'q' ],
65 'cuil.com' => ['q'],
66 'danielsearch.info' => ['q'],
67 'devilfinder.com' => ['q'],
68 'ebay' => ['satitle'],
69 'education.yahoo.com' => ['p'],
70 'errors.aol.com' => ['host'],
71 'excite' => ['search'],
72 'ez4search.com' => ['searchname'],
73 'fastbrowsersearch.com' => ['q'],
74 'fedstats.com' => ['s'],
75 'find.copernic.com' => ['query'],
76 'finna.is' => ['query'],
77 'googlesyndication' => [ 'q', 'ref', 'loc' ],
78 'google' => [ 'q', 'as_q' ],
79 'googel' => ['q'],
80 'hotbot.lycos.com' => ['query'],
81 'isearch.com' => ['Terms'],
82 'local.google' => [ 'q', 'near' ],
83 'local.yahoo.com' => [ 'stx', 'csz' ],
84 'looksmart.com' => ['key'],
85 'lycos' => ['query'],
86 'maps.google' => [ 'q', 'near' ],
87 'msntv.msn.com' => ['q'],
88 'munky.com' => ['term'],
89 'mysearch.com' => ['searchfor'],
90 'mywebsearch.com' => ['searchfor'],
91 'mytelus.com' => ['q'],
92 'netscape.com' => ['query'],
93 'nextag.com' => ['search'],
94 'overture.com' => ['Keywords'],
95 'pricescan.com' => ['SearchString'],
96 'reviews.search.com' => ['q'],
97 'search.com' => ['q'],
98 'searchalot.com' => ['q'],
99 'searchfusion.com' => ['t'],
100 'searchon.ca' => ['Terms'],
101 'search.cnn.com' => ['query'],
102 'search.bearshare.com' => ['q'],
103 'search.comcast.net' => ['q'],
104 'search.dmoz.org' => ['search'],
105 'search.earthlink.net' => ['q'],
106 'search.findsall.info' => ['s'],
107 'search.freeserve.com' => ['q'],
108 'search.freeze.com' => ['Keywords'],
109 'search.go.com' => ['search'],
110 qr/search\d?.incredimail.com/ => ['q'],
111 'search.juno.com' => ['query'],
112 'search.iol.ie' => ['q'],
113 'search.live.com' => ['q'],
114 'search.netzero.net' => ['query'],
115 'search.*.msn.' => ['q'],
116 'search.myway.com' => ['searchfor'],
117 'search.opera.com' => ['search'],
118 'search.rogers.com' => [ 'qf', 'qo' ],
119 'search.rr.com' => ['qs'],
120 'search.start.co.il' => ['q'],
121 'search.starware.com' => ['qry'],
122 'search.sympatico.msn.ca' => ['q'],
123 'search.sweetim.com' => ['q'],
124 'search.usatoday.com' => ['kw'],
125 'search.yahoo.com' => ['va'],
126 'search.virgilio.it' => ['qs'],
127 'search.wanadoo.co.uk' => ['q'],
128 'search.yahoo.com' => [ 'q', 'va', 'p' ],
129 'searchservice.myspace.com' => ['qry'],
130 'shopping.yahoo.com' => ['p'],
131 'start.shaw.ca' => ['q'],
132 'startgoogle.startpagina.nl' => ['q'],
133 'stumbleupon.com' => ['url'],
134 'sucheaol.aol.de' => ['q'],
135 'teoma.com' => ['q'],
136 'toronto.com' => ['query'],
137 'trustedsearch.net' => ['w'],
138 'trustedsearch.com' => ['w'],
139 'yahoo' => ['p'],
140 'yandex.ru' => ['text'],
141 'youtube.com' => ['search_query'],
142 'websearch.cbc.ca' => ['query'],
143 'websearch.cs.com' => ['query'],
144 'webtv.net' => ['q'],
145 'www.bestsearchonearth.info' => ['Keywords'],
146 'www.boat.com' => ['HotKeysTopCategory'],
147 'www.factbites.com' => ['kp'],
148 'www.mweb.co.za' => ['q'],
149 'www.rr.com/html/search.cfm' => ['query'],
150 'www.wotbox.com' => ['q'],
151 );
152  
153 sub parse_search_string {
154 136 ▶ my $self = shift;
Subroutine parse_search_string: called CC=8 SCAR=22.9
Pod documented
155 136 my $url = shift;
156  
157 136 foreach my $engine ( keys %url_regex ) {
158  
159 541 ▶ if ( $url =~ $url_regex{$engine} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url =~ /$url_regex{$engine}/) 1 540 541
160  
161 # fix funky URLs
162 1 $url = uf_uristr( $url );
163  
164 1 my $mech = $self->get_mech();
165  
166 1 ▶ try { $mech->get( $url ); }
Subroutine __ANON__: called CC=1 SCAR=0.0
167 catch {
168 0 ▶ warn "Issue with url: $url";
Subroutine __ANON__: not called CC=1 SCAR=6.9
169 0 warn $_;
170 1 };
171  
172 1 ▶ if ( $mech->status && $mech->status == 403 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mech->status and $mech->status == 403) 0 1 1
Condition33%1/3
$mech->status and $mech->status == 403
!ll&&!rl&&r
010
MC/DC0%0/2
$mech->status and $mech->status == 403
$mech->status $mech->status == 403
Truth table33%1/3
A and B
A= $mech->statusB= $mech->status == 403
ABresult
0X0
100
111
173 0 warn "403 returned for $url Are you being blocked?";
174 }
175  
176 1 ▶ if ( $mech->title() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mech->title) 1 0 1
177 1 my $search_term = $self->_apply_regex(
178 string => $mech->title(),
179 engine => $engine,
180 );
181  
182 1 ▶ if ( $search_term ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($search_term) 1 0 1
183 1 $self->{'more'}->{'blame'} = __PACKAGE__;
184 1 return $search_term;
185 }
186 }
187 }
188 }
189  
190 135 my $terms = $self->parse_more( $url );
191  
192 135 ▶ if ( $terms ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($terms) 135 0 135
193 135 $self->{'more'}->{'blame'} = __PACKAGE__;
194 135 return $terms;
195 }
196  
197 # We've come up empty. Let's see what the superclass can do
198 0 $self->{'more'}->{'blame'} = 'URI::ParseSearchString';
199 0 return $self->SUPER::parse_search_string( $url, @_ );
200 }
201  
202 sub se_term {
203 1 ▶ my $self = shift;
Subroutine se_term: called CC=1 SCAR=0.0
Pod documented
204 1 return $self->parse_search_string( @_ );
205 }
206  
207 sub parse_more {
208 135 ▶ my $self = shift;
Subroutine parse_more: called CC=7 SCAR=19.6
Pod documented
209 135 my $url = shift;
210  
211 135 ▶ die "you need to supply at least one argument" unless $url;
Branch: 1 branch
BranchTrueFalseTotal
unless $url 0 135 135
212  
213 135 $self->{'more'} = undef;
214 135 $self->{'more'}->{'string'} = $url;
215  
216 135 my $regex = join " | ", $self->_get_engines;
217 135 $self->{'more'}->{'regex'} = $regex;
218 135 $self->{'more'}->{'url'} = $url;
219  
220 135 ▶ if ( $url =~ m{ ( (?: $regex ) .* ?/ ) .* ?\? (.*)\z }xms ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url =~ m[ ( (?: $regex ) .* ?/ ) .* ?\? (.*)\z ]msx) 135 0 135
221  
222 135 my $domain = $1;
223 135 my $query_string = $2;
224  
225 # for some reason, escaped quoted strings were messed up under mod_perl
226 135 $query_string =~ s{"}{"}gxms;
227 135 $query_string =~ s{&\#39;}{'}gxms;
228  
229 # remove trailing slash
230 135 $domain =~ s{/\z}{};
231  
232 135 my @param_parts = ();
233 135 my %params = ();
234 135 my @engines = $self->_get_engines;
235  
236 135 my $uri = URI->new( $url );
237  
238 ENGINE:
239 135 foreach my $engine ( @engines ) {
240  
241 5531 ▶ if ( $domain =~ /$engine/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($domain =~ /$engine/i) 135 5396 5531
242  
243 135 my @names = @{ $query_lookup{$engine} };
244  
245 135 $self->{'more'}->{'domain'} = $domain;
246 135 $self->{'more'}->{'names'} = \@names;
247  
248 135 foreach my $name ( @names ) {
249 170 push @param_parts, $uri->query_param( $name );
250 170 $params{$name} = $uri->query_param( $name );
251 }
252  
253 135 last ENGINE;
254 }
255 }
256  
257 135 my $params = join( " ", @param_parts );
258 135 my $orig_domain = $domain;
259 135 $domain =~ s/\/.*//g;
260 135 ▶ unless ( $domain =~ /\w/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($domain =~ /\w/) 0 135 135
261 0 $domain = $orig_domain;
262 }
263  
264 135 $self->{'more'}->{'terms'} = \@param_parts;
265 135 $self->{'more'}->{'params'} = \%params;
266  
267 135 return $params;
268 }
269  
270 0 return;
271 }
272  
273 sub blame {
274 134 ▶ my $self = shift;
Subroutine blame: called CC=1 SCAR=0.0
Pod documented
275 134 return $self->{more}->{blame};
276 }
277  
278 sub guess {
279 0 ▶ my $self = shift;
Subroutine guess: not called CC=5 SCAR=34.0
Pod documented
280 0 ▶ my $url = shift || $self->{'more'}->{'string'};
Condition0%0/3
shift() || $self->{'more'}{'string'}
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $self->{'more'}{'string'}
shift() $self->{'more'}{'string'}
Truth table0%0/3
A || B
A= shift()B= $self->{'more'}{'string'}
ABresult
1X1
011
000
281  
282 0 my @guesses = ( 'q', 'query', 'searchfor' );
283  
284 0 my $uri = URI->new( $url );
285 0 ▶ if ( $uri->query_params ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri->query_params) 0 0 0
286  
287 0 foreach my $guess ( @guesses ) {
288 0 ▶ if ( $uri->query_param( $guess ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri->query_param($guess)) 0 0 0
289 0 return $uri->query_param( $guess );
290 }
291 }
292 }
293  
294 0 return;
295 }
296  
297 sub set_cached {
298 3 ▶ my $self = shift;
Subroutine set_cached: called CC=2 SCAR=6.9
Pod documented
299 3 my $switch = shift;
300  
301 3 ▶ if ( $switch ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($switch) { } 1 2 3
302 1 $self->{'__more_cached'} = 1;
303 }
304 else {
305 2 $self->{'__more_cached'} = 0;
306 }
307  
308 3 return $self->{'__more_cached'};
309 }
310  
311 sub get_cached {
312 276 ▶ my $self = shift;
Subroutine get_cached: called CC=1 SCAR=0.0
Pod documented
313  
314 276 return $self->{'__more_cached'};
315 }
316  
317 sub get_mech {
318 274 ▶ my $self = shift;
Subroutine get_mech: called CC=4 SCAR=13.9
Pod documented
319 274 my $cache = $self->get_cached;
320  
321 274 ▶ if ( $cache ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cache) 2 272 274
322  
323 2 ▶ if ( !exists $self->{'__more_mech_cached'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"__more_mech_cached"}) 1 1 2
324  
325 1 my $mech = WWW::Mechanize::Cached->new();
326 1 $mech->agent( "URI::ParseSearchString::More" );
327 1 $self->{'__more_mech_cached'} = $mech;
328  
329 }
330  
331 2 return $self->{'__more_mech_cached'};
332  
333 }
334  
335 # return a non-caching object
336 272 ▶ if ( !exists $self->{'__more_mech'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"__more_mech"}) 2 270 272
337  
338 2 my $mech = WWW::Mechanize->new();
339 2 $mech->agent( "URI::ParseSearchString::More" );
340 2 $self->{'__more_mech'} = $mech;
341  
342 }
343  
344 272 return $self->{'__more_mech'};
345 }
346  
347 sub _apply_regex {
348 2 ▶ my $self = shift;
Subroutine _apply_regex: called CC=3 SCAR=11.0
349 2 my %rules = (
350 string => { type => SCALAR },
351 engine => { type => SCALAR },
352 );
353  
354 2 my %args = validate( @_, \%rules );
355  
356 2 foreach my $regex ( @{ $search_regex{ $args{'engine'} } } ) {
357 2 ▶ if ( $args{'string'} =~ $regex ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"string"} =~ /$regex/) 1 1 2
358 1 return $1;
359 }
360 }
361 1 return;
362 }
363  
364 sub _get_engines {
365 271 ▶ my $lc = List::Compare->new( \@engines, [ keys %query_lookup ] );
Subroutine _get_engines: called CC=1 SCAR=0.0
366 271 my @remaining_engines = $lc->get_complement;
367  
368 271 my @all_engines = @engines;
369 271 push @all_engines, @remaining_engines;
370  
371 271 return @all_engines;
372 }
373  
374  
375 4 1;
376  
377 # ABSTRACT: Extract search strings from more referrers.
378  
379 __END__
↑ Summary Build »