Skip to content

Instantly share code, notes, and snippets.

@FiXato
Created March 16, 2010 18:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FiXato/334342 to your computer and use it in GitHub Desktop.
Save FiXato/334342 to your computer and use it in GitHub Desktop.
Fetches weather info from Google Weather API and Weather Underground API.
#!/usr/bin/env ruby
# Fetches weather info from Google Weather API and Weather Underground API.
# Syntax:
# ./weather_grabber.rb [location]
#
# (c) 2010 Filip H.F. "FiXato" Slagter
# Licensed under Creative Commons Attribution-Share Alike 3.0 Unported License, http://creativecommons.org/licenses/by-sa/3.0/
['rubygems','nokogiri','open-uri','yaml', 'iconv'].each {|lib| require lib}
if ARGV.size > 0
location = ARGV.join(" ")
else
location = "Bodø,Norway"
end
google_url = "http://www.google.com/ig/api?weather=#{URI.encode(location)}"
wunderground_url = "http://api.wunderground.com/auto/wui/geo/WXCurrentObXML/index.xml?query=#{URI.escape(Iconv.iconv("US-ASCII//TRANSLIT", "UTF-8", location).first)}"
weather = {
:google => Nokogiri::XML(open(google_url),google_url,"ISO-8859-1"),
:wunderground => Nokogiri::XML(open(wunderground_url),wunderground_url,"ISO-8859-1"),
}
puts "Google:"
puts google_url
# puts weather[:google].to_s
w_info = weather[:google].search('forecast_information').first
w_cond = weather[:google].search('current_conditions').first
puts [
w_info.search('city').first[:data],
'(%s)' % w_info.search('postal_code').first[:data],
w_cond.search('condition').first[:data],
w_cond.search('temp_c').first[:data],
w_cond.search('humidity').first[:data],
w_cond.search('wind_condition').first[:data],
w_cond.search('icon').first[:data]
].join(", ")
puts "Wunderground:"
puts wunderground_url
# puts weather[:wunderground].to_s
w_info = weather[:wunderground].search('current_observation/display_location').first
w_cond = weather[:wunderground].search('current_observation').first
puts [
w_info.search('full').first.text,
'(%s, %s, %s)' % [w_cond.search('observation_location/latitude').first.text,w_cond.search('observation_location/longitude').first.text,w_cond.search('observation_location/elevation').first.text],
w_cond.search('station_id').first.text,
w_cond.search('weather').first.text,
w_cond.search('temperature_string').first.text,
w_cond.search('relative_humidity').first.text,
w_cond.search('wind_string').first.text,
File.join(w_cond.search('icon_url_base').first.text,w_cond.search('icon').first.text + w_cond.search('icon_url_name').first.text)
].join(", ")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment