This repository was archived by the owner on Oct 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheric.rb
More file actions
69 lines (61 loc) · 1.63 KB
/
eric.rb
File metadata and controls
69 lines (61 loc) · 1.63 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
58
59
60
61
62
63
64
65
66
67
68
69
#-- vim:sw=2:et
#++
#
# :title: eric quote
#
# Author:: xx
#
#
# License:: GPL v2
require 'nokogiri'
require 'english'
class EricPlugin < Plugin
#API_URL = "http://www.iheartquotes.com/api/v1/random?format=html"
API_URL = "http://en.wikipedia.org/wiki/Special:Random"
def help(plugin, topic)
"fetches a random eric fact"
end
def reply(content)
b = (Nokogiri(content)/('.rbcontent/a')).text().gsub(/<br \/>/,'').gsub(/\-+/,' - ').gsub(/[^[:punct:][:alnum:]\- ]/,' ')
c = b.split
begin
rnd = rand(c.size)-1
end while c[rnd] =~ /(?:the|a|during|in|through|about|for|by|because|with)/i
c[rnd] = "eric"
c.join(" ")
end
def wikirand(content)
b = (Nokogiri(content)/('title')).text().gsub(/\-+/,' - ').gsub(/[^[:punct:][:alnum:] ]/,' ')
debug b
c = b.sub(/(?:\s*\(.*\))?\s*\-\s*Wikipedia,\s*the\s*free\s*encyclopedia/,'').split
debug 'size'
return nil if c.reject{|f| f =~ /^\(.*\)$/}.size < 3
x = 0
begin
rnd = rand(c.size)-1
x = x+1
end while c[rnd] =~ /^(?:the)|(?:a)|(?:of)$/i && x < 20
if c[rnd] =~ /.{2,}ing/
c[rnd] = "Ericing"
else
c[rnd] = c[rnd].singular == c[rnd] ? "Eric" : "Erics"
end
c.join(" ")
end
def random(m, params)
x=0
begin
debug 'making request'
page = @bot.httputil.get(API_URL)
answer = wikirand(page)
x = x+1
rescue Exception => e
error e.message
warning e.backtrace.join("\n")
answer = "failed"
end while answer.to_s.empty? && x < 20
m.reply answer.gsub(/\-?\s*Wikipedia$/i,'')
end
end
plugin = EricPlugin.new
plugin.map 'e', :action => 'random'