40 lines
1.2 KiB
Ruby
40 lines
1.2 KiB
Ruby
require 'open-uri'
|
|
require 'json'
|
|
|
|
class Mod_QDB < Vrobot4::Module::Module
|
|
def self.type() "QDB" end
|
|
Vrobot4::Module.add_module_type self
|
|
|
|
QDB = "http://greyserv.net/qdb"
|
|
|
|
def initialize info
|
|
super
|
|
register :c_quote, "quote", "Queries the Doominati Quote Database."
|
|
end
|
|
|
|
def c_quote m, argv
|
|
if argv.empty?
|
|
q, num = nil, nil
|
|
max = JSON.parse(open(QDB + "/interface.cgi").read)["numQuotes"]
|
|
loop do
|
|
num = rand(max) + 1
|
|
q = open(QDB + "/q/" + num.to_s).read
|
|
break unless m.serv.flags.include? "L" and
|
|
(q.lines.count > 4 or q.length > 1000)
|
|
end
|
|
m.reply_b "Quote %i:\n" % [num] + q
|
|
elsif Vrobot4.is_num? argv
|
|
m.reply_b open(QDB + "/q/" + argv.to_i.to_s).read
|
|
else
|
|
raise ArgumentError, "Invalid query" if argv.include? ?&
|
|
q = URI.escape argv
|
|
info = JSON.parse(open(QDB + "/interface.cgi?query=" + q).read)
|
|
raise ArgumentError, "No results" unless info["searchResults"].any?
|
|
res = info["searchResults"].sample
|
|
m.reply_b "Quote %i:\n" % [res] + open(QDB + "/q/" + res.to_s).read
|
|
end
|
|
end
|
|
end
|
|
|
|
## EOF
|