Created
December 13, 2010 06:47
-
-
Save DeMarko/738732 to your computer and use it in GitHub Desktop.
short script that uses Yahoo!'s Weather API to retrieve the weather conditions for the given location
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 | |
#--------------------------------- | |
# Yahoo weather app using YQL/JSON | |
#--------------------------------- | |
# based off of https://www.tropo.com/docs/scripting/t_ruby-yahooweather.htm | |
require 'rubygems' | |
require 'open-uri' | |
require 'json' | |
#Set the URI and our YQL select statement, then encode as a URI | |
yahoo_url = 'http://query.yahooapis.com/v1/public/yql?format=json&env=store://datatables.org/alltableswithkeys&q=' | |
yql_statement = 'select * from weather.bylocation where location="' + ARGV.join(" ") + '"' | |
url = URI.encode(yahoo_url + yql_statement) | |
#Fetch the JSON from the YQL API and convert the resulting | |
#JSON data to a Ruby hash | |
weather_data = JSON.parse(open(url).read) | |
#Get the relevant weather channel details and throw them into a hash | |
weather_results = weather_data["query"]["results"]["weather"]["rss"]["channel"] | |
#Print the results | |
#The sleep calls are present because some IRC clients will display messages by the timestamp received | |
#this causes the messages to look out of order | |
if weather_results["description"] != "Yahoo! Weather Error" | |
puts "Yahoo! weather results for #{weather_results["location"]["city"]}, #{weather_results["location"]["country"]}" | |
sleep 1 | |
puts "The temperature (including wind chill) is #{weather_results["wind"]["chill"]} degrees #{weather_results["units"]["temperature"]}, " + | |
"the wind speed is #{weather_results["wind"]["speed"]} #{weather_results["units"]["speed"]}" | |
sleep 1 | |
puts "The forecast is #{weather_results["item"]["forecast"][0]["text"]}, " + | |
"with a high of #{weather_results["item"]["forecast"][0]["high"]} degrees #{weather_results["units"]["temperature"]}, " + | |
"and a low of #{weather_results["item"]["forecast"][0]["low"]} degrees #{weather_results["units"]["temperature"]}." | |
else | |
puts "Invalid Location or Location Not Found. Please try again." | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment