2016-04-11 06:04:47 +02:00
|
|
|
local xkcd = {}
|
|
|
|
|
|
|
|
local HTTP = require('socket.http')
|
2016-04-15 21:07:23 +02:00
|
|
|
local JSON = require('dkjson')
|
2016-04-11 06:04:47 +02:00
|
|
|
local utilities = require('utilities')
|
|
|
|
|
2016-05-08 01:30:48 +02:00
|
|
|
xkcd.command = 'xkcd [i]'
|
2016-04-11 06:04:47 +02:00
|
|
|
xkcd.doc = [[```
|
2016-05-08 01:30:48 +02:00
|
|
|
/xkcd [i]
|
|
|
|
Returns the latest xkcd strip and its alt text. If a number is given, returns that number strip. If "r" is passed in place of a number, returns a random strip.
|
2016-01-08 14:44:37 +01:00
|
|
|
```]]
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2016-04-11 06:04:47 +02:00
|
|
|
function xkcd:init()
|
|
|
|
xkcd.triggers = utilities.triggers(self.info.username):t('xkcd', true).table
|
|
|
|
end
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2016-04-11 06:04:47 +02:00
|
|
|
function xkcd:action(msg)
|
2015-11-25 03:22:04 +01:00
|
|
|
|
|
|
|
local jstr, res = HTTP.request('http://xkcd.com/info.0.json')
|
2015-07-03 00:15:52 +02:00
|
|
|
if res ~= 200 then
|
2016-05-29 19:08:39 +02:00
|
|
|
utilities.send_reply(self, msg, self.config.errors.connection)
|
2015-11-25 03:22:04 +01:00
|
|
|
return
|
2015-07-03 00:15:52 +02:00
|
|
|
end
|
|
|
|
local latest = JSON.decode(jstr).num
|
2016-05-08 01:30:48 +02:00
|
|
|
local strip_num = latest
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2016-05-08 01:30:48 +02:00
|
|
|
local input = utilities.input(msg.text)
|
2015-07-03 00:15:52 +02:00
|
|
|
if input then
|
2016-05-10 21:49:05 +02:00
|
|
|
if input == '404' then
|
2016-05-29 19:08:39 +02:00
|
|
|
utilities.send_message(self, msg.chat.id, '*404*\nNot found.', false, nil, true)
|
2016-05-10 21:49:05 +02:00
|
|
|
return
|
|
|
|
elseif tonumber(input) then
|
2016-05-08 01:30:48 +02:00
|
|
|
if tonumber(input) > latest then
|
|
|
|
strip_num = latest
|
|
|
|
else
|
|
|
|
strip_num = input
|
|
|
|
end
|
|
|
|
elseif input == 'r' then
|
|
|
|
strip_num = math.random(latest)
|
2015-08-29 08:15:01 +02:00
|
|
|
end
|
2015-07-03 00:15:52 +02:00
|
|
|
end
|
|
|
|
|
2016-05-08 01:30:48 +02:00
|
|
|
local res_url = 'http://xkcd.com/' .. strip_num .. '/info.0.json'
|
|
|
|
|
2016-04-11 06:04:47 +02:00
|
|
|
jstr, res = HTTP.request(res_url)
|
2015-07-03 00:15:52 +02:00
|
|
|
if res ~= 200 then
|
2016-05-29 19:08:39 +02:00
|
|
|
utilities.send_reply(self, msg, self.config.errors.connection)
|
2015-11-25 03:22:04 +01:00
|
|
|
return
|
2015-07-03 00:15:52 +02:00
|
|
|
end
|
2015-11-25 03:22:04 +01:00
|
|
|
local jdat = JSON.decode(jstr)
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2016-05-31 01:10:58 +02:00
|
|
|
local output = '*' .. jdat.safe_title .. ' (*[' .. jdat.num .. '](' .. jdat.img .. ')*)*\n_' .. jdat.alt:gsub('_', '\\_') .. '_'
|
2016-01-08 14:44:37 +01:00
|
|
|
|
2016-05-29 19:08:39 +02:00
|
|
|
utilities.send_message(self, msg.chat.id, output, false, nil, true)
|
2015-07-03 00:15:52 +02:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2016-04-11 06:04:47 +02:00
|
|
|
return xkcd
|