Skip to content

Instantly share code, notes, and snippets.

@arobson
Forked from levidehaan/gist:1586373
Created January 10, 2012 02:29
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 arobson/1586476 to your computer and use it in GitHub Desktop.
Save arobson/1586476 to your computer and use it in GitHub Desktop.
zlib = require 'zlib'
module.exports = (robot) ->
robot.respond /(stack|ss) ?(find:.*) ?(tags:.*)/i, (msg)->
query = msg.match[2]
query2 = msg.match[3]
query = query.replace /find:/i, ""
query2 = query2.replace /tags:/i, ""
query = encodeURIComponent query
query2 = encodeURIComponent query2
if query.length == 0
stackscript msg, "http://api.stackoverflow.com/1.1/searchtagged=jquery&order=desc&sort=votes"
else
stackscript msg, "http://api.stackoverflow.com/1.1/search?tagged=#{query2}&intitle=#{query}&order=desc&sort=votes"
console.log "http://api.stackoverflow.com/1.1/search?tagged=#{query2}&intitle=#{query}&order=desc&sort=votes"
stackscript = (msg, url) ->
request = msg.http(url).get(
headers:
'accept-encoding': 'gzip'
)
request.on 'response', (res) ->
if res.headers['content-encoding'] == 'gzip'
console.log res.headers
content = ""
gunzip = zlib.createGunzip()
res.pipe(gunzip)
gunzip.on 'data', (chunk) ->
content += chunk
gunzip.on 'end', () ->
console.log(content)
else
console.log "incorrect headers"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment