Skip to content
This repository
Kyle Kelley November 28, 2013
file 58 lines (46 sloc) 1.524 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
# Description:
# Make hubot fetch quotes pertaining The IT Crowd
#
# Dependencies:
# "htmlparser": "1.7.6"
# "soupselect": "0.2.0"
# "jsdom": "0.2.14"
# "underscore": "1.3.3"
#
# Configuration:
# None
#
# Commands:
#
# Author:
# rrix

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 children_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

children_of_type = (root) ->
  return [root] if root?.type is "text"

  if root?.children?.length > 0
    return (children_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
Something went wrong with that request. Please try again.