diff options
author | 2020-03-07 18:00:43 +0200 | |
---|---|---|
committer | 2020-03-07 18:00:43 +0200 | |
commit | 266bcaa246ea177242b922c7d1f529ff297afe55 (patch) | |
tree | bc03a96ee08c06d57b0f5c96d351b6cd43161951 | |
parent | (Space Cog): Added `invoke_without_command` parameter to `.space` command to ... (diff) |
(Space Cog): Added date parameter to `.space epic` command.
-rw-r--r-- | bot/seasons/evergreen/space.py | 22 |
1 files changed, 19 insertions, 3 deletions
diff --git a/bot/seasons/evergreen/space.py b/bot/seasons/evergreen/space.py index f04482d3..c120ac1b 100644 --- a/bot/seasons/evergreen/space.py +++ b/bot/seasons/evergreen/space.py @@ -94,12 +94,28 @@ class Space(Cog): await ctx.send(embed=embed) @space.command(name="epic") - async def epic(self, ctx: Context) -> None: - """Get one of latest random image of earth from NASA EPIC API.""" + async def epic(self, ctx: Context, date: Optional[str] = None) -> None: + """Get one of latest random image of earth from NASA EPIC API. Support date parameter, format is YYYY-MM-DD.""" + # Parse date if provided + if date: + try: + show_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 + else: + show_date = None + # Generate URL and make request to API - async with self.http_session.get(url=f"{NASA_EPIC_BASE_URL}/api/natural") as resp: + async with self.http_session.get( + url=f"{NASA_EPIC_BASE_URL}/api/natural{f'/date/{show_date}' if show_date else ''}" + ) as resp: data = await resp.json() + if len(data) < 1: + await ctx.send("Can't find any images in this date.") + return + # Get random item from result that will be shown item = random.choice(data) |