More playing around with earthquake data
This commit is contained in:
parent
1747424598
commit
e84f11dfba
@ -3,3 +3,4 @@ source :rubygems
|
||||
gem 'fastercsv'
|
||||
gem 'hiredis'
|
||||
gem 'redis'
|
||||
gem 'slop'
|
||||
|
@ -4,6 +4,7 @@ GEM
|
||||
fastercsv (1.5.4)
|
||||
hiredis (0.4.4)
|
||||
redis (2.2.2)
|
||||
slop (3.0.4)
|
||||
|
||||
PLATFORMS
|
||||
ruby
|
||||
@ -12,3 +13,4 @@ DEPENDENCIES
|
||||
fastercsv
|
||||
hiredis
|
||||
redis
|
||||
slop
|
||||
|
@ -9,46 +9,73 @@ require 'open-uri'
|
||||
class Earthquakes
|
||||
DATA_URL = 'http://earthquake.usgs.gov/earthquakes/catalogs/eqs7day-M1.txt'
|
||||
|
||||
attr_accessor :id_set, :region_set
|
||||
|
||||
def initialize
|
||||
@redis = Redis.new
|
||||
@id_set = 'earthquakes'
|
||||
@region_set = 'earthquake-regions'
|
||||
|
||||
@logger = Logger.new(STDOUT)
|
||||
@logger.formatter = proc do |severity, datetime, progname, msg|
|
||||
"#{datetime}: #{msg}\n"
|
||||
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
|
||||
open(DATA_URL) do |f|
|
||||
CSV.parse(f.read, :headers => true) do |row|
|
||||
@redis.multi do |r|
|
||||
@logger.info("Adding earthquake #{row['Eqid']}")
|
||||
r.sadd('earthquakes', row['Eqid'])
|
||||
row.headers.each do |header|
|
||||
r.hset("eqid:#{row['Eqid']}", header, row[header])
|
||||
end
|
||||
data = CSV.parse(
|
||||
open(DATA_URL).read, :headers => true, :header_converters => [:symbol]
|
||||
)
|
||||
|
||||
data.each do |row|
|
||||
@redis.multi do |r|
|
||||
@logger.info("Adding earthquake #{row[:eqid]}")
|
||||
key = "eqid:#{row[:eqid].downcase}"
|
||||
r.sadd(@id_set, key)
|
||||
row.headers.each do |header|
|
||||
r.hset(key, header.downcase, row[header])
|
||||
end
|
||||
|
||||
region_key = "eqregion:#{row[:region].downcase}"
|
||||
|
||||
r.sadd(region_key, key)
|
||||
r.sadd(@region_set, region_key)
|
||||
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
|
||||
|
||||
|
||||
if $0 == __FILE__
|
||||
main
|
||||
Earthquakes.new.send(ARGV.first)
|
||||
end
|
||||
|
43
redis-with-ruby/eq-info
Executable file
43
redis-with-ruby/eq-info
Executable 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
|
Loading…
Reference in New Issue
Block a user