| 1 | package Net::Async::WebSearch::Provider::Serper; | ||||||||||||||||||||||||||||||||
| 2 | 1 | our $VERSION = '0.002'; | |||||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Serper.dev Google Search API provider | ||||||||||||||||||||||||||||||||
| 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 Carp qw( croak ); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Future; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use JSON::MaybeXS qw( encode_json decode_json ); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use HTTP::Request (); | ||||||||||||||||||||||||||||||
|
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=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||
| 16 | 0 | ▶ | croak "Serper provider requires 'api_key'" unless $self->{api_key}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 17 | 0 | ▶ | $self->{endpoint} ||= 'https://google.serper.dev/search'; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->{"endpoint"} ||= "https://google.serper.dev/search"
MC/DC0%0/1
$self->{"endpoint"} ||= "https://google.serper.dev/search"
$self->{"endpoint"}
Truth table0%0/2
A
A= $self->{"endpoint"}
| |||||||||||||||||||||||||||||||||
| 18 | 0 | ▶ | $self->{name} ||= 'serper'; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->{"name"} ||= "serper"
MC/DC0%0/1
$self->{"name"} ||= "serper"
$self->{"name"}
Truth table0%0/2
A
A= $self->{"name"}
| |||||||||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||
| 21 | 0 | ▶ | sub endpoint { $_[0]->{endpoint} } | ||||||||||||||||||||||||||||||
|
Subroutine
endpoint: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 22 | 0 | ▶ | sub api_key { $_[0]->{api_key} } | ||||||||||||||||||||||||||||||
|
Subroutine
api_key: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||
| 24 | sub search { | ||||||||||||||||||||||||||||||||
| 25 | 0 | ▶ | my ( $self, $http, $query, $opts ) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
search: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||||||
| 26 | 0 | ▶ | $opts ||= {}; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$opts ||= {}
MC/DC0%0/1
$opts ||= {}
$opts
Truth table0%0/2
A
A= $opts
| |||||||||||||||||||||||||||||||||
| 27 | 0 | ▶ | my $limit = $opts->{limit} || 10; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$opts->{'limit'} || 10
MC/DC0%0/1
$opts->{'limit'} || 10
$opts->{'limit'}
Truth table0%0/2
A
A= $opts->{'limit'}
| |||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||
| 29 | 0 | my %body = ( q => $query, num => $limit ); | |||||||||||||||||||||||||||||||
| 30 | 0 | ▶ | $body{gl} = $opts->{region} if defined $opts->{region}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 31 | 0 | ▶ | $body{hl} = $opts->{language} if defined $opts->{language}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 32 | 0 | ▶ | $body{tbs} = $opts->{tbs} if defined $opts->{tbs}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||
| 34 | 0 | my $req = HTTP::Request->new( | |||||||||||||||||||||||||||||||
| 35 | POST => $self->endpoint, | ||||||||||||||||||||||||||||||||
| 36 | [ | ||||||||||||||||||||||||||||||||
| 37 | 'X-API-KEY' => $self->api_key, | ||||||||||||||||||||||||||||||||
| 38 | 'Content-Type' => 'application/json', | ||||||||||||||||||||||||||||||||
| 39 | 'Accept' => 'application/json', | ||||||||||||||||||||||||||||||||
| 40 | 'User-Agent' => $self->user_agent_string, | ||||||||||||||||||||||||||||||||
| 41 | ], | ||||||||||||||||||||||||||||||||
| 42 | encode_json(\%body), | ||||||||||||||||||||||||||||||||
| 43 | ); | ||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||
| 45 | return $http->do_request( request => $req )->then(sub { | ||||||||||||||||||||||||||||||||
| 46 | 0 | ▶ | my ( $resp ) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=9 SCAR=45.0
| |||||||||||||||||||||||||||||||||
| 47 | 0 | ▶ | unless ( $resp->is_success ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 48 | 0 | return Future->fail( | |||||||||||||||||||||||||||||||
| 49 | $self->name.": HTTP ".$resp->status_line, 'websearch', $self->name, | ||||||||||||||||||||||||||||||||
| 50 | ); | ||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||
| 52 | 0 | my $data = eval { decode_json( $resp->decoded_content ) }; | |||||||||||||||||||||||||||||||
| 53 | 0 | ▶ | if ( my $e = $@ ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 54 | 0 | return Future->fail( $self->name.": invalid JSON: $e", 'websearch', $self->name ); | |||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||
| 56 | 0 | my @out; | |||||||||||||||||||||||||||||||
| 57 | 0 | my $rank = 0; | |||||||||||||||||||||||||||||||
| 58 | 0 | ▶ | for my $r ( @{ $data->{organic} || [] } ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 59 | 0 | $rank++; | |||||||||||||||||||||||||||||||
| 60 | push @out, Net::Async::WebSearch::Result->new( | ||||||||||||||||||||||||||||||||
| 61 | url => $r->{link}, | ||||||||||||||||||||||||||||||||
| 62 | title => $r->{title}, | ||||||||||||||||||||||||||||||||
| 63 | snippet => $r->{snippet}, | ||||||||||||||||||||||||||||||||
| 64 | provider => $self->name, | ||||||||||||||||||||||||||||||||
| 65 | rank => $r->{position} // $rank, | ||||||||||||||||||||||||||||||||
| 66 | published_at => $r->{date}, | ||||||||||||||||||||||||||||||||
| 67 | raw => $r, | ||||||||||||||||||||||||||||||||
| 68 | extra => { | ||||||||||||||||||||||||||||||||
| 69 | ( defined $r->{sitelinks} ? ( sitelinks => $r->{sitelinks} ) : () ), | ||||||||||||||||||||||||||||||||
| 70 | 0 | ▶ | ( defined $r->{imageUrl} ? ( imageUrl => $r->{imageUrl} ) : () ), | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
$r->{"position"} // $rank
MC/DC0%0/2
$r->{"position"} // $rank
$r->{"position"}
$rank
Truth table0%0/3
A // B
A= $r->{"position"}B= $rank
| |||||||||||||||||||||||||||||||||
| 71 | }, | ||||||||||||||||||||||||||||||||
| 72 | ); | ||||||||||||||||||||||||||||||||
| 73 | 0 | ▶ | last if $rank >= $limit; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||
| 75 | 0 | return Future->done(\@out); | |||||||||||||||||||||||||||||||
| 76 | 0 | }); | |||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||
| 79 | 1 | 1; | |||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||
| 81 | __END__ | ||||||||||||||||||||||||||||||||