| 1 | package Net::Async::WebSearch::Provider::Mojeek; | ||||||||||||
| 2 | 2 | our $VERSION = '0.003'; | |||||||||||
| 3 | # ABSTRACT: Mojeek Search API provider | ||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 2 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 2 | ▶ | use parent 'Net::Async::WebSearch::Provider'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | |||||||||||||
| 8 | 2 | ▶ | use Carp qw( croak ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 2 | ▶ | use Future; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 2 | ▶ | use JSON::MaybeXS qw( decode_json ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 2 | ▶ | use URI; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | 2 | ▶ | use HTTP::Request::Common qw( GET ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | 2 | ▶ | use Net::Async::WebSearch::Result; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | |||||||||||||
| 15 | 6 | ▶ | sub _init { | ||||||||||
|
Subroutine
_init: called CC=4 SCAR=14.2
| |||||||||||||
| 16 | 6 | my ( $self ) = @_; | |||||||||||
| 17 | # Mojeek runs its own index (not a Google reseller) and has no public/shared | ||||||||||||
| 18 | # key, so — like Brave — an api_key is mandatory. | ||||||||||||
| 19 | 6 | ▶ | croak "Mojeek provider requires 'api_key'" unless $self->{api_key}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 20 | 5 | ▶ | $self->{endpoint} ||= 'https://api.mojeek.com/search'; | ||||||||||
|
Condition50%1/2
$self->{"endpoint"} ||= "https://api.mojeek.com/search"
MC/DC0%0/1
$self->{"endpoint"} ||= "https://api.mojeek.com/search"
$self->{"endpoint"}
Truth table50%1/2
A
A= $self->{"endpoint"}
| |||||||||||||
| 21 | 5 | ▶ | $self->{name} ||= 'mojeek'; | ||||||||||
|
Condition50%1/2
$self->{"name"} ||= "mojeek"
MC/DC0%0/1
$self->{"name"} ||= "mojeek"
$self->{"name"}
Truth table50%1/2
A
A= $self->{"name"}
| |||||||||||||
| 22 | } | ||||||||||||
| 23 | |||||||||||||
| 24 | 5 | ▶ | sub endpoint { $_[0]->{endpoint} } | ||||||||||
|
Subroutine
endpoint: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 25 | 6 | ▶ | sub api_key { $_[0]->{api_key} } | ||||||||||
|
Subroutine
api_key: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 26 | |||||||||||||
| 27 | 5 | ▶ | sub search { | ||||||||||
|
Subroutine
search: called CC=6 SCAR=18.1
Pod
documented
| |||||||||||||
| 28 | 5 | my ( $self, $http, $query, $opts ) = @_; | |||||||||||
| 29 | 5 | ▶ | $opts ||= {}; | ||||||||||
|
Condition50%1/2
$opts ||= {}
MC/DC0%0/1
$opts ||= {}
$opts
Truth table50%1/2
A
A= $opts
| |||||||||||||
| 30 | 5 | ▶ | 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'}
| |||||||||||||
| 31 | |||||||||||||
| 32 | # Two Mojeek quirks live in this request: the key travels as the `api_key` | ||||||||||||
| 33 | # *query parameter* (not a header, unlike Brave), and `fmt=json` is mandatory | ||||||||||||
| 34 | # — without it Mojeek answers XML, which the JSON decode below cannot parse. | ||||||||||||
| 35 | 5 | my $uri = URI->new( $self->endpoint ); | |||||||||||
| 36 | 5 | my %q = ( | |||||||||||
| 37 | q => $query, | ||||||||||||
| 38 | t => $limit, | ||||||||||||
| 39 | fmt => 'json', | ||||||||||||
| 40 | api_key => $self->api_key, | ||||||||||||
| 41 | ); | ||||||||||||
| 42 | 5 | ▶ | $q{lb} = $opts->{language} if defined $opts->{language}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 43 | 5 | ▶ | $q{rb} = $opts->{region} if defined $opts->{region}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 44 | 5 | ▶ | $q{safe} = $opts->{safesearch} if defined $opts->{safesearch}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 45 | 5 | $uri->query_form(%q); | |||||||||||
| 46 | |||||||||||||
| 47 | 5 | my $req = GET( $uri->as_string ); | |||||||||||
| 48 | 5 | $req->header( 'User-Agent' => $self->user_agent_string ); | |||||||||||
| 49 | 5 | $req->header( 'Accept' => 'application/json' ); | |||||||||||
| 50 | |||||||||||||
| 51 | 5 | ▶ | return $http->do_request( request => $req )->then(sub { | ||||||||||
|
Subroutine
__ANON__: called CC=8 SCAR=21.5
| |||||||||||||
| 52 | 5 | my ( $resp ) = @_; | |||||||||||
| 53 | 5 | ▶ | unless ( $resp->is_success ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 54 | 1 | return Future->fail( | |||||||||||
| 55 | $self->name.": HTTP ".$resp->status_line, 'websearch', $self->name, | ||||||||||||
| 56 | ); | ||||||||||||
| 57 | } | ||||||||||||
| 58 | 4 | my $data = eval { decode_json( $resp->decoded_content ) }; | |||||||||||
| 59 | 4 | ▶ | if ( my $e = $@ ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 60 | 0 | return Future->fail( $self->name.": invalid JSON: $e", 'websearch', $self->name ); | |||||||||||
| 61 | } | ||||||||||||
| 62 | # Mojeek nests everything under a `response` container: the hit list is | ||||||||||||
| 63 | # response.results (NOT a top-level `results`), alongside response.head | ||||||||||||
| 64 | # (query metadata) and response.status (OK/ERROR). Reach through it before | ||||||||||||
| 65 | # walking the results. | ||||||||||||
| 66 | 4 | ▶ | my $response = $data->{response} || {}; | ||||||||||
|
Condition50%1/2
$data->{'response'} || {}
MC/DC0%0/1
$data->{'response'} || {}
$data->{'response'}
Truth table50%1/2
A
A= $data->{'response'}
| |||||||||||||
| 67 | 4 | my $status = $response->{status}; | |||||||||||
| 68 | 4 | my @out; | |||||||||||
| 69 | 4 | my $rank = 0; | |||||||||||
| 70 | 4 | ▶ | for my $r ( @{ $response->{results} || [] } ) { | ||||||||||
|
Condition50%1/2
$response->{'results'} || []
MC/DC0%0/1
$response->{'results'} || []
$response->{'results'}
Truth table50%1/2
A
A= $response->{'results'}
| |||||||||||||
| 71 | 8 | $rank++; | |||||||||||
| 72 | push @out, Net::Async::WebSearch::Result->new( | ||||||||||||
| 73 | url => $r->{url}, | ||||||||||||
| 74 | title => $r->{title}, | ||||||||||||
| 75 | snippet => $r->{desc}, | ||||||||||||
| 76 | 8 | ▶ | provider => $self->name, | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 77 | rank => $rank, | ||||||||||||
| 78 | raw => $r, | ||||||||||||
| 79 | extra => { | ||||||||||||
| 80 | ( defined $status ? ( status => $status ) : () ), | ||||||||||||
| 81 | }, | ||||||||||||
| 82 | ); | ||||||||||||
| 83 | 8 | ▶ | last if $rank >= $limit; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 84 | } | ||||||||||||
| 85 | 4 | return Future->done(\@out); | |||||||||||
| 86 | 5 | }); | |||||||||||
| 87 | } | ||||||||||||
| 88 | |||||||||||||
| 89 | 2 | 1; | |||||||||||
| 90 | |||||||||||||
| 91 | __END__ | ||||||||||||