Skip to content

Instantly share code, notes, and snippets.

@tumf
Created April 13, 2011 13:52
Show Gist options
  • Select an option

  • Save tumf/917585 to your computer and use it in GitHub Desktop.

Select an option

Save tumf/917585 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
=begin
過去24時間以内に特定の地域の地震が10回超えたらアラート出す
crontabへの設置方法
-----------------
*/15 * * * * exec setlock -nX /tmp/eqalert.lock cronlog -- ruby /path/to/eqalert.rb 2>&1
* daemontools http://cr.yp.to/daemontools.html (setlock)
* cronlog https://github.com/kazuho/kaztools/
=end
URL="http://tenki.jp/earthquake/entries?p=%d"
require 'rubygems'
require 'mechanize'
# @last 現在からの秒数
def fetch(last,max = 200)
data = []
Mechanize.new { |agent|
(1..max).each { |n|
agent.get(URL % n) { |page|
page.search("table#seismicInfoEntries tr").each { |tr|
item = {}
# p tr
el = tr.at("td[@class='tdFirst']")
next unless el
h = {}
if /(\d+)年(\d+)月(\d+)日/ =~ el.inner_text
h[:year] = $1
h[:month] = $2
h[:day] = $3
end
el = tr.at("td[3]")
if /(\d+)時(\d+)分/ =~ el.inner_text
h[:hour] = $1
h[:min] = $2
end
item[:time] = Time.mktime(h[:year],h[:month],h[:day],h[:hour],h[:min])
el = tr.at("td[4]")
item[:area] = el.inner_text
el = tr.at("td[5]")
item[:power] = el.inner_text
el = tr.at("td[2]/a")
if /detail-(\d+).html/ =~ el.attr("href")
item[:id] = $1
end
return data if (Time.now - last) > item[:time]
data << item
# puts e
}
}
}
}
data
end
def alert
data = fetch(24*60*60)
areas = {}
data.each { |item|
areas[item[:area]] ||= 0
areas[item[:area]] += 1
}
areas.keys.sort.each { |area|
puts "%s %d" % [area,areas[area]]
}
areas.max { |a, b| a[1] <=> b[1] }[1]
end
exit 255 if alert > 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment