root/trunk/plagger/lib/Plagger/Plugin/Publish/iCal.pm

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

Publish::iCal: don't add 1 hour duration. Fixed tests

Line 
1 package Plagger::Plugin::Publish::iCal;
2 use strict;
3 use base qw( Plagger::Plugin );
4
5 use File::Spec;
6 use Data::ICal;
7 use Data::ICal::Entry::Event;
8 use DateTime::Duration;
9 use Plagger::Util;
10
11 sub register {
12     my($self, $context) = @_;
13     $context->register_hook(
14         $self,
15         'publish.feed' => \&publish_feed,
16         'plugin.init ' => \&plugin_init,
17     );
18 }
19
20 sub plugin_init {
21     my($self, $context) = @_;
22
23     my $dir = $self->conf->{dir};
24     unless (-e $dir && -d _) {
25         mkdir $dir, 0755 or $context->error("Failed to mkdir $dir: $!");
26     }
27 }
28
29 sub publish_feed {
30     my($self, $context, $args) = @_;
31
32     my $feed = $args->{feed};
33     my $ical = Data::ICal->new;
34     $ical->add_properties(
35         'X-WR-CALNAME'  => $feed->title,
36     );
37
38     for my $entry ($feed->entries) {
39         my $date  = $entry->date;
40         my $event = Data::ICal::Entry::Event->new;
41
42         my($dtstart, $dtend);
43         if ($date->hms eq '00:00:00') {
44             $dtstart = [ $date->strftime('%Y%m%d'), { VALUE => 'DATE' } ];
45             $dtend   = [ $date->strftime('%Y%m%d'), { VALUE => 'DATE' } ];
46         } else {
47             $dtstart = $date->strftime('%Y%m%dT%H%M%S');
48             $dtend   = $date->strftime('%Y%m%dT%H%M%S');
49         }
50
51         $event->add_properties(
52             summary     => $entry->title,
53             description => $entry->summary || $entry->body || '', # xxx plaintext
54             dtstart     => $dtstart,
55             dtend       => $dtend,
56         );
57         $ical->add_entry($event);
58     }
59
60     my $file = Plagger::Util::filename_for($feed, $self->conf->{filename} || '%i.ics');
61     my $path = File::Spec->catfile($self->conf->{dir}, $file);
62
63     my $data = $ical->as_string;
64     utf8::decode($data) unless utf8::is_utf8($data);
65
66     open my $output, ">:utf8", $path or $context->error("$path: $!");
67     print $output $data;
68     close $output;
69
70     $context->log(info => "Wrote iCalendar file to $path");
71 }
72
73
74 1;
75 __END__
76
77 =head1 NAME
78
79 Plagger::Plugin::Publish::iCal - Produces iCal file out of the feed
80
81 =head1 SYNOPSIS
82
83   - module: Publish::iCal
84     config:
85       dir: /path/to/dir
86
87 =head1 DESCRIPTION
88
89 Publish::iCal creates iCal (.ics) files usine feed items. Every feed
90 is a calendar and each entry is a schedule item. Entry's posted
91 date/time is used as a schedule date/time and so on.
92
93 =head1 CONFIG
94
95 =over 4
96
97 =item dir
98
99 Directory to save ics files in.
100
101 =item filename
102
103 filename to save ics files as. Defaults to I<%i.ics>.
104
105 =head1 AUTHOR
106
107 Kentaro Kuribayashi
108
109 Tatsuhiko Miyagawa
110
111 =head1 SEE ALSO
112
113 L<Plagger>, L<Data::ICal>
114
115 =cut
Note: See TracBrowser for help on using the browser.