Created
April 19, 2016 12:38
-
-
Save unak/49e8452ad20d8fcc0dbcfcdfd6c421f6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 "open-uri" | |
URL = 'http://typhoon.yahoo.co.jp/weather/jp/earthquake/list/?sort=1&key=1&b=' | |
START = Time.local(2016,4,14,21,15,59) | |
GRAPH = 'test.tsv' | |
HEADERS = %w[ | |
熊本県天草・芦北地方 | |
熊本県熊本地方 | |
熊本県阿蘇地方 | |
大分県西部 | |
大分県中部 | |
伊予灘 | |
徳島県北部 | |
] | |
def HEADERS.===(oth) | |
self.find{|e| />#{e}</ =~ oth} | |
end | |
class BreakLoop < RuntimeError; end | |
class ContinueLoop < RuntimeError; end | |
quakes = [] | |
base = 1 | |
begin | |
open(URL + base.to_s) do |f| | |
time = idx = nil | |
f.each_line do |line| | |
line.chomp! | |
case line | |
when />(\d{4})年(\d{1,2})月(\d{1,2})日 (\d{1,2})時(\d{1,2})分(ごろ)?</ | |
if $6 | |
time = Time.local($1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i, 0) | |
if time < START | |
raise BreakLoop | |
end | |
end | |
when HEADERS | |
idx = HEADERS.index{|e| />#{e}</ =~ line} + 1 | |
when />(\d+\.\d+)</ | |
if time | |
ary = [time] | |
ary[idx] = $1.to_f | |
quakes.unshift(ary) | |
end | |
else | |
time = nil | |
end | |
end | |
end | |
base += 100 | |
raise ContinueLoop | |
rescue ContinueLoop | |
retry | |
rescue BreakLoop | |
end | |
open(GRAPH, 'w:cp932') do |graph| | |
graph.puts [' ', *HEADERS].join("\t") | |
graph.puts quakes.map{|(d,*m)| "#{d.strftime('%Y/%m/%d %H:%M')}\t#{m.join("\t")}"} | |
end | |
require 'win32ole' | |
excel = WIN32OLE.new('Excel.Application') | |
Excel = Module.new | |
WIN32OLE.const_load(excel, Excel) | |
#excel.Visible = true # debug | |
begin | |
book = excel.Workbooks.Open(File.expand_path(GRAPH)) | |
sheet = book.ActiveSheet | |
Shape = Module.new | |
WIN32OLE.const_load(sheet.Shapes, Shape) | |
sheet.Shapes.AddChart2(240, Shape::XlXYScatter).Select | |
shape = sheet.Shapes.Item(sheet.Shapes.Count) | |
shape.Left = 0 | |
shape.Top = 0 | |
shape.Width = 480 | |
shape.Height = 360 | |
chart = book.ActiveChart | |
chart.SetSourceData('Source' => sheet.Range("$A:$#{('A'.ord + HEADERS.size).chr}")) | |
chart.ChartColor = 10 | |
chart.ChartTitle.Text = "震源別地震規模推移" | |
excel.Visible = true | |
rescue WIN32OLERuntimeError | |
excel.Quit | |
$!.message.force_encoding('cp932') | |
raise $! | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ちうい:raiseとretryでループを継続したり中断したりしてるのをよい子は真似してはいけません。