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

Revision 1173 (checked in by miyagawa, 14 years ago)

Merge from refactoring-planet. YAY!

  • 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 id 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_safe {
51     my $self = shift;
52     my $id   = $self->id || $self->permalink;
53
54     # entry without id or permalink. Try entry's date or title
55     unless ($id) {
56         $id  = $self->feed_link;
57         $id .= $self->date ? $self->date->epoch : $self->title;
58     }
59
60     $id =~ m!^https?://! ? Digest::MD5::md5_hex($id) : $id;
61 }
62
63 sub title_text {
64     my $self = shift;
65     Plagger::Util::strip_html($self->title);
66 }
67
68 sub body_text {
69     my $self = shift;
70     Plagger::Util::strip_html($self->body || '');
71 }
72
73 sub add_enclosure {
74     my($self, $enclosure) = @_;
75
76     # don't add enclosure with the same URL again and again
77     unless ($enclosure->url && grep { $_->url && $_->url eq $enclosure->url } $self->enclosures) {
78         push @{ $self->{enclosures} }, $enclosure;
79     }
80 }
81
82 sub enclosure {
83     my $self = shift;
84     wantarray ? @{$self->{enclosures}} : $self->{enclosures}->[0];
85 }
86
87 sub enclosures {
88     my $self = shift;
89     wantarray ? @{$self->{enclosures}} : $self->{enclosures};
90 }
91
92 sub has_enclosure {
93     my $self = shift;
94     scalar @{$self->{enclosures}} > 0;
95 }
96
97 sub digest {
98     my $self = shift;
99     my $data = $self->title . ($self->body || '');
100     Encode::_utf8_off($data);
101     Digest::MD5::md5_hex($data);
102 }
103
104 1;
105
Note: See TracBrowser for help on using the browser.