lib/Zeta/plugins/snooper.rb in zetabot-0.0.7 vs lib/Zeta/plugins/snooper.rb in zetabot-0.0.8

- old
+ new

@@ -24,10 +24,11 @@ # Plugin for inspecting links pasted into channels. require 'video_info' require 'mechanize' require 'action_view' +require 'timeout' module Plugins class Snooper include Cinch::Plugin include Cinch::Helpers @@ -119,22 +120,36 @@ "∴ Posted #{time_ago_in_words(Time.at(i.data.datetime))} ago") end def match_other(msg,url) begin - html = Mechanize.start { |m| Nokogiri::HTML(m.get(url).content, nil, 'utf-8') } + html = Mechanize.start { |m| + # Timeout longwinded pages + m.max_history = 1 + m.read_timeout = 4 + m.max_file_buffer = 2_097_152 + + # Parse the HTML + Timeout::timeout(10) { Nokogiri::HTML(m.get(url).content, nil, 'utf-8') } + } + + # Reply Blocks if node = html.at_xpath("html/head/title") msg.reply("‡ #{node.text.lstrip.gsub(/\r|\n|\n\r/, ' ')[0..300]}") end if node = html.at_xpath('html/head/meta[@name="description"]') msg.reply("» #{node[:content].lines.first(3).join.gsub(/\r|\n|\n\r/, ' ')[0..300]}") end info "[200] #{msg.user} - #{url}" + rescue Timeout::Error + error "[408] #{msg.user} - #{url}" + msg.reply 'URL was forced Timed out' rescue => e error e error "[404] #{msg.user} - #{url}" + msg.safe_reply 'I am unable to load that URL' end end end end