google-images.coffee | |
---|---|
A way to interact with the Google Images API. hubot image me | _ = 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"
|