line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::FeedReader; |
2
|
1
|
|
|
1
|
|
623530
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
1
|
|
|
|
|
8
|
|
|
1
|
|
|
|
|
6
|
|
3
|
|
|
|
|
|
|
|
4
|
1
|
|
|
1
|
|
537
|
use Mojo::Feed::Reader; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
8
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = "0.19"; |
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
52
|
use Scalar::Util qw(blessed); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
814
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
has feed_reader => sub { Mojo::Feed::Reader->new }; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub new { |
13
|
6
|
|
|
6
|
1
|
35478
|
my $self = shift; |
14
|
6
|
|
|
|
|
52
|
return $self->SUPER::new(feed_reader => Mojo::Feed::Reader->new(@_)); |
15
|
|
|
|
|
|
|
} |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub register { |
18
|
4
|
|
|
4
|
1
|
66
|
my ($self, $app) = @_; |
19
|
4
|
|
|
|
|
19
|
$self->feed_reader->ua($app->ua); |
20
|
4
|
|
|
20
|
|
96
|
$app->helper(parse_feed => sub { shift; $self->parse_rss(@_); }); |
|
20
|
|
|
|
|
100802
|
|
|
20
|
|
|
|
|
95
|
|
21
|
4
|
|
|
1
|
|
363
|
$app->helper(parse_rss => sub { shift; $self->parse_rss(@_); }); |
|
1
|
|
|
|
|
242
|
|
|
1
|
|
|
|
|
6
|
|
22
|
4
|
|
|
13
|
|
191
|
$app->helper(find_feeds => sub { shift; $self->find_feeds(@_); }); |
|
13
|
|
|
|
|
78757
|
|
|
13
|
|
|
|
|
43
|
|
23
|
4
|
|
|
3
|
|
182
|
$app->helper(parse_opml => sub { shift; $self->feed_reader->parse_opml(@_) }); |
|
3
|
|
|
|
|
18361
|
|
|
3
|
|
|
|
|
13
|
|
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub parse_rss { |
27
|
27
|
|
|
27
|
0
|
3720
|
my $self = shift; |
28
|
27
|
|
|
|
|
63
|
my @args = @_; |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
# handle deprecated case of using Mojo::DOM as input |
31
|
27
|
100
|
100
|
|
|
300
|
if (ref $args[0] && blessed $args[0] && $args[0]->isa('Mojo::DOM')) { |
|
|
|
100
|
|
|
|
|
32
|
1
|
|
|
|
|
6
|
$args[0] = $args[0]->to_string(); |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
# handle special case of being passed a callback - make non-blocking call |
36
|
27
|
100
|
100
|
|
|
1923
|
if ( ref $args[0] |
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
66
|
|
|
|
|
37
|
|
|
|
|
|
|
&& blessed $args[0] |
38
|
|
|
|
|
|
|
&& $args[0]->isa('Mojo::URL') |
39
|
|
|
|
|
|
|
&& ref $args[1] |
40
|
|
|
|
|
|
|
&& ref $args[1] eq 'CODE') |
41
|
|
|
|
|
|
|
{ |
42
|
|
|
|
|
|
|
$self->feed_reader->ua->get( |
43
|
|
|
|
|
|
|
$args[0], |
44
|
|
|
|
|
|
|
sub { |
45
|
2
|
|
|
2
|
|
18492
|
my $tx = pop; |
46
|
2
|
|
|
|
|
6
|
my $feed = undef; |
47
|
2
|
|
|
|
|
12
|
my $result = $tx->result; |
48
|
2
|
50
|
|
|
|
55
|
if ($result->is_success) { |
49
|
2
|
|
|
|
|
42
|
my $feed_obj = $self->feed_reader->parse($result->body, |
50
|
|
|
|
|
|
|
$result->content->charset); |
51
|
2
|
50
|
|
|
|
9889
|
if ($feed_obj) { |
52
|
2
|
|
|
|
|
16
|
$feed = $feed_obj->to_hash; |
53
|
2
|
|
|
|
|
9
|
$feed->{'htmlUrl'} = delete $feed->{'link'}; |
54
|
2
|
50
|
|
|
|
11
|
for (keys %$feed) { delete $feed->{$_} if ($feed->{$_} eq '') }; |
|
14
|
|
|
|
|
28
|
|
55
|
2
|
50
|
|
|
|
5
|
delete $feed->{'items'} if (scalar @{$feed->{'items'}} == 0); |
|
2
|
|
|
|
|
104
|
|
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
} |
58
|
2
|
|
|
|
|
12
|
$args[1]->($feed); |
59
|
|
|
|
|
|
|
} |
60
|
2
|
|
|
|
|
11
|
); |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
else { |
63
|
25
|
|
|
|
|
108
|
my $feed_obj = $self->feed_reader->parse(@args); |
64
|
25
|
100
|
|
|
|
300873
|
return undef unless ($feed_obj); |
65
|
23
|
|
|
|
|
148
|
my $feed = $feed_obj->to_hash; |
66
|
23
|
|
|
|
|
89
|
$feed->{'htmlUrl'} = delete $feed->{'link'}; |
67
|
23
|
100
|
|
|
|
130
|
for (keys %$feed) { delete $feed->{$_} if ($feed->{$_} eq '') }; |
|
161
|
|
|
|
|
354
|
|
68
|
23
|
50
|
|
|
|
49
|
delete $feed->{'items'} if (scalar @{$feed->{'items'}} == 0); |
|
23
|
|
|
|
|
96
|
|
69
|
23
|
|
|
|
|
4191
|
return $feed; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub find_feeds { |
74
|
13
|
|
|
13
|
1
|
38
|
my $self = shift; |
75
|
13
|
|
|
|
|
25
|
my $cb; |
76
|
13
|
100
|
100
|
|
|
81
|
if (ref $_[-1] && ref $_[-1] eq 'CODE') { |
77
|
3
|
|
|
|
|
9
|
$cb = pop @_; |
78
|
|
|
|
|
|
|
} |
79
|
13
|
|
|
|
|
47
|
my $promise = $self->feed_reader->discover(@_); |
80
|
13
|
100
|
|
|
|
21941
|
if ($cb) { |
81
|
3
|
|
|
|
|
11
|
$promise->then($cb); |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
else { |
84
|
10
|
|
|
|
|
18
|
my @res; |
85
|
10
|
|
|
10
|
|
39
|
$promise->then(sub { @res = @_; })->wait; |
|
10
|
|
|
|
|
4861
|
|
86
|
10
|
|
|
|
|
2029
|
return @res; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
1; |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
__END__ |