diff options
author | 2020-02-29 13:17:49 +0200 | |
---|---|---|
committer | 2020-02-29 13:17:49 +0200 | |
commit | 7c15f1449627c54261ae753428ee2bd1b777cadf (patch) | |
tree | f744442a4fa5cc7e4db95991e2ba4abf3077e413 | |
parent | (Space Cog): Added fetch_from_nasa function to space.py, what do request to N... (diff) |
(Space Cog): Created .apod command that support date parameter.
-rw-r--r-- | bot/seasons/evergreen/space.py | 29 |
1 files changed, 27 insertions, 2 deletions
diff --git a/bot/seasons/evergreen/space.py b/bot/seasons/evergreen/space.py index 7d471d47..2caeb6df 100644 --- a/bot/seasons/evergreen/space.py +++ b/bot/seasons/evergreen/space.py @@ -1,8 +1,10 @@ import logging -from typing import Any, Dict +from datetime import datetime +from typing import Any, Dict, Optional from urllib.parse import urlencode -from discord.ext.commands import Cog +from discord import Embed +from discord.ext.commands import Cog, Context, command from bot.bot import SeasonalBot from bot.constants import Tokens @@ -27,6 +29,29 @@ class Space(Cog): self.bot = bot self.http_session = bot.http_session + @command(name="apod") + async def apod(self, ctx: Context, date: Optional[str] = None) -> None: + """Get Astronomy Picture of Day from NASA API. Date is optional parameter, what formatting is YYYY-MM-DD.""" + # Make copy of parameters + params = APOD_PARAMS.copy() + # Parse date to params, when provided. Show error message when invalid formatting + if date: + try: + params["date"] = datetime.strptime(date, "%Y-%m-%d").date().isoformat() + except ValueError: + await ctx.send(f"Invalid date {date}. Please make sure your date is in format YYYY-MM-DD.") + return + + # Do request to NASA API + result = await self.fetch_from_nasa("planetary/apod", params) + + # Create embed from result + embed = Embed(title=f"Astronomy Picture of Day in {result['date']}", description=result["explanation"]) + embed.set_image(url=result["hdurl"]) + embed.set_footer(text="Powered by NASA API") + + await ctx.send(embed=embed) + async def fetch_from_nasa(self, endpoint: str, params: Dict[str, Any]) -> Dict[str, Any]: """Fetch information from NASA API, return result.""" # Generate request URL from base URL, endpoint and parsed params |