Created
November 5, 2015 05:03
-
-
Save celediel/bbd5a0af4d3081e818b5 to your computer and use it in GitHub Desktop.
Cinch Google Plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# encoding=utf-8 | |
require 'google-search' | |
require 'cgi' | |
# These are all very annoying. | |
# rubocop:disable Metrics/LineLength, Metrics/ClassLength, Metrics/CyclomaticComplexity | |
# rubocop:disable Metrics/MethodLength, Metrics/PerceivedComplexity, Metrics/AbcSize | |
module Cinch | |
module Plugins | |
# Google Search | |
class GoogleSearch | |
include Cinch::Plugin | |
set :prefix, /^\./ | |
match(/g(?: (.+))?/, method: :web) | |
def web(m, query) | |
begin | |
g = Google::Search::Web.new(query: query) | |
# d = Array of Google::Search::Item::Web | |
d = [] | |
g.each_with_index { |result, index| d[index] = result } | |
first_res = d[0] | |
content = first_res.content.gsub(%r{<\/?[^>]*>}, '').split(/\n/).join('') | |
content = CGI.unescapeHTML(content) | |
output = "#{content} :: #{first_res.uri}" | |
rescue NoMethodError | |
output = 'No results' | |
end | |
m.reply(output) | |
end | |
match(/gi(?: (.+))?/, method: :image) | |
def image(m, query) | |
begin | |
g = Google::Search::Image.new(query: query) | |
d = [] | |
g.each_with_index { |result, index| d[index] = result } | |
last = d.length - 1 | |
output = d[rand(0..last)].uri | |
rescue NoMethodError | |
output = 'No results' | |
end | |
m.reply(output) | |
end | |
end | |
end | |
end | |
# vim:tabstop=2 softtabstop=2 expandtab shiftwidth=2 smarttab foldmethod=syntax: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment