Skip to content

Instantly share code, notes, and snippets.

@hypomodern
Created October 18, 2011 20:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hypomodern/1296572 to your computer and use it in GitHub Desktop.
Save hypomodern/1296572 to your computer and use it in GitHub Desktop.
google.coffee
request = require('request')
querystring = require('querystring')
module.exports =
help: [
{
command: '@agora image QUERY',
description: 'Fetches the first Google Image for QUERY'
}
]
queryParams: (query) ->
"?v=1.0&q=#{query}"
load: (bot) ->
@getGoogleImage bot
# deprecated and with no replacement offered, but should run until 2014.
# until then, fuck them and their request for an AppID and API Key.
getGoogleImage: (bot) ->
self = this
bot.onMessage /^@agora image (.+)$/i, (channel, from, message, matches) ->
url = "https://ajax.googleapis.com/ajax/services/search/images#{self.queryParams(querystring.escape(matches[1]))}"
console.log "URL: #{url}"
request url, (error, res, body) =>
console.log body # => truncated json
data = JSON.parse(body).data
console.log data # => undefined
if data.error?
@message channel, "The Googles, they had a problem with that"
else
item = data.responseData.results[0]
if item
@message channel, "@#{from.split(' ')[0]} #{item.url}"
else
@message channel, "@#{from.split(' ')[0]} The Googles couldn't find anything appropriate"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment