Created
November 11, 2015 22:20
-
-
Save meatherly/15fed7500abc4bf99598 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
defmodule EpBot.Handlers.ImageMe do | |
use Hedwig.Handler | |
@usage """ | |
Calls on hubot to image something. | |
""" | |
@default_params %{v: "1.0", rsz: "8", safe: "active"} | |
@google_search_url "https://ajax.googleapis.com/ajax/services/search/images" | |
def handle_event(%Message{delayed?: false} = msg, opts) do | |
case process_request(msg.body, opts) do | |
{:ok, image_url} -> | |
reply(msg, Stanza.body(image_url)) | |
{:error, reason} -> | |
reply(msg, reason) | |
nil -> | |
"" | |
end | |
{:ok, opts} | |
end | |
def handle_event(_msg, opts), do: {:ok, opts} | |
def process_request(message_body, opts) do | |
nickname = opts.client.nickname | |
regex_capture = ~r/^@#{nickname}(:)? (?<animated>(image|animate)) me (?<query>.*$)/i | |
case Regex.named_captures(regex_capture, message_body) do | |
%{"query" => image_phrase, "animated" => "image"} -> | |
{:ok, get_image(image_phrase)} | |
%{"query" => image_phrase, "animated" => "animate"} -> | |
{:ok, get_image(image_phrase, true)} | |
nil -> nil | |
end | |
end | |
def image_params(query) do | |
@default_params | |
|> put_param(:q, query) | |
end | |
def animated_params(query) do | |
@default_params | |
|> put_param(:as_filetype, "gif") | |
|> put_param(:q, "#{query} animated") | |
end | |
def query_params(query, animated) do | |
case animated do | |
true -> | |
animated_params(query) | |
false -> | |
image_params(query) | |
end | |
end | |
def get_images(params) do | |
case HTTPoison.get(@google_search_url, [], params: params) do | |
{:ok, %HTTPoison.Response{status_code: 200, body: body}} -> | |
Poison.decode!(body) | |
{:ok, %HTTPoison.Response{status_code: status_code}} -> | |
status_code | |
{:error, %HTTPoison.Error{reason: reason}} -> | |
reason | |
end | |
end | |
defp get_image_urls_from_response(response) do | |
response | |
|> Map.fetch!("responseData") | |
|> Map.fetch!("results") | |
|> Enum.map(fn(image_result) -> | |
Map.fetch! image_result, "unescapedUrl" | |
end) | |
end | |
defp choose_image(image_urls) do | |
Enum.random(image_urls) | |
end | |
defp ensure_image_extension(image_url) do | |
case Regex.match?(~r/(png|jpe?g|gif)$/i, image_url) do | |
true -> | |
image_url | |
false -> | |
"#{image_url}.png" | |
end | |
end | |
defp ensure_image(image_url, animated) do | |
case animated do | |
true -> | |
image_url | |
|> ensure_image_extension | |
|> String.replace(~r/(giphy\.com\/.*)\/.+_s.gif$/, "\g{1}/giphy.gif") | |
false -> | |
image_url | |
|> ensure_image_extension | |
end | |
end | |
def get_image(query, animated \\ false) do | |
query | |
|> query_params(animated) | |
|> get_images | |
|> get_image_urls_from_response | |
|> choose_image | |
|> ensure_image(animated) | |
end | |
defp put_param(params, key, value) do | |
Map.put(params, key, value) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment