#!/usr/bin/ruby
require 'rubygems'
require 'hpricot'
require 'json'
require 'open-uri'

all_cameras = Hash.new
domain = "http://flickr.com"

doc = Hpricot(open(domain+"/cameras/brands"))
# doc = Hpricot(open("/Users/blech/Desktop/brands.html"))

manufacturers = (doc/"td.clTxt/h4")
manufacturers.each do |manufacturer|
  link = domain + manufacturer.at("a")['href']
  make = manufacturer.at("a").inner_html
  
  puts link
  puts make

  newdoc = Hpricot(open(link))
#   newdoc = Hpricot(open("/Users/blech/Desktop/apple.html"))
    
  models = (newdoc/"td.cfM")
  cameras = Hash.new
  models.each do |model|
    model_link = model.at("a")["href"]
    model_ref  = model_link[9..-2]
    model_name = model.at("a").inner_html
    
    cameras[model_name] = model_ref
  end
  all_cameras[make] = cameras
end

f = File.new("/Users/blech/Desktop/cameras.json", "w")
f.puts all_cameras.to_json
f.close

puts "Done"

__END__
