2 # Forecast plugin for rbot
3 # by MrChucho (mrchucho@mrchucho.net)
4 # Copyright (C) 2006 Ralph M. Churchill
6 require 'soap/wsdlDriver'
8 require 'rexml/document'
14 # Determine the latitude and longitude of a location. City, State and/or ZIP
16 # [+return+] latitude,longitude
19 url="http://api.local.yahoo.com/MapsService/V1/geocode?appid=mrchucho_rbot_weather&location=#{loc}"
23 results = (REXML::Document.new xmldoc).root
24 lat = results.elements["//Latitude/text()"].to_s
25 long = results.elements["//Longitude/text()"].to_s
30 return lat.to_f,long.to_f
35 WSDL_URI="http://www.nws.noaa.gov/forecasts/xml/SOAP_server/ndfdXMLserver.php?wsdl"
36 def initialize(lat,long)
38 # this extra step is for backward/forward compatibility
39 factory = SOAP::WSDLDriverFactory.new(WSDL_URI)
40 @forecaster=factory.respond_to?(:create_rpc_driver) ?
41 factory.create_rpc_driver : factory.create_driver
44 return parse(retrieve),Time.new
48 forecast = @forecaster.NDFDgenByDay(
49 @lat,@long,Time.now.strftime("%Y-%m-%d"),2,"24 hourly")
50 (REXML::Document.new(forecast)).root
55 d = (day==1) ? 'Today' : 'Tomorrow'
56 hi = xml.elements["//temperature[@type='maximum']/value[#{day}]/text()"]
57 lo = xml.elements["//temperature[@type='minimum']/value[#{day}]/text()"]
58 w = xml.elements["//weather/weather-conditions[#{day}]/@weather-summary"]
59 precip_am = xml.elements["//probability-of-precipitation/value[#{day*2-1}]/text()"]
60 precip_pm = xml.elements["//probability-of-precipitation/value[#{day*2}]/text()"]
61 msg += "#{d}: Hi #{hi} Lo #{lo}, #{w}. Precip: AM #{precip_am}% PM #{precip_pm}%\n"
67 class ForecastPlugin < Plugin
68 USAGE='forecast <location> => show the 2-day forecast for a location. Location can be any combination of City, State, Country and ZIP'
69 def help(plugin,topic="")
72 def usage(m,params={})
77 # this plugin only wants to store strings
86 @forecast_cache = Hash.new
89 def forecast(m,params)
90 if params[:location] and params[:location].any?
91 loc = params[:location].join
92 @registry[m.sourcenick] = loc
95 if @registry.has_key?(m.sourcenick) then
96 loc = @registry[m.sourcenick]
99 m.reply "Please specifiy the City, State or ZIP"
104 def get_forecast(m,loc)
106 if @forecast_cache.has_key?(loc) and
107 Time.new - @forecast_cache[loc][:date] < 3600
108 forecast = @forecast_cache[loc][:forecast]
109 forecast_date = @forecast_cache[loc][:date]
113 f = Forecast.new(*l.get_lat_long(loc))
114 forecast,forecast_date = f.forecast
121 @forecast_cache[loc] = {}
122 @forecast_cache[loc][:forecast] = forecast
123 @forecast_cache[loc][:date] = forecast_date
125 m.reply "Couldn't find forecast for #{loc}"
128 m.reply "ERROR: #{e}"
132 plugin = ForecastPlugin.new
133 plugin.map 'forecast *location', :defaults => {:location => false}