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
#!/usr/bin/env ruby

%w|rubygems hpricot active_record open-uri irb|.each{|lib| require lib}

class Parse
  attr_accessor :wines
  
  def initialize
    username  = "*****"
    password  = "*****"
    base      = "http://www.cellartracker.com/xlquery.asp?table=%type%&User=#{username}&Password=#{password}"
    
    @wines = {}

    # full wine data
    ct = Hpricot::parse(open(base.gsub(/%type%/, "List")))
    ct.search("//tr[@class='properties']").collect do |row|
      iwine = row.at("td[1]").innerHTML
      @wines[iwine] = {
        :iwine        => row.at("td[1]").innerHTML,
        :qty          => row.at("td[2]").innerHTML,
        :size         => row.at("td[4]").innerHTML,
        :price        => row.at("td[5]").innerHTML,
        :vintage      => row.at("td[10]").innerHTML,
        :name         => row.at("td[11]").innerHTML,
        :country      => row.at("td[14]").innerHTML,
        :region       => row.at("td[15]").innerHTML,
        :sub_region   => row.at("td[16]").innerHTML,
        :appllation   => row.at("td[17]").innerHTML
      }
    end
    
    # Ratings and reviews
    ct = Hpricot::parse(open(base.gsub(/%type%/, "Notes")))
    ct.search("//tr[@class='properties']").collect do |row|
      iwine = row.at("td[1]").innerHTML
      
      if @wines[iwine]
        puts "Doing #{iwine}...#{row.at("td[9]").innerHTML}"
        @wines[iwine].merge!({
          :rating       => row.at("td[9]").innerHTML,
          :taste_date   => row.at("td[7]").innerHTML,
          :notes        => row.at("td[10]").innerHTML
        })
      end
    end
  end
end

IRB.start if __FILE__ == $0