This repository has been archived on 2021-04-24. You can view files and clone it, but cannot push or open issues or pull requests.
Mikubot-2/plugins/apod.lua

88 lines
2.0 KiB
Lua
Raw Normal View History

-- Credit to Heitor (tg:Wololo666; gh:heitorPB) for this plugin.
local apod = {}
local HTTPS = require('ssl.https')
local JSON = require('dkjson')
local URL = require('socket.url')
local utilities = require('utilities')
apod.command = 'apod [date]'
apod.doc = [[```
/apod [query]
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.
2016-01-15 18:35:09 +01:00
/apodhd [query]
Returns the image in HD, if available.
/apodtext [query]
Returns the explanation of the APOD.
Source: nasa.gov
```]]
function apod:init()
apod.triggers = utilities.triggers(self.info.username)
:t('apod', true):t('apodhd', true):t('apodtext', true).table
end
function apod:action(msg)
if not self.config.nasa_api_key then
self.config.nasa_api_key = 'DEMO_KEY'
end
local input = utilities.input(msg.text)
local date = '*'
2016-01-15 18:35:09 +01:00
local disable_page_preview = false
local url = 'https://api.nasa.gov/planetary/apod?api_key=' .. self.config.nasa_api_key
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
utilities.send_message(self, msg.chat.id, apod.doc, true, msg.message_id, true)
2016-01-22 23:20:08 +01:00
return
end
else
2016-01-15 04:39:24 +01:00
date = date .. os.date("%F")
end
date = date .. '*\n'
local jstr, res = HTTPS.request(url)
if res ~= 200 then
utilities.send_reply(self, msg, self.config.errors.connection)
return
end
local jdat = JSON.decode(jstr)
if jdat.error then
utilities.send_reply(msg, self.config.errors.results)
return
end
2016-01-15 18:35:09 +01:00
local img_url = jdat.url
if string.match(msg.text, '^/apodhd*') then
img_url = jdat.hdurl or jdat.url
end
local output = date .. '[' .. jdat.title .. '](' .. img_url .. ')'
2016-01-15 18:35:09 +01:00
if string.match(msg.text, '^/apodtext*') then
output = output .. '\n' .. jdat.explanation
disable_page_preview = true
end
if jdat.copyright then
2016-01-15 18:35:09 +01:00
output = output .. '\nCopyright: ' .. jdat.copyright
end
utilities.send_message(self, msg.chat.id, output, disable_page_preview, nil, true)
end
return apod