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)
|
|
|
|
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]
|
2016-01-14 20:38:26 +01:00
|
|
|
Returns the Astronomy Picture of the Day.
|
2016-01-14 20:56:30 +01:00
|
|
|
If the query is a date, in the format YYYY-MM-DD, the APOD of that day is returned.
|
2016-05-27 05:28:44 +02:00
|
|
|
]]..config.cmd_pat..[[apodhd [query]
|
2016-01-15 18:35:09 +01:00
|
|
|
Returns the image in HD, if available.
|
2016-05-27 05:28:44 +02:00
|
|
|
]]..config.cmd_pat..[[apodtext [query]
|
2016-01-15 18:35:09 +01:00
|
|
|
Returns the explanation of the APOD.
|
2016-01-14 20:56:30 +01:00
|
|
|
Source: nasa.gov
|
2016-01-14 20:38:26 +01:00
|
|
|
```]]
|
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-01-14 20:38:26 +01:00
|
|
|
|
2016-05-27 02:26:30 +02:00
|
|
|
if not config.nasa_api_key then
|
|
|
|
config.nasa_api_key = 'DEMO_KEY'
|
2016-01-14 20:56:30 +01:00
|
|
|
end
|
|
|
|
|
2016-04-08 23:12:02 +02:00
|
|
|
local input = utilities.input(msg.text)
|
2016-01-14 20:38:26 +01:00
|
|
|
local date = '*'
|
2016-01-15 18:35:09 +01:00
|
|
|
local disable_page_preview = false
|
2016-01-14 20:38:26 +01:00
|
|
|
|
2016-05-27 02:26:30 +02:00
|
|
|
local url = 'https://api.nasa.gov/planetary/apod?api_key=' .. config.nasa_api_key
|
2016-01-14 20:38:26 +01:00
|
|
|
|
|
|
|
if input then
|
2016-01-22 23:20:08 +01:00
|
|
|
if input:match('(%d+)%-(%d+)%-(%d+)$') then
|
|
|
|
url = url .. '&date=' .. URL.escape(input)
|
|
|
|
date = date .. input
|
|
|
|
else
|
2016-05-29 19:08:39 +02:00
|
|
|
utilities.send_message(self, msg.chat.id, apod.doc, true, msg.message_id, true)
|
2016-01-22 23:20:08 +01:00
|
|
|
return
|
|
|
|
end
|
2016-01-14 20:38:26 +01:00
|
|
|
else
|
2016-01-15 04:39:24 +01:00
|
|
|
date = date .. os.date("%F")
|
2016-01-14 20:38:26 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
date = date .. '*\n'
|
|
|
|
|
|
|
|
local jstr, res = HTTPS.request(url)
|
|
|
|
if res ~= 200 then
|
2016-05-27 02:26:30 +02:00
|
|
|
utilities.send_reply(self, msg, config.errors.connection)
|
2016-01-14 20:38:26 +01:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local jdat = JSON.decode(jstr)
|
|
|
|
|
|
|
|
if jdat.error then
|
2016-05-27 02:26:30 +02:00
|
|
|
utilities.send_reply(self, msg, config.errors.results)
|
2016-01-14 20:38:26 +01:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2016-01-15 18:35:09 +01:00
|
|
|
local img_url = jdat.url
|
|
|
|
|
2016-05-27 05:28:44 +02:00
|
|
|
if string.match(msg.text, '^'..config.cmd_pat..'apodhd*') then
|
2016-01-15 18:35:09 +01:00
|
|
|
img_url = jdat.hdurl or jdat.url
|
|
|
|
end
|
|
|
|
|
2016-04-08 23:12:02 +02:00
|
|
|
local output = date .. '[' .. jdat.title .. '](' .. img_url .. ')'
|
2016-01-15 18:35:09 +01:00
|
|
|
|
2016-05-27 05:28:44 +02:00
|
|
|
if string.match(msg.text, '^'..config.cmd_pat..'apodtext*') then
|
2016-01-15 18:35:09 +01:00
|
|
|
output = output .. '\n' .. jdat.explanation
|
|
|
|
disable_page_preview = true
|
|
|
|
end
|
2016-01-14 20:38:26 +01:00
|
|
|
|
|
|
|
if jdat.copyright then
|
2016-01-15 18:35:09 +01:00
|
|
|
output = output .. '\nCopyright: ' .. jdat.copyright
|
2016-01-14 20:38:26 +01:00
|
|
|
end
|
|
|
|
|
2016-05-29 19:08:39 +02:00
|
|
|
utilities.send_message(self, msg.chat.id, output, disable_page_preview, nil, true)
|
2016-01-14 20:56:30 +01:00
|
|
|
|
2016-01-14 20:38:26 +01:00
|
|
|
end
|
|
|
|
|
2016-04-08 23:12:02 +02:00
|
|
|
return apod
|