Earthquake information in Ruby

9 years ago

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