line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojo::Feed::Item; |
2
|
11
|
|
|
11
|
|
81
|
use Mojo::Base '-base'; |
|
11
|
|
|
|
|
23
|
|
|
11
|
|
|
|
|
76
|
|
3
|
11
|
|
|
11
|
|
1731
|
use Mojo::Util qw( trim ); |
|
11
|
|
|
|
|
22
|
|
|
11
|
|
|
|
|
475
|
|
4
|
|
|
|
|
|
|
|
5
|
11
|
|
|
11
|
|
62
|
use HTTP::Date 'str2time'; |
|
11
|
|
|
|
|
19
|
|
|
11
|
|
|
|
|
420
|
|
6
|
|
|
|
|
|
|
|
7
|
11
|
|
|
11
|
|
4992
|
use Mojo::Feed::Item::Enclosure; |
|
11
|
|
|
|
|
31
|
|
|
11
|
|
|
|
|
71
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
use overload |
10
|
0
|
|
|
0
|
|
0
|
bool => sub {1}, |
11
|
0
|
|
|
0
|
|
0
|
'""' => sub { shift->to_string }, |
12
|
11
|
|
|
11
|
|
1039
|
fallback => 1; |
|
11
|
|
|
|
|
23
|
|
|
11
|
|
|
|
|
82
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
has [qw(title link content id description guid published author)]; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
has tags => sub { |
18
|
|
|
|
|
|
|
shift->dom->find('category, dc\:subject') |
19
|
|
|
|
|
|
|
->map(sub { $_[0]->text || $_[0]->attr('term') }); |
20
|
|
|
|
|
|
|
}; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
has 'dom'; |
23
|
|
|
|
|
|
|
has feed => undef, weak => 1; |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
has summary => sub { shift->description }; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
my %selector = ( |
28
|
|
|
|
|
|
|
content => ['content', 'content\:encoded', 'xhtml\:body', 'description'], |
29
|
|
|
|
|
|
|
description => ['description', 'summary'], |
30
|
|
|
|
|
|
|
published => [ |
31
|
|
|
|
|
|
|
'published', 'pubDate', 'dc\:date', 'created', |
32
|
|
|
|
|
|
|
'issued', 'updated', 'modified' |
33
|
|
|
|
|
|
|
], |
34
|
|
|
|
|
|
|
author => ['author', 'dc\:creator'], |
35
|
|
|
|
|
|
|
id => ['id', 'guid', 'link'], |
36
|
|
|
|
|
|
|
title => ['title'], |
37
|
|
|
|
|
|
|
link => ['link'], |
38
|
|
|
|
|
|
|
guid => ['guid'], |
39
|
|
|
|
|
|
|
); |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub _at { |
42
|
1456
|
|
|
1456
|
|
2334
|
my ($self, $selector) = @_; |
43
|
|
|
|
|
|
|
return $self->dom->find($selector)->first(sub { |
44
|
751
|
|
|
751
|
|
463657
|
my $tag = $_->tag; |
45
|
751
|
|
|
|
|
9249
|
$tag =~ s/:/\\:/; |
46
|
751
|
|
|
|
|
4456
|
return $tag eq $selector; |
47
|
1456
|
|
|
|
|
2512
|
}); |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
foreach my $k (keys %selector) { |
51
|
|
|
|
|
|
|
has $k => sub { |
52
|
|
|
|
|
|
|
my $self = shift; |
53
|
|
|
|
|
|
|
for my $selector (@{$selector{$k}}) { |
54
|
|
|
|
|
|
|
if (my $p = $self->_at($selector)) { |
55
|
|
|
|
|
|
|
if ($k eq 'author' && $p->at('name')) { |
56
|
|
|
|
|
|
|
return trim $p->at('name')->text; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
my $text = trim ($p->text || $p->content || ''); |
59
|
|
|
|
|
|
|
if ($k eq 'published') { |
60
|
|
|
|
|
|
|
return str2time($text); |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
return $text; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
return; |
66
|
|
|
|
|
|
|
}; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
has enclosures => sub { |
70
|
|
|
|
|
|
|
my $self = shift; |
71
|
|
|
|
|
|
|
my @enclosures; |
72
|
|
|
|
|
|
|
$self->dom->find('enclosure')->each(sub { |
73
|
|
|
|
|
|
|
push @enclosures, $_; |
74
|
|
|
|
|
|
|
}); |
75
|
|
|
|
|
|
|
$self->dom->find('link')->each(sub { |
76
|
|
|
|
|
|
|
my $l = shift; |
77
|
|
|
|
|
|
|
if ($l->attr('href') && $l->attr('rel') && $l->attr('rel') eq 'enclosure') { |
78
|
|
|
|
|
|
|
push @enclosures, $l; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
}); |
81
|
|
|
|
|
|
|
return Mojo::Collection->new( |
82
|
|
|
|
|
|
|
map { Mojo::Feed::Item::Enclosure->new(dom => $_) } @enclosures); |
83
|
|
|
|
|
|
|
}; |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
has link => sub { |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
# let's handle links seperately, because ATOM loves these buggers: |
88
|
|
|
|
|
|
|
my $link; |
89
|
|
|
|
|
|
|
shift->dom->find('link')->each(sub { |
90
|
|
|
|
|
|
|
my $l = shift; |
91
|
|
|
|
|
|
|
if ($l->attr('href') |
92
|
|
|
|
|
|
|
&& (!$l->attr('rel') || $l->attr('rel') eq 'alternate')) |
93
|
|
|
|
|
|
|
{ |
94
|
|
|
|
|
|
|
$link = trim $l->attr('href'); |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
else { |
97
|
|
|
|
|
|
|
if ($l->text =~ /\w+/) { |
98
|
|
|
|
|
|
|
$link = trim $l->text; # simple link |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
}); |
102
|
|
|
|
|
|
|
return $link; |
103
|
|
|
|
|
|
|
}; |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
sub to_string { |
106
|
0
|
|
|
0
|
1
|
0
|
shift->dom->to_string; |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
sub to_hash { |
110
|
136
|
|
|
136
|
1
|
1353
|
my $self = shift; |
111
|
136
|
|
100
|
|
|
469
|
my $hash = {map { $_ => '' . ($self->$_ || '') } keys %selector}; |
|
1088
|
|
|
|
|
22948
|
|
112
|
136
|
100
|
|
|
|
551
|
if ($self->enclosures->size) { |
113
|
6
|
|
|
|
|
99
|
$hash->{'enclosures'} = $self->enclosures->map('to_hash')->to_array; |
114
|
|
|
|
|
|
|
} |
115
|
136
|
100
|
|
|
|
1443
|
if ($self->tags->size) { |
116
|
78
|
|
|
|
|
3348
|
$hash->{'tags'} = $self->tags->to_array; |
117
|
|
|
|
|
|
|
} |
118
|
136
|
|
|
|
|
66894
|
return $hash; |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
1; |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
__END__ |