| 1 | package Net::Async::WebSearch::Provider::SearxNG; | ||||||||||||||||||
| 2 | 1 | our $VERSION = '0.002'; | |||||||||||||||||
| 3 | # ABSTRACT: SearxNG/Searx JSON endpoint 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=3 SCAR=24.8
| |||||||||||||||||||
| 17 | 0 | ▶ | croak "SearxNG provider requires 'endpoint'" unless $self->{endpoint}; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 18 | 0 | $self->{endpoint} =~ s{/+$}{}; | |||||||||||||||||
| 19 | 0 | ▶ | $self->{name} ||= 'searxng'; | ||||||||||||||||
|
Condition0%0/2
$self->{"name"} ||= "searxng"
MC/DC0%0/1
$self->{"name"} ||= "searxng"
$self->{"name"}
Truth table0%0/2
A
A= $self->{"name"}
| |||||||||||||||||||
| 20 | } | ||||||||||||||||||
| 21 | |||||||||||||||||||
| 22 | 0 | ▶ | sub endpoint { $_[0]->{endpoint} } | ||||||||||||||||
|
Subroutine
endpoint: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||
| 23 | 0 | ▶ | sub api_key { $_[0]->{api_key} } | ||||||||||||||||
|
Subroutine
api_key: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||
| 24 | |||||||||||||||||||
| 25 | sub search { | ||||||||||||||||||
| 26 | 0 | ▶ | my ( $self, $http, $query, $opts ) = @_; | ||||||||||||||||
|
Subroutine
search: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||
| 27 | 0 | ▶ | $opts ||= {}; | ||||||||||||||||
|
Condition0%0/2
$opts ||= {}
MC/DC0%0/1
$opts ||= {}
$opts
Truth table0%0/2
A
A= $opts
| |||||||||||||||||||
| 28 | 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'}
| |||||||||||||||||||
| 29 | |||||||||||||||||||
| 30 | 0 | my $uri = URI->new( $self->endpoint . '/search' ); | |||||||||||||||||
| 31 | 0 | my %q = ( | |||||||||||||||||
| 32 | q => $query, | ||||||||||||||||||
| 33 | format => 'json', | ||||||||||||||||||
| 34 | ); | ||||||||||||||||||
| 35 | 0 | ▶ | $q{language} = $opts->{language} if defined $opts->{language}; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 36 | 0 | ▶ | $q{safesearch} = $opts->{safesearch} if defined $opts->{safesearch}; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 37 | 0 | ▶ | $q{categories} = $opts->{categories} if defined $opts->{categories}; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 38 | 0 | ▶ | $q{engines} = $opts->{engines} if defined $opts->{engines}; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 39 | 0 | ▶ | $q{pageno} = $opts->{pageno} if defined $opts->{pageno}; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 40 | 0 | $uri->query_form(%q); | |||||||||||||||||
| 41 | |||||||||||||||||||
| 42 | 0 | my $req = GET( $uri->as_string ); | |||||||||||||||||
| 43 | 0 | $req->header( 'User-Agent' => $self->user_agent_string ); | |||||||||||||||||
| 44 | 0 | $req->header( 'Accept' => 'application/json' ); | |||||||||||||||||
| 45 | 0 | ▶ | $req->header( 'Authorization' => 'Bearer ' . $self->api_key ) if defined $self->api_key; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 46 | |||||||||||||||||||
| 47 | return $http->do_request( request => $req )->then(sub { | ||||||||||||||||||
| 48 | 0 | ▶ | my ( $resp ) = @_; | ||||||||||||||||
|
Subroutine
__ANON__: not called CC=9 SCAR=45.0
| |||||||||||||||||||
| 49 | 0 | ▶ | unless ( $resp->is_success ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 50 | 0 | return Future->fail( | |||||||||||||||||
| 51 | $self->name.": HTTP ".$resp->status_line, 'websearch', $self->name, | ||||||||||||||||||
| 52 | ); | ||||||||||||||||||
| 53 | } | ||||||||||||||||||
| 54 | 0 | my $data = eval { decode_json( $resp->decoded_content ) }; | |||||||||||||||||
| 55 | 0 | ▶ | if ( my $e = $@ ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 56 | 0 | return Future->fail( $self->name.": invalid JSON: $e", 'websearch', $self->name ); | |||||||||||||||||
| 57 | } | ||||||||||||||||||
| 58 | 0 | my @out; | |||||||||||||||||
| 59 | 0 | my $rank = 0; | |||||||||||||||||
| 60 | 0 | ▶ | for my $r ( @{ $data->{results} || [] } ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 61 | 0 | $rank++; | |||||||||||||||||
| 62 | push @out, Net::Async::WebSearch::Result->new( | ||||||||||||||||||
| 63 | url => $r->{url}, | ||||||||||||||||||
| 64 | title => $r->{title}, | ||||||||||||||||||
| 65 | snippet => $r->{content}, | ||||||||||||||||||
| 66 | provider => $self->name, | ||||||||||||||||||
| 67 | rank => $rank, | ||||||||||||||||||
| 68 | published_at => $r->{publishedDate}, | ||||||||||||||||||
| 69 | raw => $r, | ||||||||||||||||||
| 70 | extra => { | ||||||||||||||||||
| 71 | ( defined $r->{engine} ? ( engine => $r->{engine} ) : () ), | ||||||||||||||||||
| 72 | ( defined $r->{category} ? ( category => $r->{category} ) : () ), | ||||||||||||||||||
| 73 | 0 | ▶ | ( defined $r->{thumbnail} ? ( thumbnail => $r->{thumbnail} ) : () ), | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 74 | }, | ||||||||||||||||||
| 75 | ); | ||||||||||||||||||
| 76 | 0 | ▶ | last if $rank >= $limit; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 77 | } | ||||||||||||||||||
| 78 | 0 | return Future->done(\@out); | |||||||||||||||||
| 79 | 0 | }); | |||||||||||||||||
| 80 | } | ||||||||||||||||||
| 81 | |||||||||||||||||||
| 82 | 1 | 1; | |||||||||||||||||
| 83 | |||||||||||||||||||
| 84 | __END__ | ||||||||||||||||||