| 1 | package Mojo::Feed::Reader; | ||||||||||||||||||||||||||||||||||||
| 2 | 9 | ▶ | use Mojo::Base -base; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||
| 4 | 9 | ▶ | use Mojo::UserAgent; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 5 | 9 | ▶ | use Mojo::Feed; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 6 | 9 | ▶ | use Mojo::File 'path'; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 7 | 9 | ▶ | use Mojo::Util 'decode', 'trim'; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 8 | 9 | ▶ | use Carp qw(carp croak); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 9 | 9 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||
| 11 | 9 | has charset => 'UTF-8'; | |||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||
| 13 | 0 | ▶ | has ua => sub { Mojo::UserAgent->new }; | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||
| 15 | 72 | ▶ | sub parse { | ||||||||||||||||||||||||||||||||||
|
Subroutine
parse: called CC=7 SCAR=19.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 16 | 72 | my ( $self, $xml, $charset ) = @_; | |||||||||||||||||||||||||||||||||||
| 17 | 72 | ▶ | return undef unless ($xml); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 18 | 72 | my ( $body, $source, $url, $file, $feed ); | |||||||||||||||||||||||||||||||||||
| 19 | 72 | ▶ | if ( $body = $self->_from_string($xml) ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||
| 20 | 7 | $feed = Mojo::Feed->new( body => $body ); | |||||||||||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||||||||||
| 22 | elsif ( $file = $self->_from_file($xml) ) { | ||||||||||||||||||||||||||||||||||||
| 23 | 32 | $feed = Mojo::Feed->new( file => $file ); | |||||||||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||||||||||
| 25 | elsif ( $url = $self->_from_url($xml) ) { | ||||||||||||||||||||||||||||||||||||
| 26 | 33 | $feed = Mojo::Feed->new( url => $url, ua => $self->ua ); | |||||||||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||||||||||
| 28 | else { | ||||||||||||||||||||||||||||||||||||
| 29 | 0 | croak "unknown argument $xml"; | |||||||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||||||||
| 31 | 72 | ▶ | $feed->charset($charset) if ($charset); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 32 | 72 | ▶ | return ( $feed->is_valid ) ? $feed : undef; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||
| 35 | 72 | ▶ | sub _from_string { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_from_string: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||
| 36 | 72 | my ( $self, $xml ) = @_; | |||||||||||||||||||||||||||||||||||
| 37 | 72 | ▶ | my $str = ( !ref $xml ) ? $xml : ( ref $xml eq 'SCALAR' ) ? $$xml : ''; | ||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||
| 38 | 72 | ▶ | return ( $str =~ /^\s*\</s ) ? $str : undef; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||
| 41 | 33 | ▶ | sub _from_url { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_from_url: called CC=4 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||
| 42 | 33 | my ( $self, $xml ) = @_; | |||||||||||||||||||||||||||||||||||
| 43 | 33 | ▶ | my $url = | ||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%1/3
builtin::blessed($xml) and $xml->isa("Mojo::URL")
MC/DC0%0/2
builtin::blessed($xml) and $xml->isa("Mojo::URL")
builtin::blessed($xml)
$xml->isa("Mojo::URL")
Truth table33%1/3
A and B
A= builtin::blessed($xml)B= $xml->isa("Mojo::URL")
| |||||||||||||||||||||||||||||||||||||
| 44 | ( blessed $xml && $xml->isa('Mojo::URL') ) ? $xml->clone() | ||||||||||||||||||||||||||||||||||||
| 45 | : ( $xml =~ /^https?\:/ ) ? Mojo::URL->new("$xml") | ||||||||||||||||||||||||||||||||||||
| 46 | : undef; | ||||||||||||||||||||||||||||||||||||
| 47 | 33 | return $url; | |||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||
| 50 | 65 | ▶ | sub _from_file { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_from_file: called CC=5 SCAR=16.3
| |||||||||||||||||||||||||||||||||||||
| 51 | 65 | my ( $self, $xml ) = @_; | |||||||||||||||||||||||||||||||||||
| 52 | 65 | ▶ | my $file = | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition66%2/3
builtin::blessed($xml) and $xml->can("slurp")
MC/DC50%1/2
builtin::blessed($xml) and $xml->can("slurp")
builtin::blessed($xml)
$xml->can("slurp")
Truth table66%2/3
A and B
A= builtin::blessed($xml)B= $xml->can("slurp")
| |||||||||||||||||||||||||||||||||||||
| 53 | ( ref $xml ) | ||||||||||||||||||||||||||||||||||||
| 54 | ? ( blessed $xml && $xml->can('slurp') ) | ||||||||||||||||||||||||||||||||||||
| 55 | ? $xml | ||||||||||||||||||||||||||||||||||||
| 56 | : undef | ||||||||||||||||||||||||||||||||||||
| 57 | : ( -r "$xml" ) ? Mojo::File->new($xml) | ||||||||||||||||||||||||||||||||||||
| 58 | : undef; | ||||||||||||||||||||||||||||||||||||
| 59 | 65 | return $file; | |||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||
| 62 | # discover - get RSS/Atom feed URL from argument. | ||||||||||||||||||||||||||||||||||||
| 63 | # Code adapted to use Mojolicious from Feed::Find by Benjamin Trott | ||||||||||||||||||||||||||||||||||||
| 64 | # Any stupid mistakes are my own | ||||||||||||||||||||||||||||||||||||
| 65 | 27 | ▶ | sub discover { | ||||||||||||||||||||||||||||||||||
|
Subroutine
discover: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 66 | 27 | my ( $self, $url ) = @_; | |||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||
| 68 | # $self->ua->max_redirects(5)->connect_timeout(30); | ||||||||||||||||||||||||||||||||||||
| 69 | return $self->ua->get_p($url) | ||||||||||||||||||||||||||||||||||||
| 70 | 0 | ▶ | ->catch( sub { my ($err) = shift; croak "Connection Error: $err" } ) | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||
| 71 | ->then( | ||||||||||||||||||||||||||||||||||||
| 72 | 27 | ▶ | sub { | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||
| 73 | 27 | my ($tx) = @_; | |||||||||||||||||||||||||||||||||||
| 74 | 27 | ▶ | if ( $tx->res->is_success && $tx->res->code == 200 ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$tx->res->is_success and $tx->res->code == 200
MC/DC50%1/2
$tx->res->is_success and $tx->res->code == 200
$tx->res->is_success
$tx->res->code == 200
Truth table66%2/3
A and B
A= $tx->res->is_successB= $tx->res->code == 200
| |||||||||||||||||||||||||||||||||||||
| 75 | 25 | my $feed = Mojo::Feed->new(url => $tx->req->url); | |||||||||||||||||||||||||||||||||||
| 76 | 25 | ▶ | return $feed->url if ($feed->is_feed_content_type($tx->res->headers->content_type)); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 77 | 21 | my @feeds = $feed->find_feed_links($tx->res); | |||||||||||||||||||||||||||||||||||
| 78 | 21 | ▶ | return @feeds if (@feeds); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 79 | 10 | $feed->body($tx->res->body); | |||||||||||||||||||||||||||||||||||
| 80 | 10 | ▶ | $feed->charset($tx->res->content->charset) if ($tx->res->content->charset); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 81 | 10 | ▶ | return $feed->url if ($feed->is_valid); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||
| 83 | 10 | return; | |||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||
| 85 | 27 | ); | |||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||
| 88 | 6 | ▶ | sub parse_opml { | ||||||||||||||||||||||||||||||||||
|
Subroutine
parse_opml: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 89 | 6 | my ( $self, $opml_file ) = @_; | |||||||||||||||||||||||||||||||||||
| 90 | 6 | ▶ | my $opml_str = decode $self->charset, | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 91 | ( ref $opml_file ) | ||||||||||||||||||||||||||||||||||||
| 92 | ? $opml_file->slurp | ||||||||||||||||||||||||||||||||||||
| 93 | : Mojo::File->new($opml_file)->slurp; | ||||||||||||||||||||||||||||||||||||
| 94 | 6 | my $d = Mojo::DOM->new->parse($opml_str); | |||||||||||||||||||||||||||||||||||
| 95 | 6 | my ( %subscriptions, %categories ); | |||||||||||||||||||||||||||||||||||
| 96 | 6 | for my $item ( $d->find(q{outline})->each ) { | |||||||||||||||||||||||||||||||||||
| 97 | 1924 | my $node = $item->attr; | |||||||||||||||||||||||||||||||||||
| 98 | 1924 | ▶ | if ( !defined $node->{xmlUrl} ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 99 | 80 | ▶ | my $cat = $node->{title} || $node->{text}; | ||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$node->{'title'} || $node->{'text'}
MC/DC0%0/2
$node->{'title'} || $node->{'text'}
$node->{'title'}
$node->{'text'}
Truth table66%2/3
A || B
A= $node->{'title'}B= $node->{'text'}
| |||||||||||||||||||||||||||||||||||||
| 100 | 80 | $categories{$cat} = | |||||||||||||||||||||||||||||||||||
| 101 | $item->children('[xmlUrl]')->map( 'attr', 'xmlUrl' ); | ||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||
| 103 | else { # file by RSS URL: | ||||||||||||||||||||||||||||||||||||
| 104 | 1844 | $subscriptions{ $node->{xmlUrl} } = $node; | |||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||
| 108 | # assign categories | ||||||||||||||||||||||||||||||||||||
| 109 | 6 | for my $cat ( keys %categories ) { | |||||||||||||||||||||||||||||||||||
| 110 | 80 | for my $rss ( $categories{$cat}->each ) { | |||||||||||||||||||||||||||||||||||
| 111 | next | ||||||||||||||||||||||||||||||||||||
| 112 | 1840 | ▶ | unless ( $subscriptions{$rss} ) | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 113 | ; # don't auto-vivify for empty "categories" | ||||||||||||||||||||||||||||||||||||
| 114 | 1840 | ▶ | $subscriptions{$rss}{'categories'} ||= []; | ||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$subscriptions{$rss}{"categories"} ||= []
MC/DC100%1/1
$subscriptions{$rss}{"categories"} ||= []
$subscriptions{$rss}{"categories"}
Truth table100%2/2
A
A= $subscriptions{$rss}{"categories"}
| |||||||||||||||||||||||||||||||||||||
| 115 | 1840 | push @{ $subscriptions{$rss}{'categories'} }, $cat; | |||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||
| 118 | 6 | return ( values %subscriptions ); | |||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||
| 121 | 9 | 1; | |||||||||||||||||||||||||||||||||||
| 122 | __END__ | ||||||||||||||||||||||||||||||||||||