require_gem 'rubyweather' require 'weather/service' class RubyWeatherPlugin < Plugin def initialize super class << @registry def store(val) val end def restore(val) val end end end def help(plugin, topic="") "wz [-s] => weather.com current conditions/forecast for . if '-s' option is given, stores as a default value for the user" end def t2s(i) (i.to_s =~ /^-/ ? '' : '+') + i.to_s + 'C' end def wz(m, params) Thread.new do begin loc_s = (params[:string] || []).join(' ') if loc_s && !loc_s.empty? if loc_s.gsub!(/(^|\s+)-s(\s+|$)/, '') || !@registry[m.sourcenick] @registry[m.sourcenick] = loc_s end else loc_s = @registry[m.sourcenick] end index = 0 if loc_s.gsub!(/#(\d+)/, '') index = $1.to_i - 1 end raise "i don't know where you live" if loc_s.nil? || loc_s.empty? ndays = 5 ws = Weather::Service.new locs = ws.find_location(loc_s.gsub(' ', '+')).to_a.sort { |a, b| a[1].size <=> b[1].size } loc = locs.slice!(index) raise 'not found' if loc.nil? fc = ws.fetch_forecast(loc[0], ndays) or raise 'fetch failed' s = "#{loc[1]}: #{t2s fc.current.temp}" s << " (feels like #{t2s fc.current.flik})" if fc.current.flik s << ", #{fc.current.outlook}" if fc.current.outlook s << ". Forecast" (0 .. ndays - 1).map { |_| f = fc.day(_) next unless f.hi || f.lo s << " | #{f.date.strftime('%a')}:" s << " #{t2s f.hi}" if f.hi s << " (#{t2s f.lo})" if f.lo if f.outlook_brief && f.outlook_brief != 'N/A' s << ", #{f.outlook_brief}" end } if locs.size > 0 s << " (but *maybe* you mean #{loc_s.capitalize} in " s << locs.map { |_| _[1].sub(Regexp.new(loc_s + '\s*,\s*', true), '') }.join(', ') << ' instead!)' end m.reply s rescue Exception => e m.reply "error (sorry, snaky!) -> #{e.message.gsub(/[\n\r]/, ' ')[0 .. 300]}" raise e end end end end plugin = RubyWeatherPlugin.new plugin.map 'wz *string' plugin.map 'wz'