Last active
August 29, 2015 14:12
-
-
Save samsonjs/4a672e4cf78cbc551fa9 to your computer and use it in GitHub Desktop.
scrape sunrise/sunset data
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
#!/usr/bin/env ruby -w | |
require 'faraday' | |
require 'json' | |
require 'nokogiri' | |
def sunsets_from_html(html) | |
doc = Nokogiri::HTML(html) | |
table = doc.css('table').first | |
table.css('br').each { |newline| newline.replace("\n") } | |
table.css('tr').inject({}) do |by_day, row| | |
unless row.at_css('td').text == 'Monday' | |
row.css('td').each do |cell| | |
text = cell.text | |
# nbsp | |
unless text.each_byte.to_a == [194, 160] | |
values = text.split(/\n+/) | |
day = values.shift.to_i | |
values.each { |v| v.sub!(/^[^\d]+/, '') } | |
sunrise, morning, sunset, night = *values | |
by_day[day] = { | |
sunrise: sunrise, | |
morning: morning, | |
sunset: sunset, | |
night: night, | |
} | |
end | |
end | |
end | |
by_day | |
end | |
end | |
conn = Faraday.new(url: 'http://www.sunrisesunset.com') | |
sunsets_by_month = {} | |
1.upto(12) do |month| | |
params = { | |
city_name: "Victoria, British Columbia, Canada", | |
latitude: "48.4286", | |
longitude: "123.3656", | |
latitude_n_s: "N", | |
longitude_e_w: "W", | |
timezone: -8, | |
dst_type: 1, | |
btop: nil, | |
back: "Canada", | |
month: month, | |
year: 2015, | |
want_twi_civ: 1, | |
time_type: 1, | |
wsom: 1 | |
} | |
response = conn.post('/calendar.asp', params) | |
html = response.body | |
sunsets_by_month[month] = sunsets_from_html(html) | |
end | |
puts JSON.generate(sunsets: sunsets_by_month) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment