You are not logged in.
Pages: 1
Hi there!
I wrote a little Ruby script to show the weather conditions or forecast on the commandline.
The script is short, so I'll just post it here in its entirety.
#!/usr/bin/ruby
require 'net/http'
require 'uri'
require 'cgi'
USERAGENT = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.1) Gecko/20060111 Firefox/1.5.0.1' #just in case the server is allergic to scripts
class Weather
def initialize
@http = Net::HTTP.new('www.weatheroffice.gc.ca', 80)
end
def currentConditions
resp, data = @http.get2('/rss/city/qc-147_e.xml', {'User-Agent' => USERAGENT}) #this is the XML for Laval, Quebec.
if(data =~ /title\>Current\sConditions:.*?\<description\>(.*?)\]\]\>\<\/description/m)
cc = CGI.unescapeHTML($1).gsub(/\<.*?\>/,"")
cc = cc.gsub(/:/, ":\033[32m") #insert ANSI characters to turn this text green
cc = cc.gsub(/\n/, "\033[0m\n") #end green text
cc = cc.gsub(/\&\#xB0\;/, "°" ) #replace HTML escape codes. use 176.chr for non-unicode terminals
cc = cc.gsub(/\°\;/, "°" ) #idem
puts cc
end
end
def forecast(day)
resp, data = @http.get2('/rss/city/qc-147_e.xml', {'User-Agent' => USERAGENT}) #this is the XML for Laval, Quebec.
if(data =~ /title\>#{day}:.*?\<description\>(.*?)\<\/description/im)
cc = CGI.unescapeHTML($1).gsub(/\<.*?\>/,"")
cc = cc.gsub(/Forecast/, "\nForecast") #timestamp on next line
puts cc
end
end
end
weather = Weather.new()
if(ARGV.size() > 0)
weather.forecast(ARGV[0])
else
weather.currentConditions
endthis will work for cities in Canada. Change the string "/rss/city/qc-147_e.xml" to match the xml for your city (go to http://www.weatheroffice.gc.ca, select your city and find the rss/xml link)
Output will typically be something like this:
armen@dusk:~$ weather
Observed at: Montr�al-Trudeau Int'l Airport 10:00 PM EDT Monday 30 May 2011
Condition: Clear
Temperature: 19.4°C
Pressure / Tendency: 102.0 kPa rising
Visibility: 24.1 km
Humidity: 63 %
Dewpoint: 12.1°C
Wind: W 8 km/h
Air Quality Health Index:
armen@dusk:~$ weather friday
Sunny. Low 11. High 22.
Forecast issued 3:45 PM EDT Monday 30 May 2011Offline
Pages: 1
Copyright © 2012 CrunchBang Linux.
Proudly powered by Debian. Hosted by Linode.
Debian is a registered trademark of Software in the Public Interest, Inc.