| 1 | package Net::Async::WebSearch::Provider::Google; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 3 | our $VERSION = '0.003'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Google Programmable Search (CSE) JSON API provider | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use parent 'Net::Async::WebSearch::Provider'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use Future; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use Future::Utils qw( repeat ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use JSON::MaybeXS qw( decode_json ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use URI; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 3 | ▶ | use HTTP::Request::Common qw( GET ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 3 | ▶ | use Net::Async::WebSearch::Result; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 8 | ▶ | sub _init { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_init: called CC=5 SCAR=17.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 8 | my ( $self ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 8 | ▶ | croak "Google provider requires 'api_key'" unless $self->{api_key}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 8 | ▶ | croak "Google provider requires 'cx' (Programmable Search engine id)" unless $self->{cx}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 8 | ▶ | $self->{endpoint} ||= 'https://www.googleapis.com/customsearch/v1'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"endpoint"} ||= "https://www.googleapis.com/customsearch/v1"
MC/DC0%0/1
$self->{"endpoint"} ||= "https://www.googleapis.com/customsearch/v1"
$self->{"endpoint"}
Truth table50%1/2
A
A= $self->{"endpoint"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 8 | ▶ | $self->{name} ||= 'google'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"name"} ||= "google"
MC/DC0%0/1
$self->{"name"} ||= "google"
$self->{"name"}
Truth table50%1/2
A
A= $self->{"name"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 24 | ▶ | sub endpoint { $_[0]->{endpoint} } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
endpoint: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 24 | ▶ | sub api_key { $_[0]->{api_key} } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
api_key: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 24 | ▶ | sub cx { $_[0]->{cx} } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
cx: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 8 | ▶ | sub search { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
search: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 8 | my ( $self, $http, $query, $opts ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 8 | ▶ | $opts ||= {}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$opts ||= {}
MC/DC0%0/1
$opts ||= {}
$opts
Truth table50%1/2
A
A= $opts
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 8 | ▶ | my $limit = $opts->{limit} || 10; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{'limit'} || 10
MC/DC100%1/1
$opts->{'limit'} || 10
$opts->{'limit'}
Truth table100%2/2
A
A= $opts->{'limit'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | # CSE serves at most 10 results per call and never past the 100th hit | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | # overall, so satisfy limit > 10 by paging over the 1-based `start` index | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | # (start=1,11,21,…) until we have `limit` results or the API runs dry. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 8 | my @out; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 8 | my $rank = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 8 | my $exhausted = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 24 | ▶ | my $paging = repeat { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=20.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 24 | my $start = @out + 1; # CSE `start` is 1-based | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 24 | my $num = $limit - @out; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 24 | ▶ | $num = 10 if $num > 10; # CSE hard-caps num to 10 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 24 | ▶ | $num = 100 - @out if @out + $num > 100; # never page past hit #100 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 24 | my $uri = URI->new( $self->endpoint ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 24 | my %q = ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | q => $query, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | key => $self->api_key, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | cx => $self->cx, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | num => $num, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | start => $start, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 24 | ▶ | $q{gl} = $opts->{region} if defined $opts->{region}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 24 | ▶ | $q{hl} = $opts->{language} if defined $opts->{language}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 24 | ▶ | $q{safe} = $opts->{safesearch} if defined $opts->{safesearch}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 24 | ▶ | $q{dateRestrict} = $opts->{date_restrict} if defined $opts->{date_restrict}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 24 | $uri->query_form(%q); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 24 | my $req = GET( $uri->as_string ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 24 | $req->header( 'User-Agent' => $self->user_agent_string ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 24 | $req->header( 'Accept' => 'application/json' ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 24 | ▶ | $http->do_request( request => $req )->then(sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=15 SCAR=28.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 24 | my ( $resp ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 24 | ▶ | unless ( $resp->is_success ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 1 | return Future->fail( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | $self->name.": HTTP ".$resp->status_line, 'websearch', $self->name, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 23 | my $data = eval { decode_json( $resp->decoded_content ) }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 23 | ▶ | if ( my $e = $@ ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | return Future->fail( $self->name.": invalid JSON: $e", 'websearch', $self->name ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 23 | ▶ | my $items = $data->{items} || []; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$data->{'items'} || []
MC/DC100%1/1
$data->{'items'} || []
$data->{'items'}
Truth table100%2/2
A
A= $data->{'items'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 23 | ▶ | $exhausted = 1 unless @$items; # no more results upstream | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 23 | for my $r ( @$items ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 190 | $rank++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 190 | ▶ | my $pagemap = $r->{pagemap} || {}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$r->{'pagemap'} || {}
MC/DC100%1/1
$r->{'pagemap'} || {}
$r->{'pagemap'}
Truth table100%2/2
A
A= $r->{'pagemap'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 190 | ▶ | my ($metatags) = @{ $pagemap->{metatags} || [] }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$pagemap->{'metatags'} || []
MC/DC100%1/1
$pagemap->{'metatags'} || []
$pagemap->{'metatags'}
Truth table100%2/2
A
A= $pagemap->{'metatags'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | push @out, Net::Async::WebSearch::Result->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | url => $r->{link}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | title => $r->{title}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | snippet => $r->{snippet}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | provider => $self->name, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | rank => $rank, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | published_at => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | $metatags && ( $metatags->{'article:published_time'} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | // $metatags->{'og:article:published_time'} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | // $metatags->{'date'} ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | raw => $r, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | extra => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | ( defined $r->{displayLink} ? ( displayLink => $r->{displayLink} ) : () ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | ( defined $r->{mime} ? ( mime => $r->{mime} ) : () ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 190 | ▶ | ( defined $r->{fileFormat} ? ( fileFormat => $r->{fileFormat} ) : () ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition44%4/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/DC25%1/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 table40%2/5
A && B // C // D
A= $metatagsB= $metatags->{"article:published_time"}C= $metatags->{"og:article:published_time"}D= $metatags->{"date"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 190 | ▶ | last if @out >= $limit; # honour limit exactly | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 23 | return Future->done; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 24 | }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 24 | ▶ | } until => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 24 | my ( $trial ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 24 | ▶ | return 1 if $trial->failure; # stop and propagate on error | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 23 | ▶ | return 1 if @out >= $limit; # limit satisfied | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 18 | ▶ | return 1 if @out >= 100; # CSE result ceiling reached | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 17 | return $exhausted; # nothing left to page | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 8 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 8 | ▶ | return $paging->then(sub { Future->done( \@out ) }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||