More playing around with earthquake data

This commit is contained in:
Dan Buch 2012-02-28 23:35:36 -05:00
parent 1747424598
commit e84f11dfba
4 changed files with 98 additions and 25 deletions

View File

@ -3,3 +3,4 @@ source :rubygems
gem 'fastercsv' gem 'fastercsv'
gem 'hiredis' gem 'hiredis'
gem 'redis' gem 'redis'
gem 'slop'

View File

@ -4,6 +4,7 @@ GEM
fastercsv (1.5.4) fastercsv (1.5.4)
hiredis (0.4.4) hiredis (0.4.4)
redis (2.2.2) redis (2.2.2)
slop (3.0.4)
PLATFORMS PLATFORMS
ruby ruby
@ -12,3 +13,4 @@ DEPENDENCIES
fastercsv fastercsv
hiredis hiredis
redis redis
slop

View File

@ -9,46 +9,73 @@ require 'open-uri'
class Earthquakes class Earthquakes
DATA_URL = 'http://earthquake.usgs.gov/earthquakes/catalogs/eqs7day-M1.txt' DATA_URL = 'http://earthquake.usgs.gov/earthquakes/catalogs/eqs7day-M1.txt'
attr_accessor :id_set, :region_set
def initialize def initialize
@redis = Redis.new @redis = Redis.new
@id_set = 'earthquakes'
@region_set = 'earthquake-regions'
@logger = Logger.new(STDOUT) @logger = Logger.new(STDOUT)
@logger.formatter = proc do |severity, datetime, progname, msg| @logger.formatter = proc do |severity, datetime, progname, msg|
"#{datetime}: #{msg}\n" "#{datetime}: #{msg}\n"
end end
end end
def get_info(earthquake_id)
if not /^eqid:/.match(earthquake_id)
earthquake_id = "eqid:#{earthquake_id}"
end
@redis.hgetall(earthquake_id)
end
def all_ids
@redis.smembers(@id_set).map { |i| i.gsub(/^eqid:/, '') }
end
def all_regions
@redis.smembers(@region_set).map { |r| r.gsub(/^eqregion:/, '') }
end
def drop_all
@logger.info('Removing data for each earthquake')
all_ids.each do |i|
@logger.info("Removing data for earthquake #{i.gsub(/^eqid:/, '')}")
@redis.del(i)
end
@logger.info('Removing earthquake id set')
@redis.del(@id_set)
@logger.info('Removing earthquake region set')
@redis.del(@region_set)
end
def load_data def load_data
open(DATA_URL) do |f| data = CSV.parse(
CSV.parse(f.read, :headers => true) do |row| open(DATA_URL).read, :headers => true, :header_converters => [:symbol]
@redis.multi do |r| )
@logger.info("Adding earthquake #{row['Eqid']}")
r.sadd('earthquakes', row['Eqid']) data.each do |row|
row.headers.each do |header| @redis.multi do |r|
r.hset("eqid:#{row['Eqid']}", header, row[header]) @logger.info("Adding earthquake #{row[:eqid]}")
end key = "eqid:#{row[:eqid].downcase}"
r.sadd(@id_set, key)
row.headers.each do |header|
r.hset(key, header.downcase, row[header])
end end
region_key = "eqregion:#{row[:region].downcase}"
r.sadd(region_key, key)
r.sadd(@region_set, region_key)
end end
end end
end end
def dump_data
@redis.smembers('earthquakes').each do |eqid|
@logger.info(
"Earthquake #{eqid} 'Src': #{
@redis.hget("eqid:#{eqid}", 'Src')}"
)
end
end
end
def main
eq = Earthquakes.new
eq.load_data
eq.dump_data
end end
if $0 == __FILE__ if $0 == __FILE__
main Earthquakes.new.send(ARGV.first)
end end

43
redis-with-ruby/eq-info Executable file
View File

@ -0,0 +1,43 @@
#!/usr/bin/env ruby
require 'slop'
require File.expand_path('../earthquakes', __FILE__)
def main
opts = Slop.parse(:help => true, :strict => true) do
banner "#{File.basename(__FILE__)} [options]"
on :l, :listall, 'List all available earthquake ids.'
on :r, :listall_by_region, 'List available earthquakes by region'
#on :l, :'list-by-region', 'List available earthquakes by region'
#on :L, :'list-by-magnitude', 'List available earthquakes by magnitude'
#on :r, :region=, 'Show earthquakes for region'
#on :m, :magnitude=, 'Show earthquakes of magnitude'
on :i, :eqid=, 'Show all available info for earthquake with id.'
end
earthquakes = Earthquakes.new
if opts.listall?
earthquakes.all_ids.each { |i| puts i }
return 0
end
if opts.listall_by_region?
earthquakes.all_regions.each { |r| puts r }
return 0
end
puts "Earthquake with id of #{opts[:eqid]}"
earthquakes.get_info(opts[:eqid]).each do |key, value|
puts " #{key}: #{value}"
end
return 0
end
if $0 == __FILE__
exit(main)
end