2 # Weather plugin for rbot
3 # by MrChucho (mrchucho@mrchucho.net)
4 # Copyright (C) 2006 Ralph M. Churchill
7 require 'rexml/document'
9 class CurrentConditions
10 def initialize(station)
12 @url = "http://www.nws.noaa.gov/data/current_obs/#{@station.upcase}.xml"
14 @mtime = Time.mktime(0)
15 @current_conditions = String.new
20 open(@url,"If-Modified-Since" => @mtime.rfc2822) do |feed|
21 # open(@url,"If-None-Match"=>@etag) do |feed|
22 @etag = feed.meta['etag']
23 @mtime = feed.last_modified
24 cc_doc = (REXML::Document.new feed).root
26 @current_conditions = parse(cc_doc)
28 rescue OpenURI::HTTPError => e
33 raise "Data for #{@station} not found"
35 raise "Error retrieving data: #{e}"
38 @current_conditions # +" Cached? "+ ((@iscached) ? "Y" : "N")
42 cc_doc.elements.each do |c|
43 cc[c.name.to_sym] = c.text
45 "At #{cc[:observation_time_rfc822]}, the wind was #{cc[:wind_string]} at #{cc[:location]} (#{cc[:station_id]}). The temperature was #{cc[:temperature_string]}#{heat_index_or_wind_chill(cc)}, and the pressure was #{cc[:pressure_string]}. The relative humidity was #{cc[:relative_humidity]}%. Current conditions are #{cc[:weather]} with #{cc[:visibility_mi]}mi visibility."
48 def heat_index_or_wind_chill(cc)
49 hi = cc[:heat_index_string]
50 wc = cc[:windchill_string]
52 " with a heat index of #{hi}"
54 " with a windchill of #{wc}"
61 class MyWeatherPlugin < Plugin
63 def help(plugin, topic="")
64 "weather <STATION> => display the current conditions at the location specified by the STATION code [Lookup your STATION code at http://www.nws.noaa.gov/data/current_obs/ - this will also store the STATION against your nick, so you can later just say \"weather\", weather => display the current weather at the location you last asked for"
69 # this plugin only wants to store strings
81 def describe(m, where)
82 if @cc_cache.has_key?(where) then
83 met = @cc_cache[where]
85 met = CurrentConditions.new(where)
90 @cc_cache[where] = met
95 m.reply "couldn't find weather data for #{where}"
99 def weather(m, params)
101 @registry[m.sourcenick] = params[:where]
102 describe(m,params[:where])
104 if @registry.has_key?(m.sourcenick)
105 where = @registry[m.sourcenick]
108 m.reply "I don't know where you are yet! Lookup your station at http://www.nws.noaa.gov/data/current_obs/ and tell me 'weather <station>', then I'll know."
114 plugin = MyWeatherPlugin.new
115 plugin.map 'weather :where', :defaults => {:where => false}