File: ics2rss

package info (click to toggle)
libdata-ical-datetime-perl 0.82-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 308 kB
  • sloc: perl: 1,708; makefile: 2; sh: 1
file content (70 lines) | stat: -rw-r--r-- 1,740 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/perl

use constant SOURCE => "/Users/mark/Library/Application Support/iCal/Sources/E49F498D-968C-4A4F-93E8-6E9AF7F44B2B.calendar/corestorage.ics";

use strict;
use warnings;

use XML::RSS;

# load the calendar
use Data::ICal::DateTime;

# create a new RSS feed
my $rss = new XML::RSS (version => '1.0');

rss(0, 1,  "today!");
rss(1, 2,  "tomorrow!");
rss(2, 3,  "day after tomorrow!");
rss(3, 7,  "within a week");
rss(7, 14, "within a fortnight");

print $rss->as_string;

#################################

my ($ical, @events);
sub rss
{
  my ($start, $end, $comment) = @_;

  # calculate the next 14 days.  This is done once because it 'explodes'
  # the events and hence is quite expensive
  unless ($ical)
  {
    $ical = Data::ICal->new( filename => SOURCE );
    @events = $ical->events(
      DateTime::Span->from_datetimes(
        start => midnight(),
        end   => midnight()->add( days => 14, seconds => -1),
      )
    );
  }
  
  # what are we looking for this time?
  my $span = DateTime::Span->from_datetimes(
    start => midnight()->add( days => $start ),
    end   => midnight()->add( days => $end, seconds => -1),
  );
  
  # find which of those events in the next 14 days we're concerned
  # with and add them to the rss feed
  foreach my $event (grep { $_->is_in($span) } @events)
  {
    $rss->add_item(
     title => $event->summary . " $comment",
     description => "On ".$event->start->ymd." (".(qw(
      Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sunday ))[ $event->start->dow ]
      . ")"
    );
  }
}

# midnight today.
my $midnight;
sub midnight
{
   $midnight ||= 
     DateTime->now->set( hour => 0, minute => 0, second => 0, nanosecond => 0 );
   return $midnight->clone;
}