sohooo (owner)

Revisions

gist: 91819 Download_button fork
public
Description:
retrieve tournament info from ATP homepage to create ical
Public Clone URL: git://gist.github.com/91819.git
Embed All Files: show embed
ATP calendar #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/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")