Sha256: f07ff8d7a3a17547ce2894518c20bd93f7d3a41c43f8fbafa72bc0424c79226f

Contents?: true

Size: 889 Bytes

Versions: 1

Compression:

Stored size: 889 Bytes

Contents

require 'open-uri'
require 'uri'

class Image < Linkbot::Plugin
  
  Linkbot::Plugin.register('image', self,
    {
      :message => {:regex => /!image(?: (.+))?/, :handler => :on_message, :help => :help}
    }
  )
  
  def self.on_message(message, matches) 
    searchterm = matches[0]
    if searchterm.nil?
      doc = Hpricot(open("http://www.randomword.net").read)
      searchterm = doc.search("#word h2").text.strip
    end
    doc = JSON.parse(open("http://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=#{URI.encode(searchterm)}&rsz=8&safe=off", "Referer" => "http://lgscout.com").read)
    if doc["responseData"]["results"].length > 0
      doc["responseData"]["results"][rand(doc["responseData"]["results"].length)]["url"]
    else
      "No pictures found! Nuts!"
    end
  end
  
  def self.help
    "!image [searchity search] - Return a relevant picture"
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
protolink-0.2.8 example/linkbot/plugins/image.rb