Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fallback for Missing Google Search API #31

Merged
merged 2 commits into from
Dec 7, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# hubot-google-images

A hubot script that interacts with the Google Images API for greater productivity and lulz.
A hubot script that interacts with the Google Custom Search API for greater
productivity and lulz.

See [`src/google-images.coffee`](src/google-images.coffee) for full documentation.

Expand All @@ -21,13 +22,9 @@ Then add **hubot-google-images** to your `external-scripts.json`:
## Configuration

### Custom Search Engine
Set environment variables `HUBOT_GOOGLE_CSE_ID` and `HUBOT_GOOGLE_CSE_KEY`
to use the [Google Custom Search API](https://developers.google.com/custom-search/docs/overview)
instead of the deprecated image search API.

You might want to use the custom search API if you have concerns about
seeing NSFW images. The old Google image search API only has `safe=active`
which is not as strict as `safe=strict` on the new API.
Google no longer offers an unregistered image search API. You must set up a
[Google Custom Search API](https://developers.google.com/custom-search/docs/overview) and set
the environment variables `HUBOT_GOOGLE_CSE_ID` and `HUBOT_GOOGLE_CSE_KEY`.

The Custom Search API provides up to [100 search queries per day](https://developers.google.com/custom-search/json-api/v1/overview) for free.
If you need more than that you'll have to pay.
Expand Down
36 changes: 8 additions & 28 deletions src/google-images.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# HUBOT_MUSTACHIFY_URL - Optional. Allow you to use your own mustachify instance.
# HUBOT_GOOGLE_IMAGES_HEAR - Optional. If set, bot will respond to any line that begins with "image me" or "animate me" without needing to address the bot directly
# HUBOT_GOOGLE_SAFE_SEARCH - Optional. Search safety level.
# HUBOT_GOOGLE_IMAGES_FALLBACK - The URL to use when API fails. `{q}` will be replaced with the query string.
#
# Commands:
# hubot image me <query> - The Original. Queries Google Images for <query> and returns a random top result.
Expand Down Expand Up @@ -99,37 +100,16 @@ imageMe = (msg, query, animated, faces, cb) ->
.error "(see #{error.extendedHelp})" if error.extendedHelp
) error for error in response.error.errors if response.error?.errors
else
msg.send "Google Image Search API is not longer available. " +
"Please setup up Custom Search Engine API."
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Linking to documentation on how to set this up would definitely help users.

deprecatedImage(msg, query, animated, faces, cb)

deprecatedImage = (msg, query, animated, faces, cb) ->
# Using deprecated Google image search API
q =
v: '1.0'
rsz: '8'
q: query
safe: process.env.HUBOT_GOOGLE_SAFE_SEARCH || 'active'
if animated is true
q.as_filetype = 'gif'
q.q += ' animated'
if faces is true
q.as_filetype = 'jpg'
q.imgtype = 'face'
msg.http('https://ajax.googleapis.com/ajax/services/search/images')
.query(q)
.get() (err, res, body) ->
if err
msg.send "Encountered an error :( #{err}"
return
if res.statusCode isnt 200
msg.send "Bad HTTP response :( #{res.statusCode}"
return
images = JSON.parse(body)
images = images.responseData?.results
if images?.length > 0
image = msg.random images
cb ensureResult(image.unescapedUrl, animated)
else
msg.send "Sorry, I found no results for '#{query}'."
# Show a fallback image
imgUrl = process.env.HUBOT_GOOGLE_IMAGES_FALLBACK ||
'http://i.imgur.com/CzFTOkI.png'
imgUrl = imgUrl.replace(/\{q\}/, encodeURIComponent(query))
cb ensureResult(imgUrl, animated)

# Forces giphy result to use animated version
ensureResult = (url, animated) ->
Expand Down