]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/chucknorris.rb
chucknorris: fix loading
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / chucknorris.rb
index 362462e626a8b019babfee762f3eda4f77eaccd6..42385ff303a2b0a109835c5c7f2f002a05abf596 100644 (file)
@@ -1,29 +1,74 @@
-require 'uri/common'
-require 'cgi'
+require 'yaml'
+require 'zlib'
 
+MIN_RATING = 6.0
+MIN_VOTES = 25
+
+# the plugin
 class ChuckNorrisPlugin < Plugin
 
-  def help(plugin, topic="")
-    "chucknorris => show a random chuck norris fact."
-  end
-  
-  def chucknorris(m, params)
-    factdata = @bot.httputil.get(URI.parse('http://www.4q.cc/index.php?pid=fact&person=chuck'))
-    unless factdata
-      m.reply "This Chuck Norris fact made my brain explode. (HTTP error)"
-      return
+  # Loadez les factes
+  def initialize
+    if path = find_facts_file('chucknorris.yml.gz')
+      fyml = Zlib::GzipReader.open(path).read
+    elsif path = find_facts_file('chucknorris.yml')
+      fyml = open(path).read
+    else
+      raise "Error: Couldn't find chucknorris.yml[.gz]"
     end
 
+    debug "+ [chucknorris] Loading #{path}..."
+
+    @@facts = YAML.load(fyml).map{|fact,(score,votes)| votes >= MIN_VOTES ? [score,fact] : nil}.compact
+    debug "+ [chucknorris] #{@@facts.length} Chuck Norris facts loaded..."
+    debug "  Random fact: #{@@facts[rand(@@facts.size)].inspect}"
+
+    super
+  end
+
+  def name
+    "chucknorris"
+  end
 
-    if factdata =~ %r{<h1> And now a random fact about Chuck Norris...</h1>(.+?)<hr />}
-      m.reply(CGI::unescapeHTML($1))
+  # Just a little helper for the initialize method...
+  def find_facts_file(name)
+    full_path = File.join Config::datadir, "plugins", name
+    found_files = Dir[full_path]
+    if found_files.empty?
+      nil
     else
-      m.reply "This Chuck Norris fact punched my teeth in. (Parse error)"
+      found_files[0]
     end
+  end
 
+  # HELP!
+  def help(plugin, topic="chuck")
+    "chuck|norris|chucknorris [min_rating] => show a random Chuck Norris fact (optional minimum rating from 1-10, default=6.0)."
+    #\"fact [person]\" shows a fact about someone in the channel.
+  end
+
+  # The meat.
+  def fact(m, params)
+    min = params[:minrating].to_f
+    debug "+ Getting Chuck Norris fact (rating > #{min})..."
+
+    viable_facts = @@facts.select {|rating, fact| rating >= min}
+    if viable_facts.empty?
+      debug "  - no facts found with rating >= #{min}"
+      m.reply "Are you nuts?!? There are no facts better than #{min}!!!"
+      return
+    end
+
+    rating, fact = viable_facts[rand(viable_facts.length)]
+    m.reply "#{fact} [score=#{rating}]"
   end
 
 end
 
 plugin = ChuckNorrisPlugin.new
-plugin.map 'chucknorris'
+
+# plugin.map 'fact :minrating', :action => 'fact', :defaults => {:minrating=>MIN_RATING}
+plugin.map 'chucknorris :minrating', :action => 'fact', :defaults => {:minrating=>MIN_RATING}
+plugin.map 'chuck :minrating', :action => 'fact', :defaults => {:minrating=>MIN_RATING}
+plugin.map 'norris :minrating', :action => 'fact', :defaults => {:minrating=>MIN_RATING}
+