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

Commit

Permalink
Added script to search for questions on Stackoverflow.
Browse files Browse the repository at this point in the history
  • Loading branch information
carsonmcdonald committed Oct 30, 2011
1 parent 51535cf commit 53a50c2
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -22,7 +22,8 @@
"wolfram": "0.1.1",
"soupselect": "0.2.0",
"htmlparser": "1.7.x",
"sprintf": "0.1.1"
"sprintf": "0.1.1",
"wwwdude": "0.1.0"
},

"directories": {
Expand Down
51 changes: 51 additions & 0 deletions src/scripts/sosearch.coffee
@@ -0,0 +1,51 @@
# Search stack overflow and provide links to the first 5 questions.
#
# sosearch me <query> - Search for the query
# or
# sosearch me <query> with tags <tag list sperated by ,> - Search for the query limit to given tags

wwwdude = require("wwwdude")

# API keys are public for Stackapps
hubot_stackapps_apikey = 'BeOjD228tEOZP6gbYoChsg'

module.exports = (robot) ->
robot.respond /sosearch( me)? (.*)/i, (msg) ->
re = RegExp("(.*) with tags (.*)", "i")
opts = msg.match[2].match(re)
if opts?
soSearch msg, escape(opts[1]), opts[2].split(',')
else
soSearch msg, escape(msg.match[2]), null

soSearch = (msg, search, tags) ->
client = wwwdude.createClient
headers: { 'User-Agent': 'hubot search' }
gzip: true
timeout: 500

tagged = ''
if tags?
for tag in tags
tagged += "#{tag.replace(/^\s+|\s+$/g,'')};"
tagged = "&tagged=#{escape(tagged[0..tagged.length-2])}"

client.get("http://api.stackoverflow.com/1.1/search?intitle=#{search}#{tagged}&key=#{hubot_stackapps_apikey}")
.addListener 'error', (err) ->
console.log("Error: #{err}")
msg.reply "Error while executing search."
.addListener 'http-error', (data, resp) ->
console.log("Error code: #{resp.statusCode}")
msg.reply "Error while executing search."
.addListener 'success', (data, resp) ->
results = JSON.parse(data)

if results.total > 0
qs = for question in results.questions[0..5]
"http://www.stackoverflow.com/questions/#{question.question_id} - #{question.title}"
if results.total-5 > 0
qs.push "#{results.total-5} more..."
for ans in qs
msg.send ans
else
msg.reply "No questions found matching that search."

0 comments on commit 53a50c2

Please sign in to comment.