File Coverage

blib/lib/WWW/Bing/Search.pm
Criterion Covered Total %
statement 30 64 46.8
branch 0 20 0.0
condition 0 27 0.0
subroutine 6 13 46.1
pod 0 6 0.0
total 36 130 27.6


line stmt bran cond sub pod time code
1             package WWW::Bing::Search;
2             {
3             $Bing::WWW::Search::VERSION = '0.011';
4             }
5              
6 1     1   66707 use warnings;
  1         3  
  1         39  
7 1     1   7 use strict;
  1         3  
  1         42  
8 1     1   1316 use LWP::UserAgent;
  1         80038  
  1         52  
9              
10             BEGIN {
11 1     1   11 use vars qw/ $proxy $timeout $query $rawResult @results $totalCount $resultsCount $first $minPages/;
  1         3  
  1         273  
12 1     1   2 $proxy = 0;
13 1         2 $timeout = 10;
14 1         2 $query = undef;
15 1         1 $rawResult = 0;
16 1         3 @results = {};
17 1         2 $totalCount = 0;
18 1         1 $resultsCount = 0;
19 1         2 $first = 0;
20 1         839 $minPages = 1;
21             }
22              
23             sub New {
24 0     0 0   my ($name, %args) = @_;
25 0 0         $proxy = $args{'proxy'} ? delete($args{'proxy'}) : $proxy;
26 0 0         $timeout = $args{'timeout'} ? delete($args{'timeout'}) : $timeout;
27 0           return $_[0];
28             }
29              
30              
31             sub Search {
32 0 0   0 0   $query = $_[1] if (!defined($_[2]));
33 0 0         if (!defined($query)) {
34 0           my ($name, %args) = @_;
35 0 0         $query = $args{'query'} ? delete($args{'query'}) : $query;
36 0 0         $first = $args{'first'} ? delete($args{'first'}) : $first;
37 0 0         $minPages = $args{'minPages'} ? delete($args{'minPages'}) : $minPages;
38             }
39 0           $query =~ s/\s/+/;
40 0           my $pq = $query;
41 0           $query =~ s/([^A-Za-z0-9+])/sprintf("%%%02X", ord($1))/seg;
  0            
42 0           while ($minPages) {
43 0           my $ua = LWP::UserAgent->new();
44 0           $ua->timeout($timeout);
45 0 0         $ua->proxy($proxy) if ($proxy ne 0);
46 0           $rawResult = $ua->get("http://www.bing.com/search?q=$query&qs=n&filt=all&pq=$pq&sc=1-10&sp=-1&sk=&first=$first&FORM=PERE");
47 0           _parse();
48 0           $minPages--;
49 0           $first+=10;
50             }
51             }
52              
53             sub _parse {
54 0     0     my $answer = $rawResult->as_string;
55 0           while ($answer=~m/"(\w+:\/\/[^"]+)"/gi) { #"
56 0 0 0       next if (index($1,"bing.com")!=-1
      0        
      0        
      0        
      0        
      0        
      0        
      0        
      0        
57             || index($1,"w3.org")!=-1
58             || index($1,"feedback.discoverbing.com")!=-1
59             || index($1,"login.live.com")!=-1
60             || index($1,"www.microsofttranslator.com")!=-1
61             || index($1,"go.microsoft.com")!=-1
62             || index($1,"g.msn.com")!=-1
63             || index($1,"onlinehelp.microsoft.com")!=-1
64             || index($1,"cc.bingj.com")!=-1
65             || index($1,"schemas.live.com")!=-1
66             );
67 0           push @results,$1;
68 0           $resultsCount++;
69             }
70 0 0         if ($answer =~ m/id="count">.+?([0-9&#;]+)<\/span>/gi){
71 0           $totalCount = $1;
72 0           $totalCount =~ s/&#\d+;//g;
73             }
74             }
75              
76 0     0 0   sub TotalCount { return $totalCount; }
77 0     0 0   sub Count { return $resultsCount; }
78 0     0 0   sub GetResult { return shift @results; }
79 0     0 0   sub GetResults { return @results; }
80              
81             END {
82 1     1   2 $proxy = undef;
83 1         2 $timeout = undef;
84 1         2 $query = undef;
85 1         3 $rawResult = undef;
86 1         3 @results = undef;
87 1         2 $totalCount = undef;
88 1         2 $resultsCount = undef;
89 1         1 $first = undef;
90 1         6 $minPages = undef;
91             }