Pedro Assunção

Earthquake information in Ruby

Here’s a little script to get the latest earthquakes worldwide (1 hour). Sorry, Ruby purists, for not using RSS 2.0 and hpricot for a cleaner extraction of the coordinates, but my xcode installation is messed up and i can’t build any gems ;)

require 'rss/1.0'
require 'rss/2.0'
require 'rss/atom'
require 'open-uri'
require 'cgi'

source = "http://earthquake.usgs.gov/earthquakes/catalogs/1hour-M1.xml" # url or local
filecontent = "" # raw content of rss feed will be loaded here
open(source) do |s| content = s.read end
rss = RSS::Parser.parse(content, false)
rss.items.each do |item|
  data = item.summary.to_s
  # Extract raw info
  data = data.scan( /alt\=\&quot\;([^>]*)W\&quot\;/).last.first
  # Remove html escaping
  data = CGI.unescapeHTML(data).gsub("°N", "").gsub("°", "")
  # Convert into coords
  lat = data.split(' ')[0]
  lon = data.split(' ')[1]
  # Print it
  print item.updated.content, " ", item.title.content, " ", lat, " ", lon, "\n"
end

Next step: Show this on a map :D

Related:

  1. Several years of email statistics – code included Out of boredom, I decided to create a couple of...
  2. Android location provider mock So, yeah, I resumed playing around with android, this time...


Categorised as: software development, tips


Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>