forked from github/hubot-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chm.coffee
48 lines (41 loc) · 1.37 KB
/
chm.coffee
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
# Description:
# Shows a short history lesson of the day from the Computer History Museum
#
# Dependencies:
# "htmlparser": "1.7.6"
# "soupselect: "0.2.0"
#
# Configuration:
# None
#
# Commands:
# hubot today in computer history|tdih|chm - Displays the content from the This Day in History page on the Computer History Museum site
#
# Author:
# facto
Select = require("soupselect").select
HtmlParser = require "htmlparser"
module.exports = (robot) ->
robot.respond /(today in computer history|tdih|chm)$/i, (msg) ->
msg.http("http://www.computerhistory.org/tdih/")
.get() (err, res, body) ->
handler = new HtmlParser.DefaultHandler()
parser = new HtmlParser.Parser handler
parser.parseComplete body
contentEl = Select handler.dom, ".tdihevent p"
return unless contentEl
msg.send date(handler)
msg.send title(contentEl)
for sentence in blurbSentences(contentEl)
msg.send sentence + '.' if sentence and sentence isnt ""
title = (contentEl) ->
trim contentEl[0].children[0].raw
blurbSentences = (contentEl) ->
blurb = trim contentEl[1].children[0].raw
blurb.split('.')
date = (handler) ->
dateEl = Select handler.dom, ".title"
return "" unless dateEl
trim dateEl[0].children[0].raw
trim = (string) ->
return string.replace(/^\s*|\s*$/g, '')