From 1ad251d1845dca1032f57a2c573c2e5ddbb9ee0d Mon Sep 17 00:00:00 2001 From: Frank Becker Date: Sun, 15 Mar 2020 18:49:24 +0100 Subject: [PATCH] add apod bot owner command apikey --- README.md | 3 ++- modules/apod.py | 28 +++++++++++++++++++++++----- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 97ca4ff..e2a50ff 100644 --- a/README.md +++ b/README.md @@ -229,11 +229,12 @@ Command: * !apod YYYY-MM-DD - date of the APOD image to retrieve (ex. !apod 2020-03-15) * !apod stats - show information about uri cache * !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 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 diff --git a/modules/apod.py b/modules/apod.py index 2e22821..2b5ead3 100644 --- a/modules/apod.py +++ b/modules/apod.py @@ -1,5 +1,4 @@ import os -import os import re import requests @@ -35,11 +34,14 @@ class MatrixModule(BotModule): def __init__(self, name): super().__init__(name) 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.apod_by_date_pi_url = self.apod_api_url + "&date=" + self.update_api_urls() self.matrix_uri_cache = dict() 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): 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: await self.send_apod(bot, room, self.apod_api_url) - if len(args) == 2: + elif len(args) == 2: if args[1] == "stats": await self.send_stats(bot, room) elif args[1] == "clear": @@ -58,10 +60,13 @@ class MatrixModule(BotModule): else: date = args[1] 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) else: 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): self.logger.debug(f"send request using uri {uri}") @@ -124,12 +129,16 @@ class MatrixModule(BotModule): def get_settings(self): data = super().get_settings() data["matrix_uri_cache"] = self.matrix_uri_cache + data["api_key"] = self.api_key return data def set_settings(self, data): super().set_settings(data) if data.get("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): 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) - stats - show information about uri cache - 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 """ 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') +