add apod bot owner command apikey
This commit is contained in:
parent
facec3111d
commit
1ad251d184
|
@ -229,11 +229,12 @@ Command:
|
||||||
* !apod YYYY-MM-DD - date of the APOD image to retrieve (ex. !apod 2020-03-15)
|
* !apod YYYY-MM-DD - date of the APOD image to retrieve (ex. !apod 2020-03-15)
|
||||||
* !apod stats - show information about uri cache
|
* !apod stats - show information about uri cache
|
||||||
* !apod clear - clear uri cache (Must be done as admin)
|
* !apod clear - clear uri cache (Must be done as admin)
|
||||||
|
* !apod apikey [api-key] - set the nasa api key (Must be done as bot owner)
|
||||||
* !apod help - show command help
|
* !apod help - show command help
|
||||||
|
|
||||||
API Key:
|
API Key:
|
||||||
|
|
||||||
The module uses a demo API Key which can be replaced by your own api key by setting the environment variable `APOD_API_KEY`.
|
The module uses a demo API Key which can be replaced by your own api key by setting the environment variable `APOD_API_KEY` or by setting the api key as a bot owner with command `!apod apikey [apikey]`.
|
||||||
|
|
||||||
You can create one at https://api.nasa.gov/#signUp
|
You can create one at https://api.nasa.gov/#signUp
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
import os
|
import os
|
||||||
import os
|
|
||||||
import re
|
import re
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
@ -35,11 +34,14 @@ class MatrixModule(BotModule):
|
||||||
def __init__(self, name):
|
def __init__(self, name):
|
||||||
super().__init__(name)
|
super().__init__(name)
|
||||||
self.api_key = os.getenv("APOD_API_KEY", "DEMO_KEY")
|
self.api_key = os.getenv("APOD_API_KEY", "DEMO_KEY")
|
||||||
self.apod_api_url = f"https://api.nasa.gov/planetary/apod?api_key={self.api_key}&hd=true"
|
self.update_api_urls()
|
||||||
self.apod_by_date_pi_url = self.apod_api_url + "&date="
|
|
||||||
self.matrix_uri_cache = dict()
|
self.matrix_uri_cache = dict()
|
||||||
self.APOD_DATE_PATTERN = r"^\d\d\d\d-\d\d-\d\d$"
|
self.APOD_DATE_PATTERN = r"^\d\d\d\d-\d\d-\d\d$"
|
||||||
|
|
||||||
|
def update_api_urls(self):
|
||||||
|
self.apod_api_url = f"https://api.nasa.gov/planetary/apod?api_key={self.api_key}&hd=true"
|
||||||
|
self.apod_by_date_api_url = self.apod_api_url + "&date="
|
||||||
|
|
||||||
async def matrix_message(self, bot, room, event):
|
async def matrix_message(self, bot, room, event):
|
||||||
self.logger.debug(f"room: {room.name} sender: {event.sender} wants latest astronomy picture of the day")
|
self.logger.debug(f"room: {room.name} sender: {event.sender} wants latest astronomy picture of the day")
|
||||||
|
|
||||||
|
@ -47,7 +49,7 @@ class MatrixModule(BotModule):
|
||||||
|
|
||||||
if len(args) == 1:
|
if len(args) == 1:
|
||||||
await self.send_apod(bot, room, self.apod_api_url)
|
await self.send_apod(bot, room, self.apod_api_url)
|
||||||
if len(args) == 2:
|
elif len(args) == 2:
|
||||||
if args[1] == "stats":
|
if args[1] == "stats":
|
||||||
await self.send_stats(bot, room)
|
await self.send_stats(bot, room)
|
||||||
elif args[1] == "clear":
|
elif args[1] == "clear":
|
||||||
|
@ -58,10 +60,13 @@ class MatrixModule(BotModule):
|
||||||
else:
|
else:
|
||||||
date = args[1]
|
date = args[1]
|
||||||
if re.match(self.APOD_DATE_PATTERN, date) is not None:
|
if re.match(self.APOD_DATE_PATTERN, date) is not None:
|
||||||
uri = self.apod_by_date_pi_url + date
|
uri = self.apod_by_date_api_url + date
|
||||||
await self.send_apod(bot, room, uri)
|
await self.send_apod(bot, room, uri)
|
||||||
else:
|
else:
|
||||||
await bot.send_text(room, "invalid date. accpeted: YYYY-MM-DD")
|
await bot.send_text(room, "invalid date. accpeted: YYYY-MM-DD")
|
||||||
|
elif len(args) == 3:
|
||||||
|
if args[1] == "apikey":
|
||||||
|
await self.update_api_key(bot, room, event, args[2])
|
||||||
|
|
||||||
async def send_apod(self, bot, room, uri):
|
async def send_apod(self, bot, room, uri):
|
||||||
self.logger.debug(f"send request using uri {uri}")
|
self.logger.debug(f"send request using uri {uri}")
|
||||||
|
@ -124,12 +129,16 @@ class MatrixModule(BotModule):
|
||||||
def get_settings(self):
|
def get_settings(self):
|
||||||
data = super().get_settings()
|
data = super().get_settings()
|
||||||
data["matrix_uri_cache"] = self.matrix_uri_cache
|
data["matrix_uri_cache"] = self.matrix_uri_cache
|
||||||
|
data["api_key"] = self.api_key
|
||||||
return data
|
return data
|
||||||
|
|
||||||
def set_settings(self, data):
|
def set_settings(self, data):
|
||||||
super().set_settings(data)
|
super().set_settings(data)
|
||||||
if data.get("matrix_uri_cache"):
|
if data.get("matrix_uri_cache"):
|
||||||
self.matrix_uri_cache = data["matrix_uri_cache"]
|
self.matrix_uri_cache = data["matrix_uri_cache"]
|
||||||
|
if data.get("api_key"):
|
||||||
|
self.api_key = data["api_key"]
|
||||||
|
self.update_api_urls()
|
||||||
|
|
||||||
def help(self):
|
def help(self):
|
||||||
return 'Sends latest Astronomy Picture of the Day to the room. (https://apod.nasa.gov/apod/astropix.html)'
|
return 'Sends latest Astronomy Picture of the Day to the room. (https://apod.nasa.gov/apod/astropix.html)'
|
||||||
|
@ -148,6 +157,15 @@ class MatrixModule(BotModule):
|
||||||
- YYYY-MM-DD - date of the APOD image to retrieve (ex. 2020-03-15)
|
- YYYY-MM-DD - date of the APOD image to retrieve (ex. 2020-03-15)
|
||||||
- stats - show information about uri cache
|
- stats - show information about uri cache
|
||||||
- clear - clear uri cache (Must be done as admin)
|
- clear - clear uri cache (Must be done as admin)
|
||||||
|
- apikey [api-key] - set the nasa api key (Must be done as bot owner)
|
||||||
- help - show command help
|
- help - show command help
|
||||||
"""
|
"""
|
||||||
await bot.send_text(room, msg)
|
await bot.send_text(room, msg)
|
||||||
|
|
||||||
|
async def update_api_key(self, bot, room, event, apikey):
|
||||||
|
bot.must_be_owner(event)
|
||||||
|
self.api_key = apikey
|
||||||
|
self.update_api_urls()
|
||||||
|
bot.save_settings()
|
||||||
|
await bot.send_text(room, 'Api key set')
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue