Created
October 23, 2011 11:37
-
-
Save seancaffery/1307265 to your computer and use it in GitHub Desktop.
Quick script to gather results from the recent 10KM run, anonymize, and output to CSV.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'hpricot' | |
require 'net/http' | |
require 'uri' | |
require 'cgi' | |
require 'csv' | |
def do_request(path) | |
url = URI.parse(path) | |
req = Net::HTTP::Get.new("#{url.path}?#{url.query}") | |
puts "Sending request to #{url}" | |
nhttp = Net::HTTP.new(url.host, url.port) | |
nhttp.use_ssl = true | |
nhttp.start do |http| | |
http.read_timeout = 300 | |
http.request(req) | |
end | |
end | |
(1..279).each do |i| | |
req = do_request("https://secure.eventdesq.com/index.cfm?fuseaction=results&RaceID=9&EventDesqID=5&OrgID=1142i&Page=#{i}") | |
body = req.body | |
table = Hpricot.parse(body).search("table[@class*=listing]") | |
f = File.open("res/#{i}.html", "w+") | |
f.write table | |
f.close | |
end | |
all_rows = [] | |
(1..279).each do |i| | |
file = File.open("res/#{i}.html") | |
table = Hpricot.parse file.read | |
file.close | |
rows = table.search "tr" | |
d = rows.collect do |row| | |
cols = row.search("td") | |
data = cols.collect{|d| d.inner_html} | |
end | |
data = d[1..20].collect{|a| [a[3], a[4], a[5], a[6]] } | |
data.each{|b| all_rows << b } | |
end | |
CSV.open("output.csv", "w+") do |csv| | |
all_rows.each{|row| csv << row } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment