Skip to content

Instantly share code, notes, and snippets.

@daz
Last active December 14, 2015 14:59
Show Gist options
  • Save daz/5104840 to your computer and use it in GitHub Desktop.
Save daz/5104840 to your computer and use it in GitHub Desktop.
Formula1 calendar script. P1-3, qualifying. and race start times for the year
# Dependencies
#
# $ gem install icalendar
#
# Usage
#
# $ git clone https://gist.github.com/5104840.git f1calendar
# $ ruby f1calendar/f1calendar.rb
#
# FORMULA 1 GRANDE PRÊMIO DO BRASIL 2015 - Sao Paulo - 2015-11-13 12:00:00 UTC - Practice 1
# FORMULA 1 GRANDE PRÊMIO DO BRASIL 2015 - Sao Paulo - 2015-11-13 16:00:00 UTC - Practice 2
# FORMULA 1 GRANDE PRÊMIO DO BRASIL 2015 - Sao Paulo - 2015-11-14 13:00:00 UTC - Practice 3
# FORMULA 1 GRANDE PRÊMIO DO BRASIL 2015 - Sao Paulo - 2015-11-14 16:00:00 UTC - Qualifying
# FORMULA 1 GRANDE PRÊMIO DO BRASIL 2015 - Sao Paulo - 2015-11-15 16:00:00 UTC - Race
# 2015 FORMULA 1 ETIHAD AIRWAYS ABU DHABI GRAND PRIX - Yas Marina - 2015-11-27 09:00:00 UTC - Practice 1
# 2015 FORMULA 1 ETIHAD AIRWAYS ABU DHABI GRAND PRIX - Yas Marina - 2015-11-27 13:00:00 UTC - Practice 2
# 2015 FORMULA 1 ETIHAD AIRWAYS ABU DHABI GRAND PRIX - Yas Marina - 2015-11-28 10:00:00 UTC - Practice 3
# 2015 FORMULA 1 ETIHAD AIRWAYS ABU DHABI GRAND PRIX - Yas Marina - 2015-11-28 13:00:00 UTC - Qualifying
# 2015 FORMULA 1 ETIHAD AIRWAYS ABU DHABI GRAND PRIX - Yas Marina - 2015-11-29 13:00:00 UTC - Race
class F1CalendarEvent
attr_reader :kind, :summary, :city, :start_time
def initialize(cal_event)
return nil if cal_event.sequence == 0
@city = cal_event.location.strip
self.summary = cal_event.summary
@start_time = DateTime.parse(cal_event.dtstart.to_s).to_time.utc
end
def kind_name
kind.to_s.capitalize.gsub(/\d/, ' \0')
end
def summary=(summary)
kind_regex = summary.match(/(.+):\s/)
if kind_regex
@kind = kind_regex[1].gsub(/\s+/, '').downcase.to_sym
@summary = summary.gsub(kind_regex[0], '')
.gsub(/s+/, ' ').strip
else
@kind = :race
@summary = summary.strip
end
end
def to_s
"#{summary} - #{city} - #{start_time} - #{kind_name}"
end
end
require 'net/http'
require 'uri'
require 'icalendar'
# Open this in Safari http://cal.f1.com/calendar
# Click subscribe to all sessions
URL = "http://formula1.calreply.net/calendar/calendar?id=NzIwYTFlZDMtNDk4Yy00Y2M2LWE0ZmQtMzNhZTMzZWQxYWRkfDFlM2M5N2Q0LTFiNzMtNGMwZi05ZjlmLTk4ZmYwNDRlMzhmZHx3fHx8fHxNb3ppbGxhLzUuMCAoTWFjaW50b3NoOyBJbnRlbCBNYWMgT1MgWCAxMF8xMF8zKSBBcHBsZVdlYktpdC82MDAuNS4xNyAoS0hUTUwsIGxpa2UgR2Vja28pIFZlcnNpb24vOC4wLjUgU2FmYXJpLzYwMC41LjE3fHV0bV9zb3VyY2U9fHV0bV9tZWRpdW09fHV0bV9jYW1wYWlnbj18dXRtX2NvbnRlbnQ9fHRhZ0lkPTFlM2M5N2Q0LTFiNzMtNGMwZi05ZjlmLTk4ZmYwNDRlMzhmZA=="
uri = URI(URL)
body = Net::HTTP.get(uri)
cal = Icalendar.parse(body).first
cal.events.each do |event|
puts F1CalendarEvent.new(event).to_s
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment