Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

52 lines (39 sloc) 1.489 kb
# Make hubot fetch quotes pertaining The IT Crowd
# The SHOES-A
#
# get archer
# REQUIRED MODULES
# sudo npm install htmlparser
# sudo npm install soupselect
# sudo npm install jsdom
# sudo npm install underscore
Select = require("soupselect").select
HtmlParser = require "htmlparser"
JsDom = require "jsdom"
_ = require("underscore")
module.exports = (robot) ->
robot.respond /itcrowd quote$/i, (msg) ->
msg
.http("http://en.wikiquote.org/wiki/The_IT_Crowd")
.header("User-Agent: Crowdbot for Hubot (+https://github.com/github/hubot-scripts)")
.get() (err, res, body) ->
quotes = parse_html(body, "dl")
quote = get_quote msg, quotes
get_quote = (msg, quotes) ->
nodeChildren = _.flatten childern_of_type(quotes[Math.floor(Math.random() * quotes.length)])
quote = (textNode.data for textNode in nodeChildren).join(' ').replace(/^\s+|\s+$/g, '')
msg.send quote
# Helpers
parse_html = (html, selector) ->
handler = new HtmlParser.DefaultHandler((() ->), ignoreWhitespace: true)
parser = new HtmlParser.Parser handler
parser.parseComplete html
Select handler.dom, selector
childern_of_type = (root) ->
return [root] if root?.type is "text"
if root?.children?.length > 0
return (childern_of_type(child) for child in root.children)
get_dom = (xml) ->
body = JsDom.jsdom(xml)
throw Error("No XML data returned.") if body.getElementsByTagName("FilterReturn")[0].childNodes.length == 0
return body
Jump to Line
Something went wrong with that request. Please try again.