LoginSignup
9

More than 3 years have passed since last update.

国立天文台の暦要項データを取得する

Last updated at Posted at 2015-01-09

祝祭日のデータを手動で毎年更新するのは、面倒だろうと思う。

官報に掲載される日本の暦は、国立天文台が計算している。これをまとめたものを暦要項という。暦要項は、毎年2月に次の年のものが公示される。そのデータは、国立天文台天文情報センター暦計算室がGoogle Calendarで公開しているため、そのつどiCalendar形式で取得することができる。よって祝祭日データの更新は、もはや人間が手動で行うような作業ではない。

Google CalendarなのでXMLやJSONでも取得できるが、データの内容がプログラミングに向かないため、当面はiCalendar形式で取得するのが良いだろう。

koyomi.rb
require "yaml"
require "open-uri"
require "icalendar"

calendar_id = "2bk907eqjut8imoorgq1qa4olc@group.calendar.google.com"
uri = URI.join(
    "https://calendar.google.com/calendar/ical/",
    "./#{URI.encode_www_form_component(calendar_id)}/",
    "./public/basic.ics")
ical = open(uri).read
calendar = Icalendar::Calendar.parse(ical).first
results = calendar.events.map do |event|
  {
    "name" => event.summary.to_s,
    "date" => event.dtstart.to_date,
  }
end
File.open("./koyomi.yml", "w") do |file|
  YAML.dump(results.sort_by{|result| result["date"] }, file)
end
koyomi.yml
---
- name: 元日
  date: 2001-01-01
- name: 成人の日
  date: 2001-01-08
- name: 建国記念の日
  date: 2001-02-11
- name: 休日
  date: 2001-02-12
- (中略)
- name: 体育の日
  date: 2015-10-12
- name: 文化の日
  date: 2015-11-03
- name: 勤労感謝の日
  date: 2015-11-23
- name: 天皇誕生日
  date: 2015-12-23

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9