root/plagger/trunk/lib/Plagger/Plugin/Filter/FutureEvents.pm

Revision 311, 2.1 kB (checked in by miya, 4 years ago)

CustomFeed::AmazonECS: set 'title - author' to $entry->title
CustomFeed::AmazonECS: set editorial review to $entry->body.
Filter::FutureEvents: omit dependency on Plagger::Event.
Filter::Ahead: omit dependency on Plagger::Event.

  • Property svn:executable set to *
Line 
1 package Plagger::Plugin::Filter::FutureEvents;
2
3 use strict;
4 use base qw( Plagger::Plugin );
5 use DateTime;
6 use Time::Duration::Parse;
7
8 sub register {
9     my($self, $context) = @_;
10     $context->register_hook(
11         $self,
12         'update.feed.fixup' => \&filter,
13     );
14 }
15
16 sub init {
17     my $self = shift;
18     $self->SUPER::init(@_);
19
20     my $duration = $self->conf->{duration} || 1440; # minutes
21
22     if ($duration =~ /^\d+$/) {
23         # if it's all digit, the unit is minutes
24         $self->{duration} = $duration * 60;
25         return;
26     }
27
28     eval { require Time::Duration::Parse };
29     if ($@) {
30         Plagger->context->error("You need to install Time::Duration::Parse to use human readable timespec");
31     }
32
33     $self->{duration} = Time::Duration::Parse::parse_duration($self->{duration});
34
35 }
36
37 sub filter {
38     my($self, $context, $args) = @_;
39     my $feed = $args->{feed};
40
41     my @extracted_entries;
42     my $now  = DateTime->now( time_zone => $context->conf->{timezone} || 'local' );
43     my $then = $now->clone->add( seconds => $self->{duration} );
44
45     for my $entry ( @{$feed->entries} ){
46         my $date = $entry->date;
47         push @extracted_entries, $entry if $now < $date and $date < $then;
48     }
49
50     @{$feed->entries} = @extracted_entries;
51 }
52
53 1;
54 __END__
55
56 =head1 NAME
57
58 Plagger::Plugin::Filter::FutureEvents - Extract future events.
59
60 =head1 SYNOPSIS
61
62   # events or entries that will occur within a day.
63   - module: Filter::FutureEvents
64     config:
65       duration: 1440
66
67 =head1 DESCRIPTION
68
69 This rule finds fresh entries or feeds, which means updated date is
70 within C<duration> minutes. It defaults to 2 hours, but you'd better
71 configure the value with your cronjob interval.
72
73 =head1 CONFIG
74
75 =over 1
76
77 =item C<duration>
78
79   duration: 5
80
81 This rule matches with events that will occur within 5 minutes.
82
83 If entries don't have events, check entry->date instead of entry->event->dtstart.
84
85 If the supplied value contains only digit, it's parsed as minutes. You
86 can write in a human readable format like:
87
88   duration: 4 hours
89
90 and this module DWIMs. It defaults to I<1440>, which means a day.
91
92 =back
93
94 =head1 AUTHOR
95
96 Gosuke Miyashita
97
98 =head1 SEE ALSO
99
100 L<Plagger>, L<Time::Duration>
101
102 =cut
103
Note: See TracBrowser for help on using the browser.