root/trunk/plagger/lib/Plagger/Entry.pm

Revision 1668 (checked in by miyagawa, 14 years ago)
  • merged [1621] from hackathon-summary branch
  • autoload Filter::FloatingDateTime? from Publish::Feed to address invalid RFC date format. Fixes #400
  • Property svn:keywords set to Id Revision
Line 
1 package Plagger::Entry;
2 use strict;
3
4 use base qw( Plagger::Thing );
5 __PACKAGE__->mk_accessors(qw( title author tags link feed_link summary body rate icon meta source ));
6 __PACKAGE__->mk_date_accessors(qw( date ));
7
8 use Digest::MD5;
9 use DateTime::Format::Mail;
10 use Storable;
11 use Plagger::Util;
12
13 sub new {
14     my $class = shift;
15     bless {
16         rate    => 0,
17         widgets => [],
18         tags    => [],
19         meta    => {},
20         enclosures => [],
21     }, $class;
22 }
23
24 sub add_rate {
25     my($self, $rate) = @_;
26     $self->rate( $self->rate + $rate );
27 }
28
29 sub text {
30     my $self = shift;
31     join "\n", $self->link, $self->title, $self->body;
32 }
33
34 sub add_widget {
35     my($self, $widget) = @_;
36     push @{ $self->{widgets} }, $widget;
37 }
38
39 sub widgets {
40     my $self = shift;
41     wantarray ? @{ $self->{widgets} } : $self->{widgets};
42 }
43
44 sub permalink {
45     my $self = shift;
46     $self->{permalink} = shift if @_;
47     $self->{permalink} || $self->link;
48 }
49
50 sub id {
51     my $self = shift;
52     $self->{id} = shift if @_;
53     $self->{id} || $self->permalink || do {
54         my $id = $self->feed_link;
55         $id .= $self->date ? $self->date->epoch : $self->title;
56         $id;
57     };
58 }
59
60 sub id_safe {
61     my $self = shift;
62     Plagger::Util::safe_id($self->id);
63 }
64
65 sub title_text {
66     my $self = shift;
67     Plagger::Util::strip_html($self->title);
68 }
69
70 sub body_text {
71     my $self = shift;
72     Plagger::Util::strip_html($self->body || '');
73 }
74
75 sub add_enclosure {
76     my($self, $enclosure) = @_;
77
78     # don't add enclosure with the same URL again and again
79     unless ($enclosure->url && grep { $_->url && $_->url eq $enclosure->url } $self->enclosures) {
80         push @{ $self->{enclosures} }, $enclosure;
81     }
82 }
83
84 sub enclosure {
85     my $self = shift;
86     wantarray ? @{$self->{enclosures}} : $self->{enclosures}->[0];
87 }
88
89 sub enclosures {
90     my $self = shift;
91     wantarray ? @{$self->{enclosures}} : $self->{enclosures};
92 }
93
94 sub has_enclosure {
95     my $self = shift;
96     scalar @{$self->{enclosures}} > 0;
97 }
98
99 sub digest {
100     my $self = shift;
101     my $data = $self->title . ($self->body || '');
102     Encode::_utf8_off($data);
103     Digest::MD5::md5_hex($data);
104 }
105
106 1;
107
Note: See TracBrowser for help on using the browser.