Feel Physics Backyard

HoloLensの出張授業をする会社で、教材を開発しています

090628-WindowsでGoogleカレンダーから予定を取得するRubyスクリプト

新しい記事があります。

 

リンク: 090629-Windowsで今日の予定をGoogleカレンダーから取得し表示する: Non-programmer's Ruby in Pocket.

 

 

スクリプトを実行すると、その日の予定までの残り時間が示される。

 


 

090628calendar

 

 

Googleカレンダーから今日明日の予定を取得するRubyスクリプトを書いてみました。といっても、gcalapiというライブラリを使わせてもらっているので、大したことはやっていません。

$KCODE = "UTF-8"

require "kconv"

require "gcalapi"

 

# アカウントメールアドレス

mail = "xxxxxxxx@gmail.com"

# パスワード

pass = "xxxxxxxx"

# Googleカレンダーの「カレンダー設定」画面から取得した非公開URL

feed = "http://www.google.com/calendar/feeds/xxxxxxxx%40gmail.com/private-xxxxxxxxxxxxxxxxxxxxxxxxxxx/basic"

 

srv = GoogleCalendar::Service.new(mail, pass)

cal = GoogleCalendar::Calendar::new(srv, feed)

events = cal.events

 

# 今日と明日の日付の文字列を作っておく

today = Time.now.strftime("%Y/%m/%d")

tomorrow = (Time.now + 24*60*60).strftime("%Y/%m/%d")

 

# 各予定について・・・

events.each do |event|

title = event.title

 

# 時刻指定されている場合

if /^(\w*): (\d{4}\/\d{2}\/\d{2}) (\d{2}:\d{2})~(\d{2}:\d{2}).*/ =~ event.desc

type = Regexp.last_match(1).to_s

date = Regexp.last_match(2).to_s

starttime = Regexp.last_match(3).to_s

endtime = Regexp.last_match(4).to_s

 

# 時刻指定されていない場合

elsif /^(\w*): (\d{4}\/\d{2}\/\d{2})~(\d{4}\/\d{2}\/\d{2}).*/ =~ event.desc

type = Regexp.last_match(1).to_s

starttime = Regexp.last_match(2).to_s

endtime = Regexp.last_match(3).to_s

 

# 開始日のみの場合

elsif /^(\w*): (\d{4}\/\d{2}\/\d{2}).*/ =~ event.desc

type = Regexp.last_match(1).to_s

starttime = Regexp.last_match(2).to_s

else

raise "unexpected format: " + event.desc

end

 

# 今日か明日の予定を表示する

# WindowsなのでSJISにする

if date == today || starttime == today || date == tomorrow || starttime == tomorrow

puts title.tosjis

puts date.tosjis if date

puts "開始:" + starttime.tosjis

puts "終了:" + endtime.tosjis if endtime

end

end