| 1 | package Net::Async::WebSearch::Provider::Google; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 1 | our $VERSION = '0.002'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Google Programmable Search (CSE) JSON 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( decode_json ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use URI; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use HTTP::Request::Common qw( GET ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use Net::Async::WebSearch::Result; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | sub _init { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 0 | ▶ | my ( $self ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_init: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 0 | ▶ | croak "Google provider requires 'api_key'" unless $self->{api_key}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 0 | ▶ | croak "Google provider requires 'cx' (Programmable Search engine id)" unless $self->{cx}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 0 | ▶ | $self->{endpoint} ||= 'https://www.googleapis.com/customsearch/v1'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->{"endpoint"} ||= "https://www.googleapis.com/customsearch/v1"
MC/DC0%0/1
$self->{"endpoint"} ||= "https://www.googleapis.com/customsearch/v1"
$self->{"endpoint"}
Truth table0%0/2
A
A= $self->{"endpoint"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 0 | ▶ | $self->{name} ||= 'google'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->{"name"} ||= "google"
MC/DC0%0/1
$self->{"name"} ||= "google"
$self->{"name"}
Truth table0%0/2
A
A= $self->{"name"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 0 | ▶ | sub endpoint { $_[0]->{endpoint} } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
endpoint: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 0 | ▶ | sub api_key { $_[0]->{api_key} } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
api_key: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 0 | ▶ | sub cx { $_[0]->{cx} } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
cx: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | sub search { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 0 | ▶ | my ( $self, $http, $query, $opts ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
search: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 0 | ▶ | $opts ||= {}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$opts ||= {}
MC/DC0%0/1
$opts ||= {}
$opts
Truth table0%0/2
A
A= $opts
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 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'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 0 | my $uri = URI->new( $self->endpoint ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 0 | ▶ | my %q = ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | q => $query, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | key => $self->api_key, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | cx => $self->cx, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | num => ( $limit > 10 ? 10 : $limit ), # CSE hard-caps num to 10 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 0 | ▶ | $q{gl} = $opts->{region} if defined $opts->{region}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 0 | ▶ | $q{hl} = $opts->{language} if defined $opts->{language}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 0 | ▶ | $q{safe} = $opts->{safesearch} if defined $opts->{safesearch}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | ▶ | $q{dateRestrict} = $opts->{date_restrict} if defined $opts->{date_restrict}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 0 | $uri->query_form(%q); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | my $req = GET( $uri->as_string ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | $req->header( 'User-Agent' => $self->user_agent_string ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | $req->header( 'Accept' => 'application/json' ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | return $http->do_request( request => $req )->then(sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | ▶ | my ( $resp ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=14 SCAR=53.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | ▶ | unless ( $resp->is_success ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | return Future->fail( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | $self->name.": HTTP ".$resp->status_line, 'websearch', $self->name, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | my $data = eval { decode_json( $resp->decoded_content ) }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | ▶ | if ( my $e = $@ ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | return Future->fail( $self->name.": invalid JSON: $e", 'websearch', $self->name ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | my @out; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | my $rank = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | ▶ | for my $r ( @{ $data->{items} || [] } ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | $rank++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | ▶ | my $pagemap = $r->{pagemap} || {}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$r->{'pagemap'} || {}
MC/DC0%0/1
$r->{'pagemap'} || {}
$r->{'pagemap'}
Truth table0%0/2
A
A= $r->{'pagemap'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | ▶ | my ($metatags) = @{ $pagemap->{metatags} || [] }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | push @out, Net::Async::WebSearch::Result->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | url => $r->{link}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | title => $r->{title}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | snippet => $r->{snippet}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | provider => $self->name, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | rank => $rank, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | published_at => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | $metatags && ( $metatags->{'article:published_time'} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | // $metatags->{'og:article:published_time'} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | // $metatags->{'date'} ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | raw => $r, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | extra => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | ( defined $r->{displayLink} ? ( displayLink => $r->{displayLink} ) : () ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | ( defined $r->{mime} ? ( mime => $r->{mime} ) : () ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | ▶ | ( defined $r->{fileFormat} ? ( fileFormat => $r->{fileFormat} ) : () ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition0%0/9
$metatags && (($metatags->{'article:published_time'} // $metatags->{'og:article:published_time'}) // $metatags->{'date'})
($metatags->{"article:published_time"} // $metatags->{"og:article:published_time"}) // $metatags->{"date"}
$metatags->{"article:published_time"} // $metatags->{"og:article:published_time"}
MC/DC0%0/4
$metatags && (($metatags->{'article:published_time'} // $metatags->{'og:article:published_time'}) // $metatags->{'date'})
$metatags
$metatags->{"article:published_time"}
$metatags->{"og:article:published_time"}
$metatags->{"date"}
Truth table0%0/5
A && B // C // D
A= $metatagsB= $metatags->{"article:published_time"}C= $metatags->{"og:article:published_time"}D= $metatags->{"date"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | ▶ | last if $rank >= $limit; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | return Future->done(\@out); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||