使用Ruby从公共Google日历中获取数据

时间:2013-01-18 21:29:00

标签: ruby google-calendar-api

我想从网站的公共谷歌日历中下载并解析所有活动数据,最好的方法是什么?我正在考虑下载ics文件或获取xml数据并自行解析。我已经研究过google api,但如果我想要做的就是阅读数据,那看起来就不那么复杂了。我是初学者,通常使用API​​和编程,因此我无法浏览所有文档。他们没有提供很多有用的例子。

1 个答案:

答案 0 :(得分:5)

这样的事情怎么样:

require 'ri_cal'
require 'open-uri'

components = nil
open("https://www.google.com/calendar/ical/ocs.events%40gmail.com/public/basic.ics") do |cal|
  components = RiCal.parse(cal)
end

components.each do |calendar|

  calendar.events.each do |event|
    puts "#{event.summary} starts at: #{event.dtstart} and ends at #{event.dtend}"
  end

end

您需要安装ri_gem。

更新:使用iCalendar

require 'icalendar'
require 'open-uri'

calendars = nil
open("https://www.google.com/calendar/ical/ocs.events%40gmail.com/public/basic.ics") do |cal|
  #calendars = RiCal.parse(cal)
  calendars = Icalendar.parse(cal)
end

calendars.each do |calendar|

  calendar.events.each do |event|
    puts "#{event.summary} starts at: #{event.dtstart} and ends at #{event.dtend}"
  end

end