root/branches/hackathon-summary/plagger/lib/Plagger/Entry.pm

Revision 1554 (checked in by miyagawa, 5 years ago)

added language method to Plagger::Entry

  • 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( tags link feed_link id rate icon meta source language ));
6 __PACKAGE__->mk_text_accessors(qw( title author summary body ));
7 __PACKAGE__->mk_date_accessors(qw( date ));
8
9 use Digest::MD5;
10 use DateTime::Format::Mail;
11 use Storable;
12 use Plagger::Util;
13
14 sub new {
15     my $class = shift;
16     bless {
17         rate    => 0,
18         widgets => [],
19         tags    => [],
20         meta    => {},
21         enclosures => [],
22     }, $class;
23 }
24
25 sub add_rate {
26     my($self, $rate) = @_;
27     $self->rate( $self->rate + $rate );
28 }
29
30 sub text {
31     my $self = shift;
32     join "\n", $self->link, $self->title, $self->body;
33 }
34
35 sub add_widget {
36     my($self, $widget) = @_;
37     push @{ $self->{widgets} }, $widget;
38 }
39
40 sub widgets {
41     my $self = shift;
42     wantarray ? @{ $self->{widgets} } : $self->{widgets};
43 }
44
45 sub permalink {
46     my $self = shift;
47     $self->{permalink} = shift if @_;
48     $self->{permalink} || $self->link;
49 }
50
51 sub id_safe {
52     my $self = shift;
53     my $id   = $self->id || $self->permalink;
54
55     # entry without id or permalink. Try entry's date or title
56     unless ($id) {
57         $id  = $self->feed_link;
58         $id .= $self->date ? $self->date->epoch : $self->title;
59     }
60
61     $id =~ m!^https?://! ? Digest::MD5::md5_hex($id) : $id;
62 }
63
64 sub title_text {
65     my $self = shift;
66     $self->title ? $self->title->plaintext : undef;
67 }
68
69 sub body_text {
70     my $self = shift;
71     $self->body ? $self->body->plaintext : undef;
72 }
73
74 sub add_enclosure {
75     my($self, $enclosure) = @_;
76
77     # don't add enclosure with the same URL again and again
78     unless ($enclosure->url && grep { $_->url && $_->url eq $enclosure->url } $self->enclosures) {
79         push @{ $self->{enclosures} }, $enclosure;
80     }
81 }
82
83 sub enclosure {
84     my $self = shift;
85     wantarray ? @{$self->{enclosures}} : $self->{enclosures}->[0];
86 }
87
88 sub enclosures {
89     my $self = shift;
90     wantarray ? @{$self->{enclosures}} : $self->{enclosures};
91 }
92
93 sub has_enclosure {
94     my $self = shift;
95     scalar @{$self->{enclosures}} > 0;
96 }
97
98 sub digest {
99     my $self = shift;
100     my $data = $self->title . ($self->body || '');
101     Encode::_utf8_off($data);
102     Digest::MD5::md5_hex($data);
103 }
104
105 1;
106
Note: See TracBrowser for help on using the browser.