2016-05-29 19:08:39 +02:00
|
|
|
-- Credit to Heitor (tg:Wololo666; gh:heitorPB) for this plugin.
|
|
|
|
|
2016-04-08 23:12:02 +02:00
|
|
|
local apod = {}
|
|
|
|
|
|
|
|
local HTTPS = require('ssl.https')
|
2016-04-15 21:07:23 +02:00
|
|
|
local JSON = require('dkjson')
|
2016-04-08 23:12:02 +02:00
|
|
|
local URL = require('socket.url')
|
2016-06-07 06:31:34 +02:00
|
|
|
local utilities = require('otouto.utilities')
|
2016-04-08 23:12:02 +02:00
|
|
|
|
|
|
|
apod.command = 'apod [date]'
|
2016-05-27 05:28:44 +02:00
|
|
|
|
|
|
|
function apod:init(config)
|
2016-08-14 04:46:18 +02:00
|
|
|
apod.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('apod', true).table
|
|
|
|
apod.doc = [[
|
2016-08-14 04:26:44 +02:00
|
|
|
/apod [YYYY-MM-DD]
|
2016-01-14 20:38:26 +01:00
|
|
|
Returns the Astronomy Picture of the Day.
|
2016-08-14 04:26:44 +02:00
|
|
|
Source: nasa.gov
|
2016-08-14 04:46:18 +02:00
|
|
|
]]
|
|
|
|
apod.doc = apod.doc:gsub('/', config.cmd_pat)
|
|
|
|
apod.base_url = 'https://api.nasa.gov/planetary/apod?api_key=' .. (config.nasa_api_key or 'DEMO_KEY')
|
2016-04-08 23:12:02 +02:00
|
|
|
end
|
2016-01-14 20:38:26 +01:00
|
|
|
|
2016-05-27 02:26:30 +02:00
|
|
|
function apod:action(msg, config)
|
2016-08-14 04:46:18 +02:00
|
|
|
local input = utilities.input(msg.text)
|
|
|
|
local url = apod.base_url
|
|
|
|
local date = os.date('%F')
|
|
|
|
if input then
|
|
|
|
if input:match('^(%d+)%-(%d+)%-(%d+)$') then
|
|
|
|
url = url .. '&date=' .. URL.escape(input)
|
|
|
|
date = input
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local jstr, code = HTTPS.request(url)
|
|
|
|
if code ~= 200 then
|
|
|
|
utilities.send_reply(self, msg, config.errors.connection)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local data = JSON.decode(jstr)
|
|
|
|
if data.error then
|
|
|
|
utilities.send_reply(self, msg, config.errors.results)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local output = string.format(
|
|
|
|
'<b>%s (</b><a href="%s">%s</a><b>)</b>\n%s',
|
|
|
|
utilities.html_escape(data.title),
|
|
|
|
utilities.html_escape(data.hdurl or data.url),
|
|
|
|
date,
|
|
|
|
utilities.html_escape(data.explanation)
|
|
|
|
)
|
|
|
|
utilities.send_message(self, msg.chat.id, output, false, nil, 'html')
|
2016-01-14 20:38:26 +01:00
|
|
|
end
|
|
|
|
|
2016-04-08 23:12:02 +02:00
|
|
|
return apod
|