Skip to content

Instantly share code, notes, and snippets.

@cockscomb
Created September 29, 2011 09:31
Show Gist options
  • Save cockscomb/1250390 to your computer and use it in GitHub Desktop.
Save cockscomb/1250390 to your computer and use it in GitHub Desktop.
# encoding: UTF-8
module Choseki
require 'nokogiri'
require 'open-uri'
require 'date'
HEADER = ['Time', 'Height']
def get_monthly_data(area, date)
time = []
height = []
url = "http://www1.kaiho.mlit.go.jp/KANKYO/TIDE/cgi-bin/tide_pred.cgi?area=#{area}&back=./&year=#{date.year}&month=#{date.month}&day=#{date.day}&btn=ForAreaWindow_Japanese"
open(url) do |data|
doc = Nokogiri::HTML(data)
count = 0
doc.xpath('//tr').each do |tr|
row = tr.xpath('td[@width="40"]').map { |td| td.content.to_i }
if count % 2 == 1
time += row.map! { |hour| DateTime.new(date.year, date.month, date.day, hour, 0, 0, Rational(9, 24)) }
else
height += row
end
count += 1
end
alist = [time, height].transpose
Hash[alist.flatten(0)]
end
end
module_function :get_monthly_data
end
require 'date'
require 'csv'
area = 166
first_day = Date.new(2011, 8, 1)
days = 1
file_name = 'akkeshi_2011_8.csv'
dates = []
days.times do |i|
dates << first_day + i
end
CSV.open(file_name, 'w:UTF-8', headers: true) do |csv|
csv << Choseki::HEADER
dates.each do |date|
Choseki.get_monthly_data(area, date).each do |key, value|
csv << [key, value]
end
end
sleep 1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment