leathan

google.coffee (fixed)

Jun 27th, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Description:
  2. #   A way to interact with the Google Web API.
  3. #
  4. # Commands:
  5. #   hubot google|g <query> - Queries Google Web for <query> and returns the first result.
  6. #
  7. # Author:
  8. #   leathan
  9.  
  10.  
  11. scrape = require('scrapeit')
  12.  
  13. module.exports = (robot) ->
  14.   robot.hear /^(g|google) (.*)/i, (msg) ->
  15.     googleMe msg, msg.match[2], (url) ->
  16.       msg.send url
  17.  
  18. googleMe = (msg, query, cb) ->
  19.   scrape "https://www.google.com/search?q=#{encodeURIComponent(msg.match[2])}", (err, o, dom) ->
  20.     data = o('.r a')[0].data.slice(15)
  21.     data = data.slice(0, data.indexOf('&'))
  22.     cb "#{decodeURIComponent(data)}";
Add Comment
Please, Sign In to add comment