2
2
# Search stack overflow and provide links to the first 5 questions
3
3
#
4
4
# Dependencies:
5
- # "wwwdude": "0.1.0"
5
+ # none
6
6
#
7
7
# Configuration:
8
8
# None
13
13
#
14
14
# Author:
15
15
# carsonmcdonald
16
+ # drdamour
16
17
17
- wwwdude = require (" wwwdude" )
18
+
19
+ zlib = require (' zlib' );
18
20
19
21
# API keys are public for Stackapps
20
22
hubot_stackapps_apikey = ' BeOjD228tEOZP6gbYoChsg'
@@ -23,39 +25,50 @@ module.exports = (robot) ->
23
25
robot .respond / sosearch( me)? (. * )/ i , (msg ) ->
24
26
re = RegExp (" (.*) with tags (.*)" , " i" )
25
27
opts = msg .match [2 ].match (re)
28
+
26
29
if opts?
27
- soSearch msg, escape ( opts[1 ]) , opts[2 ].split (' ,' )
30
+ soSearch msg, opts[1 ], opts[2 ].split (' ,' )
28
31
else
29
- soSearch msg, escape ( msg .match [2 ]), null
32
+ soSearch msg, msg .match [2 ], []
30
33
31
34
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
+
42
74
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