-
Notifications
You must be signed in to change notification settings - Fork 2
/
bukkit.coffee
92 lines (77 loc) · 2.53 KB
/
bukkit.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# Description:
# Fetches a serendipitous image from Ethan Marcotte's legendary bukk.it, possibly matching your choice of word.
#
# Dependencies:
# "htmlparser": "1.7.6"
# "soupselect: "0.2.0"
#
# Configuration:
# None
#
# Commands:
# hubot bukkit (.*)
#
# Author:
# David Yee (@tangentialism)
#
htmlparser = require "htmlparser"
Select = require("soupselect").select
# If false, you must address your robot directly.
ROBOT_IS_REALLY_ATTENTIVE = true
module.exports = (robot) ->
bukkit_bucket = []
sources = {
'bukkit': 'http://bukk.it/',
'misatkes': 'http://misatkes.com/',
'ryan': 'http://ryan.is/gifs/',
'floops': 'http://floop.me/'
}
bukkits = (source_bukkit) ->
if bukkit_bucket.length == 0
for own key, source of sources
fill_bukkit(key, source)
if source_bukkit
return bukkit_bucket.filter (x) -> x[1] == source_bukkit
else
return bukkit_bucket
bukkits_that_look_like = (word, source_bukkit) ->
reggie = new RegExp(word, "i");
if source_bukkit
return bukkits().filter (x) -> x[0].match(reggie) && x[1] == source_bukkit
else
return bukkits().filter (x) -> x[0].match(reggie)
fill_bukkit = (key, source) ->
robot.http(source)
.get() (err, res, body) ->
handler = new htmlparser.DefaultHandler()
parser = new htmlparser.Parser(handler)
parser.parseComplete(body)
bukkitz_elementz = Select handler.dom, "a"
bukkit_bucket = bukkit_bucket.concat([link.attribs.href, key] for link in bukkitz_elementz when link.attribs.href.match(/^[^\?\/]/))
giffize_url = (result) ->
return "#{sources[result[1]]}#{result[0]}"
fetch_me_a_bukkit = (msg, all) ->
if msg.match[1]
source = msg.match[2] if msg.match[2]
# Let's look for something... *special*
term = msg.match[1].replace(/\s+/, '')
my_bukkit = msg.random bukkits_that_look_like(term, source)
if my_bukkit
msg.send giffize_url(my_bukkit)
else
msg.reply "There is a curious void in the #{source || 'bukk.itz'} for “#{term}”"
else
my_bukkit = msg.random bukkits(source)
msg.send giffize_url(my_bukkit)
# Start it up.
bukkits()
if ROBOT_IS_REALLY_ATTENTIVE
robot.hear /^bukkit( [\w\-]+)?(?: from (\w+))?$/i, (msg) ->
fetch_me_a_bukkit(msg)
else
robot.respond /bukkit( [\w\-]+)?(?: from (\w+))?$/i, (msg) ->
fetch_me_a_bukkit(msg)
robot.respond /reload bukkits/i, (msg) ->
bukkit_bucket = []
bukkits()
msg.reply "All thy bukkits have been reloaded."