Help us understand the problem. What is going on with this article?

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

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

官報に掲載される日本の暦は、国立天文台が計算している。これをまとめたものを暦要項という。暦要項は、毎年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
hadzimme
⚙ in the machine
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした