4 # :title: Forecast plugin for rbot
6 # Author:: MrChucho (mrchucho@mrchucho.net)
7 # Copyright:: (C) 2006 Ralph M. Churchill
9 require 'soap/wsdlDriver'
10 # TODO why not use HttpUtil instead of open-uri?
12 require 'rexml/document'
18 # Determine the latitude and longitude of a location. City, State and/or ZIP
20 # [+return+] latitude,longitude
23 url="http://api.local.yahoo.com/MapsService/V1/geocode?appid=mrchucho_rbot_weather&location=#{loc}"
27 results = (REXML::Document.new xmldoc).root
28 lat = results.elements["//Latitude/text()"].to_s
29 long = results.elements["//Longitude/text()"].to_s
34 return lat.to_f,long.to_f
39 WSDL_URI="http://www.nws.noaa.gov/forecasts/xml/SOAP_server/ndfdXMLserver.php?wsdl"
40 def initialize(lat,long)
42 # this extra step is for backward/forward compatibility
43 factory = SOAP::WSDLDriverFactory.new(WSDL_URI)
44 @forecaster=factory.respond_to?(:create_rpc_driver) ?
45 factory.create_rpc_driver : factory.create_driver
48 return parse(retrieve),Time.new
52 forecast = @forecaster.NDFDgenByDay(
53 @lat,@long,Time.now.strftime("%Y-%m-%d"),2,"24 hourly")
54 (REXML::Document.new(forecast)).root
59 d = (day==1) ? 'Today' : 'Tomorrow'
60 hi = xml.elements["//temperature[@type='maximum']/value[#{day}]/text()"]
61 lo = xml.elements["//temperature[@type='minimum']/value[#{day}]/text()"]
62 w = xml.elements["//weather/weather-conditions[#{day}]/@weather-summary"]
63 precip_am = xml.elements["//probability-of-precipitation/value[#{day*2-1}]/text()"]
64 precip_pm = xml.elements["//probability-of-precipitation/value[#{day*2}]/text()"]
65 msg += "#{d}: Hi #{hi} Lo #{lo}, #{w}. Precip: AM #{precip_am}% PM #{precip_pm}%\n"
71 class ForecastPlugin < Plugin
72 USAGE='forecast <location> => show the 2-day forecast for a location. Location can be any combination of City, State, Country and ZIP'
73 def help(plugin,topic="")
76 def usage(m,params={})
81 # this plugin only wants to store strings
90 @forecast_cache = Hash.new
91 @cache_mutex = Mutex.new
94 def forecast(m,params)
95 if params[:location] and params[:location].any?
96 loc = params[:location].join
97 @registry[m.sourcenick] = loc
100 if @registry.has_key?(m.sourcenick) then
101 loc = @registry[m.sourcenick]
104 m.reply "Please specifiy the City, State or ZIP"
109 def get_forecast(m,loc)
111 @cache_mutex.synchronize do
112 if @forecast_cache.has_key?(loc) and
113 Time.new - @forecast_cache[loc][:date] < 3600
114 forecast = @forecast_cache[loc][:forecast]
123 f = Forecast.new(*l.get_lat_long(loc))
124 forecast,forecast_date = f.forecast
130 @cache_mutex.synchronize do
131 @forecast_cache[loc] = {
132 :forecast => forecast,
133 :date => forecast_date
137 m.reply "Couldn't find forecast for #{loc}"
140 m.reply "ERROR: #{e}"
144 plugin = ForecastPlugin.new
145 plugin.map 'forecast *location',
146 :defaults => {:location => false}, :thread => true