| 1 | package Net::Async::WebSearch::Provider::Reddit; | ||||||||||||||||||||||||||||||||||||||
| 2 | 2 | our $VERSION = '0.002'; | |||||||||||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Reddit search provider (keyless JSON endpoint) | ||||||||||||||||||||||||||||||||||||||
| 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 Future; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use JSON::MaybeXS qw( decode_json ); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use URI; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use HTTP::Request::Common qw( GET ); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use Net::Async::WebSearch::Result; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||
| 14 | sub _init { | ||||||||||||||||||||||||||||||||||||||
| 15 | 13 | ▶ | my ( $self ) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_init: called CC=7 SCAR=21.2
| |||||||||||||||||||||||||||||||||||||||
| 16 | 13 | ▶ | $self->{endpoint} ||= 'https://www.reddit.com'; | ||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"endpoint"} ||= "https://www.reddit.com"
MC/DC0%0/1
$self->{"endpoint"} ||= "https://www.reddit.com"
$self->{"endpoint"}
Truth table50%1/2
A
A= $self->{"endpoint"}
| |||||||||||||||||||||||||||||||||||||||
| 17 | 13 | $self->{endpoint} =~ s{/+$}{}; | |||||||||||||||||||||||||||||||||||||
| 18 | 13 | ▶ | $self->{name} ||= 'reddit'; | ||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"name"} ||= "reddit"
MC/DC0%0/1
$self->{"name"} ||= "reddit"
$self->{"name"}
Truth table50%1/2
A
A= $self->{"name"}
| |||||||||||||||||||||||||||||||||||||||
| 19 | 13 | ▶ | $self->{subreddit} //= undef; | ||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"subreddit"} //= undef
MC/DC0%0/1
$self->{"subreddit"} //= undef
$self->{"subreddit"}
Truth table50%1/2
A
A= $self->{"subreddit"}
| |||||||||||||||||||||||||||||||||||||||
| 20 | 13 | ▶ | $self->{sort} ||= 'relevance'; # relevance | hot | top | new | comments | ||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"sort"} ||= "relevance"
MC/DC0%0/1
$self->{"sort"} ||= "relevance"
$self->{"sort"}
Truth table50%1/2
A
A= $self->{"sort"}
| |||||||||||||||||||||||||||||||||||||||
| 21 | 13 | ▶ | $self->{time} ||= 'all'; # hour | day | week | month | year | all | ||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"time"} ||= "all"
MC/DC0%0/1
$self->{"time"} ||= "all"
$self->{"time"}
Truth table50%1/2
A
A= $self->{"time"}
| |||||||||||||||||||||||||||||||||||||||
| 22 | 13 | ▶ | $self->{link_base} ||= 'https://www.reddit.com'; | ||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"link_base"} ||= "https://www.reddit.com"
MC/DC0%0/1
$self->{"link_base"} ||= "https://www.reddit.com"
$self->{"link_base"}
Truth table50%1/2
A
A= $self->{"link_base"}
| |||||||||||||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||
| 25 | 9 | ▶ | sub endpoint { $_[0]->{endpoint} } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
endpoint: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 26 | 9 | ▶ | sub subreddit { @_ > 1 ? ($_[0]->{subreddit} = $_[1]) : $_[0]->{subreddit} } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
subreddit: called CC=2 SCAR=7.6
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 27 | 9 | ▶ | sub sort { @_ > 1 ? ($_[0]->{sort} = $_[1]) : $_[0]->{sort} } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
sort: called CC=2 SCAR=7.6
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 28 | 9 | ▶ | sub time { @_ > 1 ? ($_[0]->{time} = $_[1]) : $_[0]->{time} } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
time: called CC=2 SCAR=7.6
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 29 | 7 | ▶ | sub link_base { $_[0]->{link_base} } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
link_base: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||
| 31 | sub search { | ||||||||||||||||||||||||||||||||||||||
| 32 | 0 | ▶ | my ( $self, $http, $query, $opts ) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
search: not called CC=11 SCAR=48.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 33 | 0 | ▶ | $opts ||= {}; | ||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$opts ||= {}
MC/DC0%0/1
$opts ||= {}
$opts
Truth table0%0/2
A
A= $opts
| |||||||||||||||||||||||||||||||||||||||
| 34 | 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'}
| |||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||
| 36 | 0 | ▶ | my $sub = defined $opts->{subreddit} ? $opts->{subreddit} : $self->subreddit; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 37 | 0 | ▶ | my $path = defined $sub && length $sub ? "/r/$sub/search.json" : "/search.json"; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $sub and length $sub
MC/DC0%0/2
defined $sub and length $sub
defined $sub
length $sub
Truth table0%0/3
A and B
A= defined $subB= length $sub
| |||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||
| 39 | 0 | my $uri = URI->new( $self->endpoint . $path ); | |||||||||||||||||||||||||||||||||||||
| 40 | my %q = ( | ||||||||||||||||||||||||||||||||||||||
| 41 | q => $query, | ||||||||||||||||||||||||||||||||||||||
| 42 | limit => $limit, | ||||||||||||||||||||||||||||||||||||||
| 43 | sort => $opts->{sort} // $self->sort, | ||||||||||||||||||||||||||||||||||||||
| 44 | 0 | ▶ | t => $opts->{time} // $self->time, | ||||||||||||||||||||||||||||||||||||
|
Condition0%0/6
$opts->{"sort"} // $self->sort
$opts->{"time"} // $self->time
MC/DC0%0/2
$opts->{"sort"} // $self->sort
$opts->{"sort"}
$self->sort
MC/DC0%0/2
$opts->{"time"} // $self->time
$opts->{"time"}
$self->time
Truth table0%0/3
A // B
A= $opts->{"sort"}B= $self->sort
Truth table0%0/3
A // B
A= $opts->{"time"}B= $self->time
| |||||||||||||||||||||||||||||||||||||||
| 45 | ); | ||||||||||||||||||||||||||||||||||||||
| 46 | 0 | ▶ | $q{restrict_sr} = 1 if defined $sub && length $sub; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $sub and length $sub
MC/DC0%0/2
defined $sub and length $sub
defined $sub
length $sub
Truth table0%0/3
A and B
A= defined $subB= length $sub
| |||||||||||||||||||||||||||||||||||||||
| 47 | 0 | ▶ | $q{include_over_18} = $opts->{include_nsfw} ? 'on' : 'off'; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 48 | 0 | $uri->query_form(%q); | |||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||
| 50 | 0 | my $req = GET( $uri->as_string ); | |||||||||||||||||||||||||||||||||||||
| 51 | # Reddit is picky — generic UAs get rate-limited hard. | ||||||||||||||||||||||||||||||||||||||
| 52 | 0 | $req->header( 'User-Agent' => $self->user_agent_string ); | |||||||||||||||||||||||||||||||||||||
| 53 | 0 | $req->header( 'Accept' => 'application/json' ); | |||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||
| 55 | return $http->do_request( request => $req )->then(sub { | ||||||||||||||||||||||||||||||||||||||
| 56 | 0 | ▶ | my ( $resp ) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||
| 57 | 0 | ▶ | unless ( $resp->is_success ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 58 | 0 | return Future->fail( | |||||||||||||||||||||||||||||||||||||
| 59 | $self->name.": HTTP ".$resp->status_line, 'websearch', $self->name, | ||||||||||||||||||||||||||||||||||||||
| 60 | ); | ||||||||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||||||
| 62 | 0 | my $data = eval { decode_json( $resp->decoded_content ) }; | |||||||||||||||||||||||||||||||||||||
| 63 | 0 | ▶ | if ( my $e = $@ ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 64 | 0 | return Future->fail( $self->name.": invalid JSON: $e", 'websearch', $self->name ); | |||||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||||
| 66 | 0 | return Future->done( $self->_parse_listing($data, $limit) ); | |||||||||||||||||||||||||||||||||||||
| 67 | 0 | }); | |||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||
| 70 | sub _parse_listing { | ||||||||||||||||||||||||||||||||||||||
| 71 | 9 | ▶ | my ( $self, $data, $limit ) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse_listing: called CC=13 SCAR=29.3
| |||||||||||||||||||||||||||||||||||||||
| 72 | 9 | my @out; | |||||||||||||||||||||||||||||||||||||
| 73 | 9 | my $rank = 0; | |||||||||||||||||||||||||||||||||||||
| 74 | 9 | ▶ | for my $child ( @{ ($data->{data} || {})->{children} || [] } ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$data->{'data'} || {}
MC/DC0%0/1
$data->{'data'} || {}
$data->{'data'}
Truth table50%1/2
A
A= $data->{'data'}
| |||||||||||||||||||||||||||||||||||||||
| 75 | 7 | ▶ | my $d = $child->{data} or next; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 76 | 7 | $rank++; | |||||||||||||||||||||||||||||||||||||
| 77 | 7 | ▶ | my $permalink = $self->link_base . ( $d->{permalink} // '' ); | ||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$d->{"permalink"} // ""
MC/DC0%0/1
$d->{"permalink"} // ""
$d->{"permalink"}
Truth table50%1/2
A
A= $d->{"permalink"}
| |||||||||||||||||||||||||||||||||||||||
| 78 | 7 | ▶ | my $target = $d->{url} // $permalink; | ||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$d->{"url"} // $permalink
MC/DC0%0/2
$d->{"url"} // $permalink
$d->{"url"}
$permalink
Truth table33%1/3
A // B
A= $d->{"url"}B= $permalink
| |||||||||||||||||||||||||||||||||||||||
| 79 | 7 | my $iso; | |||||||||||||||||||||||||||||||||||||
| 80 | 7 | ▶ | if ( defined $d->{created_utc} ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 81 | 7 | my @t = gmtime( $d->{created_utc} ); | |||||||||||||||||||||||||||||||||||||
| 82 | 7 | $iso = sprintf '%04d-%02d-%02dT%02d:%02d:%02dZ', | |||||||||||||||||||||||||||||||||||||
| 83 | $t[5]+1900, $t[4]+1, $t[3], $t[2], $t[1], $t[0]; | ||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||
| 85 | push @out, Net::Async::WebSearch::Result->new( | ||||||||||||||||||||||||||||||||||||||
| 86 | url => $target, | ||||||||||||||||||||||||||||||||||||||
| 87 | title => $d->{title}, | ||||||||||||||||||||||||||||||||||||||
| 88 | snippet => ( $d->{selftext} && length $d->{selftext} | ||||||||||||||||||||||||||||||||||||||
| 89 | ? substr( $d->{selftext}, 0, 400 ) | ||||||||||||||||||||||||||||||||||||||
| 90 | : undef ), | ||||||||||||||||||||||||||||||||||||||
| 91 | provider => $self->name, | ||||||||||||||||||||||||||||||||||||||
| 92 | rank => $rank, | ||||||||||||||||||||||||||||||||||||||
| 93 | published_at => $iso, | ||||||||||||||||||||||||||||||||||||||
| 94 | nsfw => ( $d->{over_18} ? 1 : 0 ), | ||||||||||||||||||||||||||||||||||||||
| 95 | domain => $d->{domain}, | ||||||||||||||||||||||||||||||||||||||
| 96 | raw => $d, | ||||||||||||||||||||||||||||||||||||||
| 97 | extra => { | ||||||||||||||||||||||||||||||||||||||
| 98 | permalink => $permalink, | ||||||||||||||||||||||||||||||||||||||
| 99 | subreddit => $d->{subreddit}, | ||||||||||||||||||||||||||||||||||||||
| 100 | author => $d->{author}, | ||||||||||||||||||||||||||||||||||||||
| 101 | reddit_score => $d->{score}, | ||||||||||||||||||||||||||||||||||||||
| 102 | num_comments => $d->{num_comments}, | ||||||||||||||||||||||||||||||||||||||
| 103 | 7 | ▶ | ( defined $d->{created_utc} ? ( created_utc => $d->{created_utc} ) : () ), | ||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition33%1/3
$d->{"selftext"} and length $d->{"selftext"}
MC/DC0%0/2
$d->{"selftext"} and length $d->{"selftext"}
$d->{"selftext"}
length $d->{"selftext"}
Truth table33%1/3
A and B
A= $d->{"selftext"}B= length $d->{"selftext"}
| |||||||||||||||||||||||||||||||||||||||
| 104 | }, | ||||||||||||||||||||||||||||||||||||||
| 105 | ); | ||||||||||||||||||||||||||||||||||||||
| 106 | 7 | ▶ | last if $rank >= $limit; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||
| 108 | 9 | return \@out; | |||||||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||
| 111 | 2 | 1; | |||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||
| 113 | __END__ | ||||||||||||||||||||||||||||||||||||||