-
Notifications
You must be signed in to change notification settings - Fork 3
/
update-holidays.pl
executable file
·224 lines (188 loc) · 7.72 KB
/
update-holidays.pl
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#! /usr/bin/perl
################################################################################
##
## Copyright 2006 - 2015, Paul Beckingham, Federico Hernandez.
##
## Permission is hereby granted, free of charge, to any person obtaining a copy
## of this software and associated documentation files (the "Software"), to deal
## in the Software without restriction, including without limitation the rights
## to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
## copies of the Software, and to permit persons to whom the Software is
## furnished to do so, subject to the following conditions:
##
## The above copyright notice and this permission notice shall be included
## in all copies or substantial portions of the Software.
##
## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
## OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
## FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
## THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
## LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
## OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
## SOFTWARE.
##
## http://www.opensource.org/licenses/mit-license.php
##
################################################################################
use strict;
use warnings;
use Getopt::Long;
# Give a nice error if the (non-standard) JSON module is not installed.
eval "use JSON";
if ($@)
{
print "Error: You need to install the JSON Perl module.\n";
exit 1;
}
eval "use LWP::Simple";
if ($@)
{
print "Error: You need to install the LWP::Simple Perl module.\n";
exit 1;
}
eval "use Encode";
if ($@)
{
print "Error: You need to install the Encode Perl module.\n";
exit 1;
}
# Command line options, argument validation.
my $help;
my $locale;
my @regions;
my $file;
Getopt::Long::GetOptions (
'help' => \$help,
'locale=s' => \$locale,
'region=s' => \@regions,
'file=s' => \$file);
if ($help || !$file)
{
print <<EOF;
usage: update-holidays.pl [--help]
[--locale <locale>]
[--region <region>]
--file <holiday-file>
--help Displays this message.
--locale Locale to be used, if it cannot be derived from the file name.
--region Regional data. May be specified more than once.
--file Location of the holiday file to update.
Note: this file *will* be overwritten.
Typical usage is to simply specify a locale, like this:
./update-holidays.pl --locale en-US --file holidays.en-US.rc
This will give you all holidays for the locale. If you want data for a locale
that has regional data, you may want to specify regions, to make sure you get
the data local to your region. For example, the following command will download
Australian holiday data that is either national, or specific to the New South
Wales territory.
update-holidays.pl --locale en-AU \
--region NSW \
--file holidays.en-AU.rc
Multiple regions may be specified, such as the two Swiss cantons of Zürich and
Schwyz:
update-holidays.pl --locale de-CH \
--region Zürich \
--region Schwyz \
--file holidays.de-CH.rc
See http://holidata.net for details of supported locales and regions.
It is recommended that you regularly update your holiday files. Not only does
this keep your holiday data current, but allows for corrected data to be used.
EOF
exit 1;
}
# File name is required.
die "You must specify a holiday file to update.\n" if ! $file;
# Convert @regions to %region_hash to simplify lookup.
my %region_hash = map {$_ => undef} @regions;
# Perhaps the locale can be found in the file name, if not already specified?
if (!$locale &&
$file =~ /([a-z]{2}-[A-Z]{2})/)
{
$locale = $1;
}
die "The locale could not be determined from the file name. " .
"Please secify a locale.\n" if !$locale;
# Determine the current year.
my $current = (localtime (time))[5] + 1900;
my $next = $current + 1;
# Construct the holidata.net URL.
my $url_current = "http://holidata.net/${locale}/${current}.json";
my $url_next = "http://holidata.net/${locale}/${next}.json";
# Fetch data for the current year.
my $data_current = get ($url_current);
print "\n",
"Data for ${locale}, for ${current} could not be downloaded. This could\n",
"mean that you do not have an internet connection, or that\n",
"Holidata.net does not support this locale and/or year.\n",
"\n"
unless defined $data_current;
# Fetch data for the next year.
my $data_next = get ($url_next);
print "\n",
"Data for ${locale}, for ${next} could not be downloaded. This could\n",
"mean that you do not have an internet connection, or that\n",
"Holidata.net does not support this locale and/or year.\n",
"\n"
unless defined $data_next;
# Without data, cannot proceed.
my $data;
$data .= decode ('utf-8', $data_current) if defined $data_current;
$data .= decode ('utf-8', $data_next) if defined $data_next;
exit (1) if !defined $data || $data eq '';
# Filter the holidays according to @regions.
my $id = 1;
my $content;
my %seen;
for my $holiday (split /\n/ms, $data)
{
my $parsed = from_json ($holiday);
# Change date format from YYYY-MM-DD to YYYYMMDD.
$parsed->{'date'} =~ s/-//g;
if (@regions == 0 ||
(@regions > 0 && ($parsed->{'region'} eq '' ||
exists $region_hash{$parsed->{'region'}})))
{
next if $seen{ $parsed->{'description'} . ':' . $parsed->{'date'} }++;
$content .= "holiday.${locale}${id}.name=" . $parsed->{'description'} . "\n" .
"holiday.${locale}${id}.date=" . $parsed->{'date'} . "\n";
}
++$id;
}
# Overwrite the file.
if (open my $fh, '>:utf8', $file)
{
print $fh
"###############################################################################\n",
"# International Holiday Data provided by Holidata.net\n",
"# ${url_current}\n",
"# ${url_next}\n",
"#\n",
"# Copyright 2006 - 2015, Paul Beckingham, Federico Hernandez.\n",
"#\n",
"# Permission is hereby granted, free of charge, to any person obtaining a copy\n",
"# of this software and associated documentation files (the \"Software\"), to deal\n",
"# in the Software without restriction, including without limitation the rights\n",
"# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n",
"# copies of the Software, and to permit persons to whom the Software is\n",
"# furnished to do so, subject to the following conditions:\n",
"#\n",
"# The above copyright notice and this permission notice shall be included\n",
"# in all copies or substantial portions of the Software.\n",
"#\n",
"# THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS\n",
"# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n",
"# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL\n",
"# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n",
"# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n",
"# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n",
"# SOFTWARE.\n",
"#\n",
"# http://www.opensource.org/licenses/mit-license.php\n",
"#\n",
"###############################################################################\n",
"\n",
$content;
close $fh;
}
exit 0;
################################################################################