2016-04-11 06:04:47 +02:00
|
|
|
local urbandictionary = {}
|
|
|
|
|
|
|
|
local HTTP = require('socket.http')
|
|
|
|
local URL = require('socket.url')
|
2016-04-15 21:07:23 +02:00
|
|
|
local JSON = require('dkjson')
|
2016-06-07 06:31:34 +02:00
|
|
|
local utilities = require('otouto.utilities')
|
2016-04-11 06:04:47 +02:00
|
|
|
|
|
|
|
urbandictionary.command = 'urbandictionary <query>'
|
2016-08-14 04:26:44 +02:00
|
|
|
urbandictionary.base_url = 'http://api.urbandictionary.com/v0/define?term='
|
2016-05-27 05:28:44 +02:00
|
|
|
|
|
|
|
function urbandictionary:init(config)
|
2016-08-14 04:46:18 +02:00
|
|
|
urbandictionary.triggers = utilities.triggers(self.info.username, config.cmd_pat)
|
|
|
|
:t('urbandictionary', true):t('ud', true):t('urban', true).table
|
2016-10-04 16:07:15 +02:00
|
|
|
urbandictionary.doc = [[/urbandictionary <query>
|
2016-01-08 14:44:37 +01:00
|
|
|
Returns a definition from Urban Dictionary.
|
2016-10-04 16:07:15 +02:00
|
|
|
Aliases: /ud, /urban]]
|
2016-08-14 04:46:18 +02:00
|
|
|
urbandictionary.doc = urbandictionary.doc:gsub('/', config.cmd_pat)
|
2016-04-11 06:04:47 +02:00
|
|
|
end
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2016-05-27 02:26:30 +02:00
|
|
|
function urbandictionary:action(msg, config)
|
2016-08-14 04:46:18 +02:00
|
|
|
local input = utilities.input_from_msg(msg)
|
|
|
|
if not input then
|
2016-10-04 16:07:15 +02:00
|
|
|
utilities.send_reply(msg, urbandictionary.doc, 'html')
|
2016-08-14 04:46:18 +02:00
|
|
|
return
|
|
|
|
end
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2016-08-14 04:46:18 +02:00
|
|
|
local url = urbandictionary.base_url .. URL.escape(input)
|
|
|
|
local jstr, code = HTTP.request(url)
|
|
|
|
if code ~= 200 then
|
2016-08-23 06:16:32 +02:00
|
|
|
utilities.send_reply(msg, config.errors.connection)
|
2016-08-14 04:46:18 +02:00
|
|
|
return
|
|
|
|
end
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2016-08-14 04:46:18 +02:00
|
|
|
local data = JSON.decode(jstr)
|
|
|
|
local output
|
|
|
|
if data.result_type == 'no_results' then
|
|
|
|
output = config.errors.results
|
|
|
|
else
|
2016-10-04 16:07:15 +02:00
|
|
|
output = string.format('<b>%s</b>\n\n%s\n\n<i>%s</i>',
|
|
|
|
utilities.html_escape(data.list[1].word),
|
|
|
|
utilities.trim(utilities.html_escape(data.list[1].definition)),
|
|
|
|
utilities.trim(utilities.html_escape(data.list[1].example or ''))
|
2016-08-14 04:46:18 +02:00
|
|
|
)
|
|
|
|
end
|
2016-10-04 16:07:15 +02:00
|
|
|
utilities.send_reply(msg, output, 'html')
|
2015-07-03 00:15:52 +02:00
|
|
|
end
|
|
|
|
|
2016-04-11 06:04:47 +02:00
|
|
|
return urbandictionary
|