Created
October 18, 2011 20:13
-
-
Save hypomodern/1296572 to your computer and use it in GitHub Desktop.
google.coffee
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
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