Advertisement
Namasteh

Untitled

Feb 9th, 2014
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.05 KB | None | 0 0
  1. require 'cinch'
  2. require 'ostruct'
  3. require 'open-uri'
  4. require 'json'
  5. require 'cgi'
  6.  
  7. module Cinch
  8.   module Plugins
  9.     class Google
  10.       include Cinch::Plugin
  11.        
  12.       set :prefix, /^~/
  13.       set :plugin_name, 'google'
  14.       set :help, <<-USAGE.gsub(/^ {6}/, '')
  15.         Searches Google for results on image and web entries.
  16.           Usage:
  17.             * ~google <terms>: This will execute a Google WEB search, and return the top three results.
  18.             * ~images <terms>: This will execute a Google Image search and returns the top three results.
  19.           USAGE
  20.        
  21.       match /google (.+)/i, method: :execute_w
  22.       match /images (.+)/i, method: :execute_i
  23.      
  24.       def execute_w(m, query)
  25.         query.gsub! /\s/, '+'
  26.         data = search_w(m, query)
  27.         return m.reply "No results found for #{query}." if data.empty?
  28.         search_result(m, data)
  29.       end
  30.    
  31.       def execute_i(m, query)
  32.         query.gsub! /\s/, '+'
  33.         data = search_i(m, query)
  34.         return m.reply "No results found for #{query}." if data.empty?
  35.         search_result(m, data)
  36.       end
  37.    
  38.       def search_w(m, terms)
  39.         logo = "12G4o8o12g9l4e"
  40.         data = JSON.parse(open("http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=#{terms}").read)
  41.         rdata = data['responseData']['results']
  42.         results = []
  43.      
  44.         rdata.each{|i| results.push("%s: %s [ %s ]" % [logo, i['titleNoFormatting'], i['unescapedUrl']])}
  45.      
  46.         return results
  47.       end
  48.      
  49.       def search_i(m, terms)
  50.         logo = "12I4m8a12g9e4s"
  51.         data = JSON.parse(open("http://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=#{terms}").read)
  52.         rdata = data['responseData']['results']
  53.         results = []
  54.        
  55.         rdata.each{|i| results.push("%s: %s (%sx%s)" % [logo, i['unescapedUrl'], i['width'], i['height']])}
  56.        
  57.         return results
  58.       end
  59.        
  60.       def search_result(m, data)
  61.         data[0..2].each{|i| m.reply i}
  62.       end
  63.      
  64.     end
  65.   end
  66. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement