X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=data%2Frbot%2Fplugins%2Fforecast.rb;h=1095886f3b46ac28036f5d922b4131485137b83f;hb=51c35168127e0faed56bceb3f5103f92ed31478c;hp=63e3127c7140fc81c55807af63cce0b9ee566fa0;hpb=1043b1a026c7362ced11c6bb4cd706a2c0d5cec3;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/data/rbot/plugins/forecast.rb b/data/rbot/plugins/forecast.rb index 63e3127c..1095886f 100644 --- a/data/rbot/plugins/forecast.rb +++ b/data/rbot/plugins/forecast.rb @@ -1,10 +1,14 @@ +#-- vim:sw=2:et +#++ # -# Forecast plugin for rbot -# by MrChucho (mrchucho@mrchucho.net) -# Copyright (C) 2006 Ralph M. Churchill +# :title: Forecast plugin for rbot # +# Author:: MrChucho (mrchucho@mrchucho.net) +# Copyright:: (C) 2006 Ralph M. Churchill + require 'soap/wsdlDriver' -require 'open-uri' +# TODO why not use HttpUtil instead of open-uri? +require 'open-uri' require 'rexml/document' require 'erb' @@ -84,6 +88,7 @@ class ForecastPlugin < Plugin end end @forecast_cache = Hash.new + @cache_mutex = Mutex.new end def forecast(m,params) @@ -102,32 +107,40 @@ class ForecastPlugin < Plugin end def get_forecast(m,loc) - begin - if @forecast_cache.has_key?(loc) and - Time.new - @forecast_cache[loc][:date] < 3600 - forecast = @forecast_cache[loc][:forecast] - forecast_date = @forecast_cache[loc][:date] - else - begin - l = LatLong.new - f = Forecast.new(*l.get_lat_long(loc)) - forecast,forecast_date = f.forecast - rescue => err - m.reply err - end - end + begin + @cache_mutex.synchronize do + if @forecast_cache.has_key?(loc) and + Time.new - @forecast_cache[loc][:date] < 3600 + forecast = @forecast_cache[loc][:forecast] if forecast - m.reply forecast - @forecast_cache[loc] = {} - @forecast_cache[loc][:forecast] = forecast - @forecast_cache[loc][:date] = forecast_date - else - m.reply "Couldn't find forecast for #{loc}" + m.reply forecast + return end - rescue => e - m.reply "ERROR: #{e}" + end + end + begin + l = LatLong.new + f = Forecast.new(*l.get_lat_long(loc)) + forecast,forecast_date = f.forecast + rescue => err + m.reply err + end + if forecast + m.reply forecast + @cache_mutex.synchronize do + @forecast_cache[loc] = { + :forecast => forecast, + :date => forecast_date + } + end + else + m.reply "Couldn't find forecast for #{loc}" end + rescue => e + m.reply "ERROR: #{e}" + end end end plugin = ForecastPlugin.new -plugin.map 'forecast *location', :defaults => {:location => false} +plugin.map 'forecast *location', + :defaults => {:location => false}, :thread => true