4 # :title: Forecast plugin for rbot
6 # Author:: MrChucho (mrchucho@mrchucho.net)
7 # Copyright:: (C) 2006 Ralph M. Churchill
9 require 'soap/wsdlDriver'
11 require 'rexml/document'
17 # Determine the latitude and longitude of a location. City, State and/or ZIP
19 # [+return+] latitude,longitude
22 url="http://api.local.yahoo.com/MapsService/V1/geocode?appid=mrchucho_rbot_weather&location=#{loc}"
26 results = (REXML::Document.new xmldoc).root
27 lat = results.elements["//Latitude/text()"].to_s
28 long = results.elements["//Longitude/text()"].to_s
33 return lat.to_f,long.to_f
38 WSDL_URI="http://www.nws.noaa.gov/forecasts/xml/SOAP_server/ndfdXMLserver.php?wsdl"
39 def initialize(lat,long)
41 # this extra step is for backward/forward compatibility
42 factory = SOAP::WSDLDriverFactory.new(WSDL_URI)
43 @forecaster=factory.respond_to?(:create_rpc_driver) ?
44 factory.create_rpc_driver : factory.create_driver
47 return parse(retrieve),Time.new
51 forecast = @forecaster.NDFDgenByDay(
52 @lat,@long,Time.now.strftime("%Y-%m-%d"),2,"24 hourly")
53 (REXML::Document.new(forecast)).root
58 d = (day==1) ? 'Today' : 'Tomorrow'
59 hi = xml.elements["//temperature[@type='maximum']/value[#{day}]/text()"]
60 lo = xml.elements["//temperature[@type='minimum']/value[#{day}]/text()"]
61 w = xml.elements["//weather/weather-conditions[#{day}]/@weather-summary"]
62 precip_am = xml.elements["//probability-of-precipitation/value[#{day*2-1}]/text()"]
63 precip_pm = xml.elements["//probability-of-precipitation/value[#{day*2}]/text()"]
64 msg += "#{d}: Hi #{hi} Lo #{lo}, #{w}. Precip: AM #{precip_am}% PM #{precip_pm}%\n"
70 class ForecastPlugin < Plugin
71 USAGE='forecast <location> => show the 2-day forecast for a location. Location can be any combination of City, State, Country and ZIP'
72 def help(plugin,topic="")
75 def usage(m,params={})
80 # this plugin only wants to store strings
89 @forecast_cache = Hash.new
92 def forecast(m,params)
93 if params[:location] and params[:location].any?
94 loc = params[:location].join
95 @registry[m.sourcenick] = loc
98 if @registry.has_key?(m.sourcenick) then
99 loc = @registry[m.sourcenick]
102 m.reply "Please specifiy the City, State or ZIP"
107 def get_forecast(m,loc)
109 if @forecast_cache.has_key?(loc) and
110 Time.new - @forecast_cache[loc][:date] < 3600
111 forecast = @forecast_cache[loc][:forecast]
112 forecast_date = @forecast_cache[loc][:date]
116 f = Forecast.new(*l.get_lat_long(loc))
117 forecast,forecast_date = f.forecast
124 @forecast_cache[loc] = {}
125 @forecast_cache[loc][:forecast] = forecast
126 @forecast_cache[loc][:date] = forecast_date
128 m.reply "Couldn't find forecast for #{loc}"
131 m.reply "ERROR: #{e}"
135 plugin = ForecastPlugin.new
136 plugin.map 'forecast *location', :defaults => {:location => false}