unexist.dev

Changeset 326:b9ec7521dd46

Added weather plugin
author unexist
date Fri, 03 Aug 2012 18:56:20 +0200
parents c0ad9eb80bef
children 1a17f18c7492
files bot/cinch.rb
diffstat 1 files changed, 33 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/bot/cinch.rb	Thu Jun 28 13:56:17 2012 +0200
+++ b/bot/cinch.rb	Fri Aug 03 18:56:20 2012 +0200
@@ -1,3 +1,4 @@
+# encoding: UTF-8"
 #!/usr/bin/ruby
 #
 # @file Bot
@@ -22,10 +23,10 @@
 
 # Config
 if "aral" == Socket.gethostname
-  NICK     = "SFW-test"
+  NICK     = "CB-test"
   CHANNELS = [ "#subtle-test" ]
 else
-  NICK     = "SFW"
+  NICK     = "ContextBot"
   CHANNELS = [ "#subtle" ]
 end
 
@@ -576,7 +577,7 @@
 
       unless(phrase.nil?)
         # Get phrase
-        if(!version.empty? and phrase.has_version?(version.to_i))
+        if not version.empty? and phrase.has_version?(version.to_i)
           v = phrase.specific_version(version.to_i)
         else
           v = phrase.latest_version
@@ -663,7 +664,7 @@
     match /^([0-9]*)?(\!|\?)([^ ]+)((?:\s(?:[^ ]*))*)/, method: :get_phrase, use_prefix: false # {{{
     def get_phrase(m, version, op, key, args)
       # FIXME: Exclude keywords until groups are implemented
-      return if [ "news", "best", "worst", "alias", "rem", "give", "find", "forget", "proto" ].include?(key)
+      return if [ "news", "best", "worst", "alias", "rem", "give", "find", "forget", "proto", "weather" ].include?(key)
 
       phrase, *args = phrase_args(key, args)
 
@@ -820,6 +821,32 @@
       m.reply @protos[rand(@protos.size)]
     end
   end # }}}
+
+  class Weather # {{{
+    include Cinch::Plugin
+    set :react_on, :channel
+
+    match /weather (.+)/
+    def execute(m, location)
+      # Create mechanize agent
+      if @agent.nil?
+        @agent = Mechanize.new
+        @agent.user_agent_alias = "Linux Mozilla"
+        @agent.max_history      = 0
+      end
+
+      page   = @agent.get("http://thefuckingweather.com/?where=%s&unit=c" % [ CGI.escape(location) ])
+      temp   = page.search("//span[@class='temperature']").text rescue nil
+      remark = page.search("//p[@class='remark']").text rescue nil
+      flavor = page.search("//p[@class='flavor']").text rescue nil
+
+      if temp.nil? or remark.nil? or flavor.nil?
+        m.reply "Oops something went wrong..", true
+      else
+        m.reply "%s°?! %s! (%s)" % [ temp, remark, flavor ]
+      end
+    end
+  end # }}}
 end # }}}
 
 # Create bot
@@ -842,7 +869,8 @@
     Plugins::Scraper,
     Plugins::Phrases,
     Plugins::Actions,
-    Plugins::Protolol
+    Plugins::Protolol,
+    Plugins::Weather
   ]
   end
 end