blib/lib/URI/ParseSearchString/More.pm

filter: Statementstmt 88.7% 126 / 142 Branchbran 65.6% 21 / 32 Conditioncond 16.6% 1 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 91.3% 21 / 23 Podpod 100% 8 / 8 totaltotal 82.3% 177 / 215 CC 24 SCAR 19.6
CC
24
Cov
82%
guess
30.0
parse_search_string
9.8
parse_more
7.1
CRAP 7.1
↑ Summary
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 our $VERSION = '0.18';
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 LWP::Protocol::https ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Params::Validate qw( validate SCALAR );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Try::Tiny qw( catch try );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use URI ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use URI::Heuristic qw(uf_uristr);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use URI::QueryParam ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use WWW::Mechanize::Cached ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 4 my %search_regex = (
18 answers => [qr{(.*)}],
19 aol => [qr{(.*) - AOL Search Results}],
20 as => [qr{Starware (.*) Search Results}],
21 dogpile => [qr{(.*) - Dogpile Web Search}],
22 );
23  
24 4 my %url_regex = (
25 answers => qr{answers.yahoo.com},
26 aol => qr{aol.com/(?:aol|aolcom)/search\?encquery=},
27 as => qr{as.\w+.com/dp/search\?x=},
28 dogpile => qr{http://www.dogpile},
29 );
30  
31 # local.yahoo.com should come before Yahoo as it has different
32 # params
33 # ie. this list is for ordering some of the engines in the regex. if an
34 # engine has no reason to be at a certain point in the list, it should
35 # just be listed in the %query_lookup table
36  
37 4 my @engines = (
38 'local.google', 'maps.google',
39 'googlesyndication', 'google',
40 'local.yahoo.com', 'search.yahoo.com',
41 'shopping.yahoo.com', 'yahoo',
42 'alltheweb.com', 'errors.aol.com',
43 'sucheaol.aol.de', 'aol',
44 'ask.*', 'fastbrowsersearch.com',
45 'as.*.com', 'att.net',
46 'trustedsearch.com',
47 );
48  
49 4 my %query_lookup = (
50  
51 'abcsok.no' => ['q'],
52 'about.com' => ['terms'],
53 'alltheweb.com' => ['q'],
54 'answers.com' => ['s'],
55 'aol' => [ 'query', 'q' ],
56 'as.*.com' => ['qry'],
57 'ask.*' => ['q'],
58 'att.net' => [ 'qry', 'q' ],
59 'baidu.com' => ['bs'],
60 'bing.com' => ['q'],
61 'blingo.com' => ['q'],
62 'citysearch.com' => ['query'],
63 'clicknow.org.uk' => ['q'],
64 'clusty.com' => ['query'],
65 'comcast.net' => [ 'query', 'q' ],
66 'cuil.com' => ['q'],
67 'danielsearch.info' => ['q'],
68 'devilfinder.com' => ['q'],
69 'ebay' => ['satitle'],
70 'education.yahoo.com' => ['p'],
71 'errors.aol.com' => ['host'],
72 'excite' => ['search'],
73 'ez4search.com' => ['searchname'],
74 'fastbrowsersearch.com' => ['q'],
75 'fedstats.com' => ['s'],
76 'find.copernic.com' => ['query'],
77 'finna.is' => ['query'],
78 'googlesyndication' => [ 'q', 'ref', 'loc' ],
79 'google' => [ 'q', 'as_q' ],
80 'googel' => ['q'],
81 'hotbot.lycos.com' => ['query'],
82 'isearch.com' => ['Terms'],
83 'local.google' => [ 'q', 'near' ],
84 'local.yahoo.com' => [ 'stx', 'csz' ],
85 'looksmart.com' => ['key'],
86 'lycos' => ['query'],
87 'maps.google' => [ 'q', 'near' ],
88 'msntv.msn.com' => ['q'],
89 'munky.com' => ['term'],
90 'mysearch.com' => ['searchfor'],
91 'mywebsearch.com' => ['searchfor'],
92 'mytelus.com' => ['q'],
93 'netscape.com' => ['query'],
94 'nextag.com' => ['search'],
95 'overture.com' => ['Keywords'],
96 'pricescan.com' => ['SearchString'],
97 'reviews.search.com' => ['q'],
98 'search.com' => ['q'],
99 'searchalot.com' => ['q'],
100 'searchfusion.com' => ['t'],
101 'searchon.ca' => ['Terms'],
102 'search.cnn.com' => ['query'],
103 'search.bearshare.com' => ['q'],
104 'search.comcast.net' => ['q'],
105 'search.dmoz.org' => ['search'],
106 'search.earthlink.net' => ['q'],
107 'search.findsall.info' => ['s'],
108 'search.freeserve.com' => ['q'],
109 'search.freeze.com' => ['Keywords'],
110 'search.go.com' => ['search'],
111 qr/search\d?.incredimail.com/ => ['q'],
112 'search.juno.com' => ['query'],
113 'search.iol.ie' => ['q'],
114 'search.live.com' => ['q'],
115 'search.netzero.net' => ['query'],
116 'search.*.msn.' => ['q'],
117 'search.myway.com' => ['searchfor'],
118 'search.opera.com' => ['search'],
119 'search.rogers.com' => [ 'qf', 'qo' ],
120 'search.rr.com' => ['qs'],
121 'search.start.co.il' => ['q'],
122 'search.starware.com' => ['qry'],
123 'search.sympatico.msn.ca' => ['q'],
124 'search.sweetim.com' => ['q'],
125 'search.usatoday.com' => ['kw'],
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 543 ▶ if ( $url =~ $url_regex{$engine} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url =~ /$url_regex{$engine}/) 1 542 543
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  
224 # remove trailing slash
225 135 $domain =~ s{/\z}{};
226  
227 135 my @param_parts = ();
228 135 my %params = ();
229  
230 135 my $uri = URI->new($url);
231  
232 ENGINE:
233 135 foreach my $engine ( $self->_get_engines ) {
234  
235 5531 ▶ if ( $domain =~ /$engine/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($domain =~ /$engine/i) 135 5396 5531
236  
237 135 my @names = @{ $query_lookup{$engine} };
238  
239 135 $self->{'more'}->{'domain'} = $domain;
240 135 $self->{'more'}->{'names'} = \@names;
241  
242 135 foreach my $name (@names) {
243 170 push @param_parts, $uri->query_param($name);
244 170 $params{$name} = $uri->query_param($name);
245 }
246  
247 135 last ENGINE;
248 }
249 }
250  
251 135 my $params = join( q{ }, @param_parts );
252 135 my $orig_domain = $domain;
253 135 $domain =~ s/\/.*//g;
254 135 ▶ unless ( $domain =~ /\w/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($domain =~ /\w/) 0 135 135
255 0 $domain = $orig_domain;
256 }
257  
258 135 $self->{'more'}->{'terms'} = \@param_parts;
259 135 $self->{'more'}->{'params'} = \%params;
260  
261 135 return $params;
262 }
263  
264 0 return;
265 }
266  
267 sub blame {
268 134 ▶ my $self = shift;
Subroutine blame: called CC=1 SCAR=0.0
Pod documented
269 134 return $self->{more}->{blame};
270 }
271  
272 sub guess {
273 0 ▶ my $self = shift;
Subroutine guess: not called CC=5 SCAR=34.0
Pod documented
274 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
275  
276 0 my @guesses = ( 'q', 'query', 'searchfor' );
277  
278 0 my $uri = URI->new($url);
279 0 ▶ if ( $uri->query_params ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri->query_params) 0 0 0
280  
281 0 foreach my $guess (@guesses) {
282 0 ▶ if ( $uri->query_param($guess) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri->query_param($guess)) 0 0 0
283 0 return $uri->query_param($guess);
284 }
285 }
286 }
287  
288 0 return;
289 }
290  
291 sub set_cached {
292 3 ▶ my $self = shift;
Subroutine set_cached: called CC=2 SCAR=6.9
Pod documented
293 3 my $switch = shift;
294  
295 3 ▶ if ($switch) {
Branch: 1 branch
BranchTrueFalseTotal
if ($switch) { } 1 2 3
296 1 $self->{'__more_cached'} = 1;
297 }
298 else {
299 2 $self->{'__more_cached'} = 0;
300 }
301  
302 3 return $self->{'__more_cached'};
303 }
304  
305 sub get_cached {
306 276 ▶ my $self = shift;
Subroutine get_cached: called CC=1 SCAR=0.0
Pod documented
307  
308 276 return $self->{'__more_cached'};
309 }
310  
311 sub get_mech {
312 274 ▶ my $self = shift;
Subroutine get_mech: called CC=4 SCAR=13.9
Pod documented
313 274 my $cache = $self->get_cached;
314  
315 274 ▶ if ($cache) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cache) 2 272 274
316  
317 2 ▶ if ( !exists $self->{'__more_mech_cached'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"__more_mech_cached"}) 1 1 2
318  
319 1 my $mech = WWW::Mechanize::Cached->new();
320 1 $mech->agent('URI::ParseSearchString::More');
321 1 $self->{'__more_mech_cached'} = $mech;
322  
323 }
324  
325 2 return $self->{'__more_mech_cached'};
326  
327 }
328  
329 # return a non-caching object
330 272 ▶ if ( !exists $self->{'__more_mech'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"__more_mech"}) 2 270 272
331  
332 2 my $mech = WWW::Mechanize->new();
333 2 $mech->agent('URI::ParseSearchString::More');
334 2 $self->{'__more_mech'} = $mech;
335  
336 }
337  
338 272 return $self->{'__more_mech'};
339 }
340  
341 sub _apply_regex {
342 2 ▶ my $self = shift;
Subroutine _apply_regex: called CC=3 SCAR=11.0
343 2 my %rules = (
344 string => { type => SCALAR },
345 engine => { type => SCALAR },
346 );
347  
348 2 my %args = validate( @_, \%rules );
349  
350 2 foreach my $regex ( @{ $search_regex{ $args{'engine'} } } ) {
351 2 ▶ if ( $args{'string'} =~ $regex ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"string"} =~ /$regex/) 1 1 2
352 1 return $1;
353 }
354 }
355 1 return;
356 }
357  
358 sub _get_engines {
359 271 ▶ my $lc = List::Compare->new( \@engines, [ keys %query_lookup ] );
Subroutine _get_engines: called CC=1 SCAR=0.0
360 271 my @remaining_engines = $lc->get_complement;
361  
362 271 my @all_engines = @engines;
363 271 push @all_engines, @remaining_engines;
364  
365 271 return @all_engines;
366 }
367  
368 4 1;
369  
370 # ABSTRACT: Extract search strings from more referrers.
371  
372 __END__
↑ Summary