diff options
author | 2020-02-29 13:30:41 +0200 | |
---|---|---|
committer | 2020-02-29 13:30:41 +0200 | |
commit | 698f78ee0fd8aa233b1af63c75da841ae8517096 (patch) | |
tree | d490d488d124065bc5240569b277a49a70303140 | |
parent | (Space Cog): Created get_random_nasa_item function what will be used in (upco... (diff) |
(Space Cog): Added .nasa command that show information and facts about NASA and/or space
-rw-r--r-- | bot/seasons/evergreen/space.py | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/bot/seasons/evergreen/space.py b/bot/seasons/evergreen/space.py index 2dd17ecf..5eeba7dc 100644 --- a/bot/seasons/evergreen/space.py +++ b/bot/seasons/evergreen/space.py @@ -14,6 +14,7 @@ logger = logging.getLogger(__name__) # NASA API base URL BASE_URL = "https://api.nasa.gov/" +NASA_IMAGES_BASE = "https://images-api.nasa.gov/" # Default Parameters: # .apod command default request parameters @@ -53,6 +54,29 @@ class Space(Cog): await ctx.send(embed=embed) + @command(name="nasa") + async def nasa(self, ctx: Context) -> None: + """Get random NASA information/facts + image.""" + page = randint(1, 50) + + # Create params for request, create URL and do request + params = { + "media_type": "image", + "page": page + } + async with self.http_session.get(url=f"{NASA_IMAGES_BASE}search?{urlencode(params)}") as resp: + data = await resp.json() + + # Get (random) item from result, that will be shown + item = await self.get_random_nasa_item(data) + + # Create embed and send it + embed = Embed(title=item["data"][0]["title"], description=item["data"][0]["description"]) + embed.set_image(url=item["links"][0]["href"]) + 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 |