WebSearch/Provider/DuckDuckGo.pm

filter: Statementstmt 39.3% 26 / 66 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 61.5% 8 / 13 Podpod 100% 2 / 2 totaltotal 32.1% 36 / 112 CC 15 SCAR 32.6
CC
15
Cov
28%
_parse_html
56.0
search
42.0
_init
12.0
CRAP 26.0
« WebSearch/Provider/Reddit.pm ↑ Summary WebSearch/Result.pm »
1 package Net::Async::WebSearch::Provider::DuckDuckGo;
2 1 our $VERSION = '0.002';
3 # ABSTRACT: DuckDuckGo HTML endpoint provider (keyless)
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use parent 'Net::Async::WebSearch::Provider';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use HTTP::Request::Common qw( POST );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use HTML::TreeBuilder;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Net::Async::WebSearch::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 sub _init {
15 0 ▶ my ( $self ) = @_;
Subroutine _init: not called CC=3 SCAR=24.8
16 0 ▶ $self->{endpoint} ||= 'https://html.duckduckgo.com/html/';
Condition0%0/2
$self->{"endpoint"} ||= "https://html.duckduckgo.com/html/"
l!l
00
MC/DC0%0/1
$self->{"endpoint"} ||= "https://html.duckduckgo.com/html/"
$self->{"endpoint"}
Truth table0%0/2
A
A= $self->{"endpoint"}
Aresult
11
00
17 0 ▶ $self->{name} ||= 'duckduckgo';
Condition0%0/2
$self->{"name"} ||= "duckduckgo"
l!l
00
MC/DC0%0/1
$self->{"name"} ||= "duckduckgo"
$self->{"name"}
Truth table0%0/2
A
A= $self->{"name"}
Aresult
11
00
18 }
19  
20 0 ▶sub endpoint { $_[0]->{endpoint} }
Subroutine endpoint: not called CC=1 SCAR=6.9
Pod documented
21  
22 sub search {
23 0 ▶ my ( $self, $http, $query, $opts ) = @_;
Subroutine search: not called CC=6 SCAR=37.4
Pod documented
24 0 ▶ $opts ||= {};
Condition0%0/2
$opts ||= {}
l!l
00
MC/DC0%0/1
$opts ||= {}
$opts
Truth table0%0/2
A
A= $opts
Aresult
11
00
25 0 ▶ my $limit = $opts->{limit} || 10;
Condition0%0/2
$opts->{'limit'} || 10
l!l
00
MC/DC0%0/1
$opts->{'limit'} || 10
$opts->{'limit'}
Truth table0%0/2
A
A= $opts->{'limit'}
Aresult
11
00
26  
27 0 my %form = ( q => $query );
28 0 ▶ $form{kl} = $opts->{region} if defined $opts->{region};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"region"} 0 0 0
29 0 ▶ $form{kp} = $opts->{safesearch} if defined $opts->{safesearch};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"safesearch"} 0 0 0
30  
31 0 my $req = POST( $self->endpoint, [ %form ] );
32 0 $req->header( 'User-Agent' => $self->user_agent_string );
33 0 $req->header( 'Accept' => 'text/html' );
34 0 ▶ $req->header( 'Accept-Language' => $opts->{language} // 'en-US,en;q=0.5' );
Condition0%0/2
$opts->{"language"} // "en-US,en;q=0.5"
l!l
00
MC/DC0%0/1
$opts->{"language"} // "en-US,en;q=0.5"
$opts->{"language"}
Truth table0%0/2
A
A= $opts->{"language"}
Aresult
11
00
35  
36 return $http->do_request( request => $req )->then(sub {
37 0 ▶ my ( $resp ) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
38 0 ▶ unless ( $resp->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($resp->is_success) 0 0 0
39 0 return Future->fail(
40 "duckduckgo: HTTP ".$resp->status_line, 'websearch', $self->name,
41 );
42 }
43 0 my $results = $self->_parse_html( $resp->decoded_content, $limit );
44 0 return Future->done($results);
45 0 });
46 }
47  
48 sub _parse_html {
49 0 ▶ my ( $self, $html, $limit ) = @_;
Subroutine _parse_html: not called CC=7 SCAR=40.3
50 0 my @out;
51 0 my $tree = HTML::TreeBuilder->new_from_content( $html );
52 0 my @blocks = $tree->look_down( _tag => 'div', class => qr/\bresult\b/ );
53 0 my $rank = 0;
54 0 for my $b (@blocks) {
55 0 my $a = $b->look_down( _tag => 'a', class => qr/\bresult__a\b/ );
56 0 ▶ next unless $a;
Branch: 1 branch
BranchTrueFalseTotal
unless $a 0 0 0
57 0 ▶ my $href = $a->attr('href') or next;
Branch: 1 branch
BranchTrueFalseTotal
my $href = $a->attr("href") or next 0 0 0
58 # DDG wraps real URL in //duckduckgo.com/l/?uddg=...
59 0 ▶ if ( $href =~ m{[?&]uddg=([^&]+)} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($href =~ /[?&]uddg=([^&]+)/) 0 0 0
60 0 require URI::Escape;
61 0 $href = URI::Escape::uri_unescape($1);
62 }
63 0 my $title = $a->as_trimmed_text;
64 0 my $sn = $b->look_down( _tag => 'a', class => qr/\bresult__snippet\b/ );
65 0 ▶ my $snippet = $sn ? $sn->as_trimmed_text : undef;
Branch: 1 branch
BranchTrueFalseTotal
$sn ? : 0 0 0
66 0 $rank++;
67 0 push @out, Net::Async::WebSearch::Result->new(
68 url => $href,
69 title => $title,
70 snippet => $snippet,
71 provider => $self->name,
72 rank => $rank,
73 );
74 0 ▶ last if $rank >= $limit;
Branch: 1 branch
BranchTrueFalseTotal
if $rank >= $limit 0 0 0
75 }
76 0 $tree->delete;
77 0 return \@out;
78 }
79  
80 1 1;
81  
82 __END__
« WebSearch/Provider/Reddit.pm ↑ Summary WebSearch/Result.pm »