やりたいこと
- IT勉強会カレンダーから北海道に関連する情報だけを抜き出す
- 抜き出したイベントを別のカレンダーにインポートできるようにする
使ったもの
- icalendarライブラリ
- open-uri
- Ruby逆引きレシピ
- icalendar
- ファイル操作
ソース
STEP1 iCalendarライブラリでicalファイルを作ってみる
# gem install icalendar
require 'rubygems' require 'icalendar' // 日本語を取り扱うので必要 $KCODE = 'u' // 新しいカレンダーを作る cal = Icalendar::Calendar.new // 新しいイベントを作る event = Icalendar::Event.new event.dtstart = Date.new(2010,12,18) event.dtend = Date.new(2010,12,18) event.summary = '道民部忘年会' // カレンダーにイベントを追加する cal.add_event(event) // ファイルに出力する output_file = File.open("sample.ical", "w") output_file.write(cal.to_ical) output_file.close
日本語とソースコードで言ってることが全く同じですね。
作ったicalファイルはちゃんとGoogleCalendarにインポートできました!
STEP2 GoogleCalendarのical形式ファイルから北海道に関連するイベントだけを抜き出す
require 'rubygems' require 'icalendar' require 'open-uri' $KCODE = 'u' hokkaido_cal = Icalendar::Calendar.new open('http://www.google.com/calendar/ical/fvijvohm91uifvd9hratehf65k%40group.calendar.google.com/public/basic.ics') {|f| cal = Icalendar.parse(f).first cal.events.each { |event| if event.summary.match("北海道") and Date::today <= event.dtstart then hokkaido_cal.add_event(event) end } } output = File.open("HokkaidoIT.ical", "w") output.write(hokkaido_cal.to_ical) output.close
実行
ruby hokkaidoItCal.rb
IT勉強会カレンダーのicalファイルのサイズがでかいのでちょっと時間がかかりましたが
無事にファイルができました!
個人カレンダーにインポートして使っています!
これからやりたいこと
- 差分だけ取得したいなぁ
- さらに、それを自動化したいなぁ
- 決まったカレンダーに同期させたいなぁ
差分の取得ができるようになったら、インポートしたカレンダーを公開して
北海道の勉強会情報をもっとわかりやすく共有できたらな、と思う。
Ruby 逆引きレシピ すぐに美味しいサンプル&テクニック 232 (PROGRAMMER’S RECIPE)
posted with amazlet at 10.12.19
島田 浩二 設樂 洋爾 村田 賢太 前田 智樹 谷口 文威
翔泳社
売り上げランキング: 67909
翔泳社
売り上げランキング: 67909
One comment: