Skip to content

Instantly share code, notes, and snippets.

@mattn
Forked from lestrrat/gist:406110
Created May 19, 2010 10:26
Show Gist options
  • Save mattn/406170 to your computer and use it in GitHub Desktop.
Save mattn/406170 to your computer and use it in GitHub Desktop.
#!perl
# 日本の祝日をGoogleから持ってくるスクリプト
use strict;
use LWP::UserAgent;
use POSIX qw(strftime);
use URI;
use JSON::XS;
use XML::LibXML;
use XML::LibXML::XPathContext;
use Time::Local;
use YAML::Syck;
sub get_holidays {
my @holidays;
my $ua = LWP::UserAgent->new();
$ua->env_proxy;
my $uri = URI->new('http://www.google.com/calendar/feeds/outid3el0qkcrsuf89fltf7a4qbacgt9@import.calendar.google.com/public/full-noattendees');
$uri->query_form({
'start-min' => sprintf('%04d-%02d-%02d', (localtime)[5] + 1900, 1, 1),
'start-max' => sprintf('%04d-%02d-%02d', (localtime)[5] + 1900, 12, 31),
});
my $res = $ua->get($uri);
my $p = XML::LibXML->new();
my $dom = $p->parse_string( $res->content );
my $xpc = XML::LibXML::XPathContext->new();
$xpc->registerNs('atom', "http://www.w3.org/2005/Atom");
$xpc->registerNs('gd', "http://schemas.google.com/g/2005" );
foreach my $entry ( $xpc->findnodes('/atom:feed/atom:entry', $dom) ) {
my $start = $entry->findvalue('gd:when/@startTime');
if ($start =~ /^(\d{4})-(\d{2})-(\d{2})$/) {
push @holidays, {
day => timelocal(0, 0, 0, int($3), int($2) - 1, int($1) - 1900),
name => $entry->findvalue('*[local-name()="title"]')
};
}
}
return sort { $a->{day} <=> $b->{day} } @holidays;
}
print map { strftime("%Y-%m-%d ", localtime($_->{day})) . $_->{name} . "\n" } get_holidays();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment