Last active
August 29, 2015 13:56
-
-
Save fujikky/9321504 to your computer and use it in GitHub Desktop.
Generate Holidays JSON
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
#!/usr/bin/env ruby | |
# | |
# Generate Holidays JSON | |
# | |
# Copyright (c) 2014 Yusuke Fujiki @ Himakan | |
# | |
# 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. | |
require "uri" | |
require "open-uri" | |
require "json" | |
require "openssl" | |
require "optparse" | |
config = Hash.new | |
opts = OptionParser.new | |
opts.on("-k google_api_key", desc = "You have to get API key for Google Calendar API Access. See more: https://developers.google.com/console/help/new/#generatingdevkeys"){|v| config[:key] = v } | |
opts.on("-l [locale]", desc = "United States = \"us\", Japan = \"jp\", South Korea = \"kr\" (Default: \"us\")"){|v| config[:locale] = v } | |
opts.on("-s [start_year]", desc = "Start Year (ex. 2013, Default: this year)"){|v| config[:start_year] = v } | |
opts.on("-e [end_year]", desc = "End Year (ex. 2014, Default: next year)"){|v| config[:end_year] = v } | |
opts.on("-f [format]", desc = "Time Format, ISO 8601 = \"iso\" or Unix Time = \"unix\" (Default: \"iso\")"){|v| config[:format] = v } | |
opts.parse!(ARGV) | |
if config[:key].nil? | |
puts opts | |
exit | |
end | |
timezone = { | |
"us" => "America/New_York", | |
"jp" => "Asia/Tokyo", | |
"kr" => "Asia/Seoul", | |
} | |
holiday_id = { | |
"us" => URI.encode("en.usa#[email protected]"), | |
"jp" => URI.encode("ja.japanese#[email protected]"), | |
"kr" => URI.encode("ko.south_korea#[email protected]"), | |
} | |
start_year = config[:start_year] || DateTime.now.year.to_s | |
end_year = config[:end_year] || (DateTime.now.year + 1).to_s | |
start_date = URI.encode "#{start_year}-01-01T00:00:00.000Z" | |
end_date = URI.encode "#{end_year}-01-01T00:00:00.000Z" | |
if (!timezone.has_key?(config[:locale])) | |
config[:locale] = "us" | |
end | |
ENV["TZ"] = timezone[config[:locale]] | |
url = "https://www.googleapis.com/calendar/v3/calendars/#{holiday_id[config[:locale]]}/events?" + | |
"key=#{config[:key]}" + | |
"&timeMin=#{start_date}" + | |
"&timeMax=#{end_date}" + | |
"&orderBy=startTime" + | |
"&singleEvents=true" | |
response = open(url, :ssl_verify_mode => OpenSSL::SSL::VERIFY_NONE).read | |
result = JSON.parse response | |
dates = result["items"].map {|item| | |
date = Time.parse(item["start"]["date"]) | |
if config[:format] == "unix" | |
date = date.to_i | |
end | |
date | |
} | |
puts dates.to_json |
Author
fujikky
commented
Mar 3, 2014
# Normal
$ ./holidays_json.rb -k YOUR_API_KEY -s 2014 -e 2015 -l jp
["2014-01-01 00:00:00 +0900","2014-01-02 00:00:00 +0900","2014-01-03 00:00:00 +0900","2014-01-13 00:00:00 +0900","2014-02-11 00:00:00 +0900","2014-03-21 00:00:00 +0900","2014-04-29 00:00:00 +0900","2014-05-03 00:00:00 +0900","2014-05-04 00:00:00 +0900","2014-05-05 00:00:00 +0900","2014-05-06 00:00:00 +0900","2014-07-21 00:00:00 +0900","2014-09-15 00:00:00 +0900","2014-09-23 00:00:00 +0900","2014-10-13 00:00:00 +0900","2014-11-03 00:00:00 +0900","2014-11-23 00:00:00 +0900","2014-11-24 00:00:00 +0900","2014-12-23 00:00:00 +0900","2014-12-25 00:00:00 +0900","2014-12-31 00:00:00 +0900"]
# Unix Time Stamp Format
$ ./holidays_json.rb -k YOUR_API_KEY -s 2014 -e 2015 -l jp -f unix
[1388502000,1388588400,1388674800,1389538800,1392044400,1395327600,1398697200,1399042800,1399129200,1399215600,1399302000,1405868400,1410706800,1411398000,1413126000,1414940400,1416668400,1416754800,1419260400,1419433200,1419951600]
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment