Skip to content

Instantly share code, notes, and snippets.

@hitode909
Created April 3, 2010 05:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hitode909/354155 to your computer and use it in GitHub Desktop.
Save hitode909/354155 to your computer and use it in GitHub Desktop.
学年暦生成するやつ
# -*- coding: utf-8 -*-
require 'nokogiri'
require 'open-uri'
require 'icalendar'
# 立命館大学の学年暦(学部)からiCal形式のカレンダーを生成
# 使い方 ruby rits_gakubu_ial.rb > gakubu.ics
calendar = Icalendar::Calendar.new
source_url = 'http://www.ritsumei.jp/profile/a11_j.html'
source = Nokogiri(open(source_url).read)
year = source.at('h4').text.scan(/^\d+/).first.to_i rescue Time.now.year
table = source.at('table')
month = last_month = day = day_of_week = event = nil
table.search('tr')[1..-1].each{ |tr|
tds = tr.search('td').map{|node| node.text =~ /^\d+$/? node.text.to_i : node.text }
case tds.length
when 4
month, day, day_of_week, event = *tds
year +=1 if (last_month || 0) > month
when 3
day, day_of_week, event = *tds
when 1
event = *tds
end
calendar.event {
dtstart Date.new(year, month, day)
summary event
url source_url
}
last_month = month
}
puts calendar.to_ical
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment