root/trunk/plagger/assets/plugins/CustomFeed-Script/slims_calendar.pl

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

s/entries/entry/

  • Property svn:executable set to *
Line 
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4 use DateTime;
5 use DateTime::Format::W3CDTF;
6 use LWP::UserAgent;
7 use HTML::TreeBuilder::XPath;
8 use YAML;
9
10 my $this = DateTime->now(time_zone => 'America/Los_Angeles');
11    $this->set(day => 1, hour => 0, minute => 0, second => 0);
12 our $url_base = "http://www.slims-sf.com/slims-bin/showcal?date=%04d-%02d";
13
14 my $feed = {
15     title => "Slim's schedule",
16     link  => "http://www.slims-sf.com/slims-bin/showcal",
17 };
18 my @months = ($this->clone, do { $this->add(months => 1); $this->clone }, do { $this->add(months => 1); $this->clone });
19 for my $month (@months) {
20     fetch_calendar($month, $feed);
21 }
22
23 print YAML::Dump $feed;
24
25 sub fetch_calendar {
26     my($month, $feed) = @_;
27
28     my $url = sprintf $url_base, $month->year, $month->month;
29     my $ua  = LWP::UserAgent->new;
30     my $content = $ua->get($url)->content;
31
32     my $tree = HTML::TreeBuilder::XPath->new;
33     $tree->parse($content);
34
35     my @node = $tree->findnodes(q(//div[@align="center"]/table/tr[@valign="top"]/td));
36     for my $node (@node) {
37         my $day   = ($node->look_down(_tag => 'font'))[0] or next;
38         my $start = ($node->look_down(_tag => 'font', size => 1))[0] or next;
39
40         my($hour, $min, $ampm) = $start->as_text =~ /(\d+):(\d+) (AM|PM)/ or next;
41         $hour += 12 if $ampm eq 'PM';
42
43         my $date = $month->clone;
44         $date->set(
45             day  => $day->as_text,
46             hour => $hour,
47             minute => $min,
48         );
49
50         my $headliner = ($node->look_down(_tag => 'b'))[0] or next;
51         my $info      = ($node->look_down(_tag => 'a'))[0] or next;
52         push @{$feed->{entry}}, {
53             date  =>  DateTime::Format::W3CDTF->format_datetime($date),
54             title => $headliner->as_text,
55             link  => URI->new_abs( $info->attr('href'), $url )->as_string,
56         };
57     }
58 }
59
60
Note: See TracBrowser for help on using the browser.