Skip to content

Instantly share code, notes, and snippets.

@romuloceccon
Created April 13, 2011 13:17
Show Gist options
  • Save romuloceccon/917519 to your computer and use it in GitHub Desktop.
Save romuloceccon/917519 to your computer and use it in GitHub Desktop.
Gera um YAML com os resultados do Ironman Brasil (2008, 2009 e 2010)
require 'net/http'
require 'nokogiri'
$eventos = { '2008' => '159', '2009' => '280', '2010' => '396' }
unless $eventos.keys.include?(ARGV[0])
STDERR.puts "Uso: #{$0} <#{$eventos.keys.sort.join('|')}>"
exit(2)
end
def download_doc(params)
res = Net::HTTP.post_form(
URI.parse('http://www.championchipbrasil.com.br/resultados.asp?id_cliente=56'),
{ 'id_evento' => $eventos[ARGV[0]], 'acao_site' => 'consultar' }.merge(params))
unless Net::HTTPSuccess === res
puts "error (#{params.inspect}): #{res.code}"
exit(1)
end
Nokogiri::HTML(res.body)
end
page = 0
while true do
page += 1
STDERR.puts("Page #{page}")
doc = download_doc('inPage' => page.to_s)
chips = {}
doc.xpath('//a').each do |link|
if href = link.attributes['href']
if m = href.value.match(/ver_resultado\((\d*)\)/)
chips[m[1]] = link.content
end
end
end
break if chips.empty?
chips.each do |chip, nome|
doc = download_doc('id_chipcode' => chip)
puts "#{chip}:"
puts " Nome: '#{nome}'"
table = doc.xpath("//table[tr[1]/td[1]='Evento']").first
table.xpath("tr").each do |tr|
tds = tr.xpath("td")
puts " #{tds[0].content.strip}: '#{tds[1].content.strip}'"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment