-
Notifications
You must be signed in to change notification settings - Fork 4
/
austin.rb
29 lines (24 loc) · 854 Bytes
/
austin.rb
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
require 'open-uri'
require 'hpricot'
class Austin < CampfireBot::Plugin
BASE_URL = 'http://www.imdb.com/character/ch0002425/quotes'
on_command 'austin', :austin
def austin(msg)
doc = Hpricot(open(BASE_URL))
chunks = []
doc.traverse_element("br") {|b| chunks << b if b.next_node != nil && b.next_node.elem? && b.next_node.to_s == '<br />' }
chunks.pop
random_chunk = rand(chunks.size - 1)
raw_quote = chunks[random_chunk].nodes_at(2..(chunks[random_chunk + 1].node_position - chunks[random_chunk].node_position - 1))
quote = raw_quote.to_s
quote.gsub!(/(\\n)/, "")
quote.gsub!(/(<br \/>)/, "\n")
quote.gsub!(/^\s/, "")
quote.gsub!(/ {2,}/, " ")
quote.gsub!(/<\/?[^>]*>/, "")
quote.split("\n")
quote.each {|l| msg.speak l}
rescue
msg.speak 'Austin Powers: Yeah, baby, yeah'
end
end