Skip to content
This repository was archived by the owner on Jun 8, 2023. It is now read-only.

Commit 9d6efa5

Browse files
author
Tom Bell
committed
Merge pull request #809 from drdamour/sosearch
fixed sosearch to not use wwwdude as mentioned in #700
2 parents 93b51f1 + ffbbad9 commit 9d6efa5

File tree

1 file changed

+46
-33
lines changed

1 file changed

+46
-33
lines changed

src/scripts/sosearch.coffee

Lines changed: 46 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Search stack overflow and provide links to the first 5 questions
33
#
44
# Dependencies:
5-
# "wwwdude": "0.1.0"
5+
# none
66
#
77
# Configuration:
88
# None
@@ -13,8 +13,10 @@
1313
#
1414
# Author:
1515
# carsonmcdonald
16+
# drdamour
1617

17-
wwwdude = require("wwwdude")
18+
19+
zlib = require('zlib');
1820

1921
# API keys are public for Stackapps
2022
hubot_stackapps_apikey = 'BeOjD228tEOZP6gbYoChsg'
@@ -23,39 +25,50 @@ module.exports = (robot) ->
2325
robot.respond /sosearch( me)? (.*)/i, (msg) ->
2426
re = RegExp("(.*) with tags (.*)", "i")
2527
opts = msg.match[2].match(re)
28+
2629
if opts?
27-
soSearch msg, escape(opts[1]), opts[2].split(',')
30+
soSearch msg, opts[1], opts[2].split(',')
2831
else
29-
soSearch msg, escape(msg.match[2]), null
32+
soSearch msg, msg.match[2], []
3033

3134
soSearch = (msg, search, tags) ->
32-
client = wwwdude.createClient
33-
headers: { 'User-Agent': 'hubot search' }
34-
gzip: true
35-
timeout: 500
36-
37-
tagged = ''
38-
if tags?
39-
for tag in tags
40-
tagged += "#{tag.replace(/^\s+|\s+$/g,'')};"
41-
tagged = "&tagged=#{escape(tagged[0..tagged.length-2])}"
35+
36+
37+
38+
data = ""
39+
msg.http("http://api.stackoverflow.com/1.1/search")
40+
.query
41+
intitle: encodeURIComponent(search)
42+
key: hubot_stackapps_apikey
43+
tagged: encodeURIComponent(tags.join(':'))
44+
.get( (err, req)->
45+
req.addListener "response", (res)->
46+
output = res
47+
48+
#pattern stolen from http://stackoverflow.com/questions/10207762/how-to-use-request-or-http-module-to-read-gzip-page-into-a-string
49+
if res.headers['content-encoding'] is 'gzip'
50+
output = zlib.createGunzip()
51+
res.pipe(output)
52+
53+
output.on 'data', (d)->
54+
data += d.toString('utf-8')
55+
56+
output.on 'end', ()->
57+
parsedData = JSON.parse(data)
58+
if parsedData.error
59+
msg.send "Error searching stack overflow: #{parsedData.error.message}"
60+
return
61+
62+
if parsedData.total > 0
63+
qs = for question in parsedData.questions[0..5]
64+
"http://www.stackoverflow.com/questions/#{question.question_id} - #{question.title}"
65+
if parsedData.total-5 > 0
66+
qs.push "#{parsedData.total-5} more..."
67+
for ans in qs
68+
msg.send ans
69+
else
70+
msg.reply "No questions found matching that search."
71+
)()
72+
73+
4274

43-
client.get("http://api.stackoverflow.com/1.1/search?intitle=#{search}#{tagged}&key=#{hubot_stackapps_apikey}")
44-
.addListener 'error', (err) ->
45-
console.log("Error: #{err}")
46-
msg.reply "Error while executing search."
47-
.addListener 'http-error', (data, resp) ->
48-
console.log("Error code: #{resp.statusCode}")
49-
msg.reply "Error while executing search."
50-
.addListener 'success', (data, resp) ->
51-
results = JSON.parse(data)
52-
53-
if results.total > 0
54-
qs = for question in results.questions[0..5]
55-
"http://www.stackoverflow.com/questions/#{question.question_id} - #{question.title}"
56-
if results.total-5 > 0
57-
qs.push "#{results.total-5} more..."
58-
for ans in qs
59-
msg.send ans
60-
else
61-
msg.reply "No questions found matching that search."

0 commit comments

Comments
 (0)