class Plugins::Snooper

Public Instance Methods

execute(msg, url) click to toggle source
# File lib/Zeta/plugins/snooper.rb, line 52
def execute(msg, url)
  parser = URI::Parser.new
  url = "http://#{url}" unless url=~/^https?:\/\//
  url = parser.escape(url)

  # Ignore items on blacklist
  # blacklist = DEFAULT_BLACKLIST.dup
  # blacklist.concat(config[:blacklist]) if config[:blacklist]
  # return if blacklist.any?{|entry| url =~ entry}

  # Log
  # debug "URL matched: #{url}"

  # Parse URI
  p = URI(url)

  # Disregaurd on blacklist
  return if Blacklist.urls.include? p.host

  # API key lookup
  VideoInfo.provider_api_keys = { youtube: Config.secrets[:google] }

  # Parse out specific websites
  if p.host == 'youtube.com' || p.host == 'www.youtube.com' || p.host == 'youtu.be'
    match_youtube(msg, url)
  elsif p.host == 'i.imgur.com' || p.host == 'imgur.com'
    match_imgur(msg, url)
  else
    match_other(msg,url)
  end

rescue => e
  error "#{e.class.name}: #{e.message}"
  error "[404] #{msg.user} - #{url}"
end

Private Instance Methods

match_github(msg, url) click to toggle source
# File lib/Zeta/plugins/snooper.rb, line 99
def match_github(msg, url)
  # TODO: parse github url
end
match_imgur(msg, url) click to toggle source
# File lib/Zeta/plugins/snooper.rb, line 103
def match_imgur(msg, url)
  id = url.to_s.match(%r{(?:https?://i?\.?imgur.com/)(?:.*\/)?([A-Za-z0-9]+)(?:\.jpg|png|gif|gifv)?}i)
  return match_other(msg, url) unless id

  # Query Data
  data = JSON.parse(
      RestClient.get("https://api.imgur.com/3/image/#{id[1]}", 
                     { Authorization: "Client-ID #{Config.secrets[:imgur_id]}" }
                    )
    )
  return 'Unable to query imgur' unless defined?(data)

  i = Hashie::Mash.new(data)

  # SET Not Safe For Work
  nsf = i.data.nsfw ? "[#{Format(:red, 'NSFW')}]" : "[#{Format(:green, 'SAFE')}]"

  # Trigger reply message
  msg.reply("#{Format(:purple, 'IMGUR')} #{nsf} "\
          "∴ [#{Format(:yellow, i.data.type)}] #{i.data.width}x#{i.data.height} "\
          "∴ Views: #{ i.data.views.to_s} ∴ #{i.data.title ? i.data.title[0..100] : 'No Title'} "\
          "∴ Posted #{time_ago_in_words(Time.at(i.data.datetime))} ago")
end
match_other(msg,url) click to toggle source
# File lib/Zeta/plugins/snooper.rb, line 127
def match_other(msg,url)
  begin
    html = Mechanize.start { |m|
      # Timeout long winded pages
      m.max_history = 1
      m.read_timeout = 4
      m.max_file_buffer = 2_097_152

      # Parse the HTML
      Timeout::timeout(12) { 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
match_youtube(msg, url) click to toggle source
# File lib/Zeta/plugins/snooper.rb, line 89
def match_youtube(msg, url)
  if Config.secrets[:google]
    video = VideoInfo.new(url)
    msg.reply "#{Format(:red, 'YouTube ')}∴ #{video.title} ( #{Format(:green, Time.at(video.duration).utc.strftime("%H:%M:%S"))} )"
  else
    match_other(msg, url)
  end

end