Skip to content

Instantly share code, notes, and snippets.

@sohooo
Created April 8, 2009 15:18
Show Gist options
  • Save sohooo/91819 to your computer and use it in GitHub Desktop.
Save sohooo/91819 to your computer and use it in GitHub Desktop.
retrieve tournament info from ATP homepage to create ical
#!/usr/bin/env ruby
require 'rubygems'
require 'hpricot'
require 'icalendar'
require 'date'
class ATPCalendar
def initialize(source)
@source = source
if @source =~ /^http/ # if source = website
@hp = Hpricot(open(@source))
else # if source = file
@hp = open(@source) { |f| Hpricot(f) }
end
@tourneys = prettify_output(parse_resource(@hp))
end
def parse_resource(doc)
@rows = []
# rows array will contain the raw data of evey event
(doc/"div.maincolwide//table[4]//tr").each do |row|
cells = []
(row/"td").each do |cell|
next if cell.inner_text == "" # skip empty cells
next if cell.inner_text =~ /\302\240/
raw = cell.inner_text # we just want the text
raw.gsub!(/\t/,'') # remove funny formatting
#raw.gsub!(/\\/,'') # remove funny formatting
raw.strip!
cells << raw
end
@rows << cells unless cells.size == 0
end
@rows
end
def build_ical(filename)
cal = Icalendar::Calendar.new
@tourneys.each do |t|
event = cal.event
event.start = t[:date]
event.summary = t[:tourney]
event.description = "Surface: #{t[:surface]}; Winners: #{t[:winners]}"
event.location = t[:location]
cal.add_event(event)
end
cal_file = File.new(File.join(Dir.getwd, filename), "w+")
cal_file.puts cal.to_ical
end
private
# create nicely formatted hashes out
# of each row of tournament info
def prettify_output(rows)
rows.map do |date, location, surface, price, tickets, winners|
location, tourney = location.split("\n")
{
:date => Date.parse(date),
:location => location,
:tourney => tourney.sub(/(\w)ATP/,"\\1\nATP"),
:surface => surface,
:price => price,
:tickets => tickets,
:winners => winners || "no info available"
}
end
end
end
calendar = ATPCalendar.new("atp_cal2009.html")
calendar.build_ical("atp_tourneys_2009.ics")
@nyxee
Copy link

nyxee commented Jan 5, 2022

How can I use this to generate the atp ical for 2022 please.

@sohooo
Copy link
Author

sohooo commented Jan 8, 2022

Well, I just looked and the ATP still doesn't provide a tournament calendar in ical format. The code above (from 2009!) was based on the tournament listing on their website. Since then, the website changed quite a bit and there's also lots of info missing (only first half of 2022, no prize money).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment