Created
May 9, 2012 16:59
-
-
Save mcamiano/2646701 to your computer and use it in GitHub Desktop.
Weather Underground Ruby Sample; Show current temps in F, download radar animation gif and launch Safari to view it
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# See http://api.wunderground.com/weather/api/d/documentation.html | |
require 'open-uri' | |
require 'json' | |
zip=ARGV[0]||27571 | |
apikey=YOURWUGAPIKEYHERE | |
open("http://api.wunderground.com/api/#{apikey}/conditions/forecast/q/#{zip}.json") do |f| | |
json_string = f.read | |
parsed_json = JSON.parse(json_string) | |
location = parsed_json['current_observation']['display_location']['city'] | |
temp_f = parsed_json['current_observation']['temp_f'] | |
print "Current temperature in #{location} is: #{temp_f} deg F\n" | |
end | |
# Radar api: | |
open("http://api.wunderground.com/api/#{apikey}/animatedradar/q/#{zip}.gif?newmaps=1&timelabel=1&timelabel.y=10&num=5&delay=50") do |f| | |
img_content = f.read | |
begin | |
fpath = "/tmp/wunderground_#{rand(100000)}.gif" | |
end while File.exist? fpath | |
nf = File.new(fpath,"w") | |
trap("EXIT") { File.unlink(fpath); exit } | |
nf.write(img_content) | |
nf.close | |
`open -a /Applications/Safari.app "#{fpath}"` | |
sleep 3 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment