otouto 3.13
good lord
This commit is contained in:
@ -10,79 +10,47 @@ local utilities = require('otouto.utilities')
|
||||
apod.command = 'apod [date]'
|
||||
|
||||
function apod:init(config)
|
||||
apod.triggers = utilities.triggers(self.info.username, config.cmd_pat)
|
||||
:t('apod', true):t('apodhd', true):t('apodtext', true).table
|
||||
apod.doc = config.cmd_pat .. [[apod [query]
|
||||
apod.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('apod', true).table
|
||||
apod.doc = [[
|
||||
/apod [YYYY-MM-DD]
|
||||
Returns the Astronomy Picture of the Day.
|
||||
If the query is a date, in the format YYYY-MM-DD, the APOD of that day is returned.
|
||||
Examples:
|
||||
]] .. config.cmd_pat .. [[apodhd [query]
|
||||
Returns the image in HD, if available.
|
||||
|
||||
]] .. config.cmd_pat .. [[apodtext [query]
|
||||
Returns the explanation of the APOD.
|
||||
|
||||
Source: nasa.gov]]
|
||||
Source: nasa.gov
|
||||
]]
|
||||
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')
|
||||
end
|
||||
|
||||
function apod:action(msg, config)
|
||||
|
||||
if not config.nasa_api_key then
|
||||
config.nasa_api_key = 'DEMO_KEY'
|
||||
end
|
||||
|
||||
local input = utilities.input(msg.text)
|
||||
local date = '*'
|
||||
local disable_page_preview = false
|
||||
|
||||
local url = 'https://api.nasa.gov/planetary/apod?api_key=' .. config.nasa_api_key
|
||||
|
||||
local url = apod.base_url
|
||||
local date = os.date('%F')
|
||||
if input then
|
||||
if input:match('(%d+)%-(%d+)%-(%d+)$') then
|
||||
if input:match('^(%d+)%-(%d+)%-(%d+)$') then
|
||||
url = url .. '&date=' .. URL.escape(input)
|
||||
date = date .. input
|
||||
else
|
||||
utilities.send_message(self, msg.chat.id, apod.doc, true, msg.message_id, true)
|
||||
return
|
||||
date = input
|
||||
end
|
||||
else
|
||||
date = date .. os.date("%F")
|
||||
end
|
||||
|
||||
date = date .. '*\n'
|
||||
|
||||
local jstr, res = HTTPS.request(url)
|
||||
if res ~= 200 then
|
||||
local jstr, code = HTTPS.request(url)
|
||||
if code ~= 200 then
|
||||
utilities.send_reply(self, msg, config.errors.connection)
|
||||
return
|
||||
end
|
||||
|
||||
local jdat = JSON.decode(jstr)
|
||||
|
||||
if jdat.error then
|
||||
local data = JSON.decode(jstr)
|
||||
if data.error then
|
||||
utilities.send_reply(self, msg, config.errors.results)
|
||||
return
|
||||
end
|
||||
|
||||
local img_url = jdat.url
|
||||
|
||||
if string.match(msg.text, '^'..config.cmd_pat..'apodhd*') then
|
||||
img_url = jdat.hdurl or jdat.url
|
||||
end
|
||||
|
||||
local output = date .. '[' .. jdat.title .. '](' .. img_url .. ')'
|
||||
|
||||
if string.match(msg.text, '^'..config.cmd_pat..'apodtext*') then
|
||||
output = output .. '\n' .. jdat.explanation
|
||||
disable_page_preview = true
|
||||
end
|
||||
|
||||
if jdat.copyright then
|
||||
output = output .. '\nCopyright: ' .. jdat.copyright
|
||||
end
|
||||
|
||||
utilities.send_message(self, msg.chat.id, output, disable_page_preview, nil, true)
|
||||
|
||||
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')
|
||||
end
|
||||
|
||||
return apod
|
||||
|
Reference in New Issue
Block a user