From be6ecc04e23e477f717cd2e60453ae7943228eeb Mon Sep 17 00:00:00 2001 From: "Heitor P. de Bittencourt" Date: Thu, 14 Jan 2016 21:38:26 +0200 Subject: [PATCH] Added APOD plugin. Gets Astronomy Picture of the Day, from NASA. --- README.md | 5 ++-- config.lua | 4 ++- plugins/apod.lua | 73 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 3 deletions(-) create mode 100755 plugins/apod.lua diff --git a/README.md b/README.md index d1cc446..87060a8 100755 --- a/README.md +++ b/README.md @@ -19,6 +19,7 @@ Here is a list of most otouto plugins. |--------|---------|----------|-------| | help.lua | /help | Returns a list of commands. | /h | | about.lua | /about | Returns the about text as configured in config.lua. | +| apod.lua | /apod [query] | Gets Astronomy Picture of the Day for current day, or for a specific date (YYYY-MM-DD). | | | ping.lua | /ping | The simplest plugin ever! | | echo.lua | /echo | Repeats a string of text. | | gSearch.lua | /google | Returns Google web results. | /g, /gnsfw | @@ -93,7 +94,7 @@ Where the key is the preconfigured response (where #NAME will be replaced with t ##Setup You **must** have Lua (5.2+), LuaSocket, and LuaSec installed. For uploading photos and other files, you must have curl installed. The fortune.lua plugin requires that fortune is installed. -For weather.lua, lastfm.lua, and bible.lua to work, you must have API keys for [OpenWeatherMap](http://openweathermap.org), [last.fm](http://last.fm), and [Biblia.com](http://biblia.com), respectively. cats.lua uses an API key (via [The Cat API](http://thecatapi.com)) to get more results, though it is not required. +For weather.lua, lastfm.lua, and bible.lua to work, you must have API keys for [OpenWeatherMap](http://openweathermap.org), [last.fm](http://last.fm), and [Biblia.com](http://biblia.com), respectively. cats.lua uses an API key (via [The Cat API](http://thecatapi.com)) to get more results, though it is not required. apod.lua uses an API key (via [NASA API](https://api.nasa.gov/)) to have more queries per minute, but it can be used with a demo API key (DEMO_KEY). **Before you do anything, open config.lua in a text editor and make the following changes:** @@ -145,5 +146,5 @@ otouto uses dkjson, a pure-Lua JSON parser. This is provided with the code and d ##Contributors The creator and maintainer of otouto is [topkecleon](http://github.com/topkecleon). He can be contacted via [Telegram](http://telegram.me/topkecleon), [Twitter](http://twitter.com/topkecleon), or [email](mailto:topkecleon@outlook.com). -Other developers who have contributed to otouto are [Juan Potato](http://github.com/JuanPotato), [Tiago Danin](http://github.com/TiagoDanin), [Ender](http://github.com/luksireiku), and [Iman Daneshi](http://github.com/Imandaneshi). +Other developers who have contributed to otouto are [Juan Potato](http://github.com/JuanPotato), [Tiago Danin](http://github.com/TiagoDanin), [Ender](http://github.com/luksireiku), [Iman Daneshi](http://github.com/Imandaneshi) and [HeitorPB](https://github.com/heitorPB). diff --git a/config.lua b/config.lua index c6e7026..93e3886 100755 --- a/config.lua +++ b/config.lua @@ -6,6 +6,7 @@ return { owm_api_key = '', biblia_api_key = '', thecatapi_key = '', + nasaapi_key = '', time_offset = 0, lang = 'en', antisquig = false, @@ -99,7 +100,8 @@ telegram.me/otouto 'cats.lua', 'hearthstone.lua', 'shout.lua', - -- Put new plugins here. + 'apod.lua', + -- Put new plugins above this line. 'help.lua', 'greetings.lua' } diff --git a/plugins/apod.lua b/plugins/apod.lua new file mode 100755 index 0000000..49c69f0 --- /dev/null +++ b/plugins/apod.lua @@ -0,0 +1,73 @@ +-- TODO: +-- inline bot stuff + +if not config.nasaapi_key then + print('Missing config value: nasaapi_key.') + print('You can use the simple key DEMO_KEY, but it is very limited.') + print('apod.lua will not be enabled.') + return +end + +local command = 'apod [query]' +local 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. +```]] + +local triggers = { + '^/apod[@'..bot.username..']*' +} + +local action = function(msg) + + local input = msg.text:input() + local caption = '' + local date = '*' + --local date_url = '' + + local url = 'https://api.nasa.gov/planetary/apod?api_key=' .. config.nasaapi_key + + if input then + url = url .. '&date=' .. URL.escape(input) + -- date_url = string.sub(date,3,4) .. string.sub(date,6,7) .. string.sub(date,9,10) + date = date .. input + else + -- date_url = os.date("%y%m%d") + date = date .. os.date("%Y-%m-%d") + end + + date = date .. '*\n' + + local jstr, res = HTTPS.request(url) + if res ~= 200 then + sendReply(msg, config.errors.connection) + return + end + + local jdat = JSON.decode(jstr) + + if jdat.error then + sendReply(msg, config.errors.results) + return + end + + --local weburl = 'http://apod.nasa.gov/apod/ap' .. date_url .. '.html' + --caption = date .. '[' .. jdat.title .. '](' .. weburl .. ')\n' + caption = date .. '[' .. jdat.title .. '](' .. jdat.url .. ')\n' + + if jdat.copyright then + caption = caption .. 'Copyright: ' .. jdat.copyright + end + + sendMessage(msg.chat.id, caption, false, nil, true) +end + +return { + action = action, + triggers = triggers, + doc = doc, + command = command +}