From a00decfc2c754b7048fd6e76e7637783b72374d7 Mon Sep 17 00:00:00 2001 From: Robb Kidd Date: Wed, 22 Jul 2020 09:58:49 -0400 Subject: [PATCH] !image eats !gif !gif maybe used to use Reddit, but now it's basically the same as !image with a parameter requesting animated images. Signed-off-by: Robb Kidd --- plugins/gif.rb | 68 ------------------------------------------------ plugins/image.rb | 13 ++++++--- 2 files changed, 10 insertions(+), 71 deletions(-) delete mode 100644 plugins/gif.rb diff --git a/plugins/gif.rb b/plugins/gif.rb deleted file mode 100644 index 068eb80..0000000 --- a/plugins/gif.rb +++ /dev/null @@ -1,68 +0,0 @@ -require 'open-uri' -require 'hpricot' - -class Gif < Linkbot::Plugin - - def initialize - register :regex => /!gif(?: (.+))?/i - help "!gif [search term] - get a gif from reddit based on the optional search term" - end - - def on_message(message, matches) - searchterm = matches[0] - if searchterm.nil? - past_messages = message_history(message) - if past_messages - searchterm = past_messages[0]['body'] - else - searchterm = random_word - end - end - - searchterm = URI.encode(searchterm) - searchurl = "https://www.google.com/search?tbs=itp:animated&tbm=isch&q=#{searchterm}&safe=active" - - # this is an old iphone user agent. Seems to make google return good results. - useragent = "Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7" - - gifs = [] - - begin - Timeout::timeout(4) do - gifs = open(searchurl, "User-Agent" => useragent) - end - rescue Timeout::Error - return "Google is slow! No gifs for you." - end - - # pull gif URLs out of the page - gifs = gifs.read.scan(/var u='(.*?)'/).flatten - - # unescape google octal escapes - gifs = gifs.map { |g| unescape_octal(g) } - - #funnyjunk sucks - gifs.reject! {|x| x =~ /fjcdn\.com/} - - return "No gifs found. Lame." if gifs.empty? - - gifs.sample - end - - def random_word - doc = Hpricot(open("http://www.randomword.net").read) - doc.search("#word h2").text.strip - end - - # unescape google urls with octal (!?) escapes into url-encoded hex equivalents - def unescape(url) - url.gsub(/\\(\d{2})/) do |escape| - # given an octal escape like '\\75': - # - # 1. strip the leading slash - # 2. convert to an integer - # 3. convert to a hex string (url escaped) - "%#{escape[1..-1].to_i(8).to_s(16)}" - end - end -end diff --git a/plugins/image.rb b/plugins/image.rb index 111ffea..4a41d11 100644 --- a/plugins/image.rb +++ b/plugins/image.rb @@ -8,12 +8,18 @@ class Image < Linkbot::Plugin def initialize - register :regex => /!image(?: (.+))?/ - help "!image [searchity search] - Return a relevant picture" + register :regex => /\A!(gif|image)(?: (.+))?/i + + help <<~HELP + !image [searchity search] - Return a relevant picture + !gif [searchity search] - make that an animated image, barkeep! + HELP end def on_message(message, matches) color = nil + gif_requested = message.body.start_with?('!gif ') + searchterm = matches[0] if searchterm.nil? past_messages = message_history(message) @@ -26,6 +32,7 @@ def on_message(message, matches) searchterm = URI.encode(searchterm) searchurl = "https://www.google.com/search?tbm=isch&q=#{searchterm}&safe=active" + searchurl += "&tbs=itp:animated" if gif_requested # this is an old iphone user agent. Seems to make google return good results. useragent = "Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7" @@ -56,7 +63,7 @@ def on_message(message, matches) url = images.sample - if wallpaper?(url) + if !gif_requested && wallpaper?(url) url = [url, "(dealwithit) WALLPAPER WALLPAPER WALLPAPER (dealwithit)"] end