root/trunk/plagger/lib/Plagger/Rule/Fresh.pm

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

Merge from refactoring-planet. YAY!

  • Property svn:keywords set to Id Revision
Line 
1 package Plagger::Rule::Fresh;
2 use strict;
3 use base qw( Plagger::Rule );
4
5 sub init {
6     my $self = shift;
7
8     if (my $config = $self->{mtime}) {
9         my $path = $config->{path};
10
11         # If autoupdate is set, automatically touch the file
12         my $now   = time;
13         my $mtime = (stat($path))[9];
14
15         if ($config->{autoupdate}) {
16             if ($mtime) {
17                 utime $now, $now, $path or Plagger->context->error("$path: $!");
18             } else {
19                 open my $fh, ">", $path or Plagger->context->error("$path: $!");
20             }
21         } else {
22             $mtime or Plagger->context->error("$path: $!")
23         }
24         $self->{timestamp} = $mtime || 0;
25     } else {
26         eval {
27             $self->{duration}   = $self->init_duration();
28             $self->{timestamp}  = time - $self->{duration};
29         };
30         if ($@) {
31             Plagger->context->error("Parse duration error: $@");
32         }
33     }
34
35     $self->{compare_dt} = Plagger::Date->from_epoch(epoch => $self->{timestamp});
36 }
37
38 sub init_duration {
39     my $self = shift;
40
41     my $duration = $self->{duration} || 120; # minutes
42
43     if ($duration =~ /^\d+$/) {
44         # if it's all digit, the unit is minutes
45         return $duration * 60;
46     }
47
48     eval { require Time::Duration::Parse };
49     if ($@) {
50         Plagger->context->error("You need to install Time::Duration::Parse to use human readable timespec");
51     }
52
53     Time::Duration::Parse::parse_duration($self->{duration});
54 }
55
56 sub id {
57     my $self = shift;
58     return "fresh:$self->{duration}sec";
59 }
60
61 sub as_title {
62     my $self = shift;
63
64     my $duration = $self->{duration}
65         ? "within " . $self->duration_friendly
66         : "since "  . $self->{compare_dt}->strftime('%Y/%m/%d %H:%M');
67
68     return "updated " . $duration;
69 }
70
71 sub duration_friendly {
72     my $self = shift;
73     eval { require Time::Duration };
74     return $@ ? "$self->{duration} seconds"
75               : Time::Duration::duration($self->{duration});
76 }
77
78 sub dispatch {
79     my($self, $args) = @_;
80
81     my $date;
82     if ($args->{entry}) {
83         $date = $args->{entry}->date;
84     } elsif ($args->{feed}) {
85         $date = $args->{feed}->updated;
86     } else {
87         Plagger->context->error("No entry nor feed object in this plugin phase");
88     }
89
90     # no date field ... should be Fresh, ugh.
91     $date ? $date >= $self->{compare_dt} : 1;
92 }
93
94 1;
95
96 __END__
97
98 =head1 NAME
99
100 Plagger::Rule::Fresh - Rule to find 'fresh' entries or feeds
101
102 =head1 SYNOPSIS
103
104   # entries updated within 120 minutes
105   - module: SmartFeed
106     config:
107       id: fresh-entries
108     rule:
109       module: Fresh
110       duration: 120
111
112   # remove entries older than mtime of /tmp/foo.tmp
113   - module: Filter::Rule
114     rule:
115       module: Fresh
116       mtime:
117         path: /tmp/foo.tmp
118         autoupdate: 1
119
120 =head1 DESCRIPTION
121
122 This rule finds fresh entries or feeds, which means updated date is
123 within C<duration> minutes. It defaults to 2 hours, but you'd better
124 configure the value with your cronjob interval.
125
126 =head1 CONFIG
127
128 =over 4
129
130 =item C<duration>
131
132   duration: 5
133
134 This rule matches with entries posted within 5 minutes. When you
135 invoke C<plagger> script in cronjob, you'd better specify the
136 same C<duration> variable with the job interval.
137
138 If the supplied value contains only digit, it's parsed as minutes. You
139 can write in a human readable format like:
140
141   duration: 4 hours
142
143 and this module DWIMs. It defaults to I<120>, which means 2 hours.
144
145 =item C<mtime>
146
147   mtime:
148     path: /path/to/mtime.file
149     autoupdate: 1
150
151 This rule matches with entries newer than mtime of
152 C</path/to/mtime.file>. If C<autoupdate> option is set (default is
153 off), this plugin automatically creates and updates the file in plugin
154 registration phase.
155
156 =back
157
158 =head1 AUTHOR
159
160 Tatsuhiko Miyagawa
161
162 Thanks to youpy, who originally wrote Plagger::Plugin::Filter::Fresh
163 at L<http://subtech.g.hatena.ne.jp/youpy/20060224/p1>
164
165 =head1 SEE ALSO
166
167 L<Plagger>, L<Time::Duration>
168
169 =cut
Note: See TracBrowser for help on using the browser.