line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojo::Feed::Item; |
2
|
12
|
|
|
12
|
|
86
|
use Mojo::Base '-base'; |
|
12
|
|
|
|
|
23
|
|
|
12
|
|
|
|
|
81
|
|
3
|
12
|
|
|
12
|
|
1996
|
use Mojo::Util qw( trim ); |
|
12
|
|
|
|
|
24
|
|
|
12
|
|
|
|
|
540
|
|
4
|
|
|
|
|
|
|
|
5
|
12
|
|
|
12
|
|
67
|
use HTTP::Date 'str2time'; |
|
12
|
|
|
|
|
23
|
|
|
12
|
|
|
|
|
494
|
|
6
|
|
|
|
|
|
|
|
7
|
12
|
|
|
12
|
|
5548
|
use Mojo::Feed::Item::Enclosure; |
|
12
|
|
|
|
|
35
|
|
|
12
|
|
|
|
|
78
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
use overload |
10
|
0
|
|
|
0
|
|
0
|
bool => sub {1}, |
11
|
0
|
|
|
0
|
|
0
|
'""' => sub { shift->to_string }, |
12
|
12
|
|
|
12
|
|
1069
|
fallback => 1; |
|
12
|
|
|
|
|
30
|
|
|
12
|
|
|
|
|
85
|
|
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 > name', '|author', 'atom|author', 'dc|creator'], |
35
|
|
|
|
|
|
|
id => ['id', 'guid', 'link'], |
36
|
|
|
|
|
|
|
title => ['title'], |
37
|
|
|
|
|
|
|
link => ['link'], |
38
|
|
|
|
|
|
|
guid => ['guid'], |
39
|
|
|
|
|
|
|
); |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
foreach my $k (keys %selector) { |
42
|
|
|
|
|
|
|
has $k => sub { |
43
|
|
|
|
|
|
|
my $self = shift; |
44
|
|
|
|
|
|
|
for my $selector (@{$selector{$k}}) { |
45
|
|
|
|
|
|
|
if (my $p = $self->dom->at($selector, %{$self->feed->namespaces})) { |
46
|
|
|
|
|
|
|
if ($k eq 'author' && $p->at('name')) { |
47
|
|
|
|
|
|
|
return trim $p->at('name')->text; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
my ($text) = grep $_, map trim($_), grep $_, $p->text, $p->content; |
50
|
|
|
|
|
|
|
$text ||= ''; |
51
|
|
|
|
|
|
|
if ($k eq 'published') { |
52
|
|
|
|
|
|
|
return str2time($text); |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
return $text; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
return; |
58
|
|
|
|
|
|
|
}; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
has enclosures => sub { |
62
|
|
|
|
|
|
|
my $self = shift; |
63
|
|
|
|
|
|
|
my @enclosures; |
64
|
|
|
|
|
|
|
$self->dom->find('enclosure')->each(sub { |
65
|
|
|
|
|
|
|
push @enclosures, $_; |
66
|
|
|
|
|
|
|
}); |
67
|
|
|
|
|
|
|
$self->dom->find('link')->each(sub { |
68
|
|
|
|
|
|
|
my $l = shift; |
69
|
|
|
|
|
|
|
if ($l->attr('href') && $l->attr('rel') && $l->attr('rel') eq 'enclosure') { |
70
|
|
|
|
|
|
|
push @enclosures, $l; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
}); |
73
|
|
|
|
|
|
|
return Mojo::Collection->new( |
74
|
|
|
|
|
|
|
map { Mojo::Feed::Item::Enclosure->new(dom => $_) } @enclosures); |
75
|
|
|
|
|
|
|
}; |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
has link => sub { |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
# let's handle links seperately, because ATOM loves these buggers: |
80
|
|
|
|
|
|
|
my $link; |
81
|
|
|
|
|
|
|
shift->dom->find('link')->each(sub { |
82
|
|
|
|
|
|
|
my $l = shift; |
83
|
|
|
|
|
|
|
if ($l->attr('href') |
84
|
|
|
|
|
|
|
&& (!$l->attr('rel') || $l->attr('rel') eq 'alternate')) |
85
|
|
|
|
|
|
|
{ |
86
|
|
|
|
|
|
|
$link = trim $l->attr('href'); |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
else { |
89
|
|
|
|
|
|
|
if ($l->text =~ /\w+/) { |
90
|
|
|
|
|
|
|
$link = trim $l->text; # simple link |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
}); |
94
|
|
|
|
|
|
|
return $link; |
95
|
|
|
|
|
|
|
}; |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub to_string { |
98
|
0
|
|
|
0
|
1
|
0
|
shift->dom->to_string; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
sub to_hash { |
102
|
148
|
|
|
148
|
1
|
1795
|
my $self = shift; |
103
|
148
|
|
100
|
|
|
577
|
my $hash = {map { $_ => '' . ($self->$_ || '') } keys %selector}; |
|
1184
|
|
|
|
|
22862
|
|
104
|
148
|
100
|
|
|
|
725
|
if ($self->enclosures->size) { |
105
|
6
|
|
|
|
|
107
|
$hash->{'enclosures'} = $self->enclosures->map('to_hash')->to_array; |
106
|
|
|
|
|
|
|
} |
107
|
148
|
100
|
|
|
|
1837
|
if ($self->tags->size) { |
108
|
90
|
|
|
|
|
4773
|
$hash->{'tags'} = $self->tags->to_array; |
109
|
|
|
|
|
|
|
} |
110
|
148
|
|
|
|
|
83371
|
return $hash; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
1; |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
__END__ |