-
-
Save mattn/406170 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!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/[email protected]/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