Jump To …

google-images.coffee

A way to interact with the Google Images API.

hubot image me - The Original. Queries Google Images for and returns a random top result. hubot animate me - The same thing as image me, except adds a few parameters to try to return an animated GIF instead. hubot mustache me - Adds a mustache to the specified URL. hubot mustache me - Searches Google Images for the specified query and mustaches it.

_ = require('underscore')._

module.exports = (robot) ->
  robot.respond /(image|img)( me)? (.*)/i, (msg) ->
    imageMe msg, msg.match[3], (url) ->
      msg.send url

  robot.respond /animate me (.*)/i, (msg) ->
    imageMe msg, msg.match[1], as_filetype: "gif", (url) ->
      msg.send url

  robot.respond /(?:mo?u)?sta(?:s|c)he?(?: me)? (.*)/i, (msg) ->
    type = Math.floor(Math.random() * 3)
    mustachify = "http://mustachify.me/#{type}?src="
    imagery = msg.match[1]

    if imagery.match /^https?:\/\//i
      msg.send "#{mustachify}#{imagery}"
    else
      imageMe msg, imagery, (url) ->
        msg.send "#{mustachify}#{url}"

imageMe = (msg, query, queryOptions, cb) ->
  cb = queryOptions if typeof queryOptions == 'function' 
  msg.http('http://ajax.googleapis.com/ajax/services/search/images')
    .query(_.extend({v: "1.0", rsz: '8', q: query, safe: 'active'}, queryOptions ? {}))
    .get() (err, res, body) ->
      images = JSON.parse(body)
      images = images.responseData.results
      if images.length > 0
        image  = msg.random images
        cb "#{image.unescapedUrl}#.png"