diff options
author | 2020-03-07 18:22:55 +0200 | |
---|---|---|
committer | 2020-03-07 18:22:55 +0200 | |
commit | ca77b345a255d7a55f7fab916f0fb5d7cdc7631e (patch) | |
tree | 5e72ea27b0170ff0c32935655f500f3757d1b42c | |
parent | (Space Cog): Added date parameter to `.space epic` command. (diff) |
(Space Cog): Added optional search term to `.space nasa` command, removed random page due no way to fetch how much pages is really there with specific search term.
-rw-r--r-- | bot/seasons/evergreen/space.py | 17 |
1 files changed, 11 insertions, 6 deletions
diff --git a/bot/seasons/evergreen/space.py b/bot/seasons/evergreen/space.py index c120ac1b..d94dcbf4 100644 --- a/bot/seasons/evergreen/space.py +++ b/bot/seasons/evergreen/space.py @@ -71,18 +71,23 @@ class Space(Cog): await ctx.send(embed=embed) @space.command(name="nasa") - async def nasa(self, ctx: Context) -> None: - """Get random NASA information/facts + image.""" - page = random.randint(1, 50) - + async def nasa(self, ctx: Context, *, search_term: Optional[str] = None) -> None: + """Get random NASA information/facts + image. Support `search_term` parameter for more specific search.""" # Create params for request, create URL and do request params = { - "media_type": "image", - "page": page + "media_type": "image" } + if search_term: + params["q"] = search_term + async with self.http_session.get(url=f"{NASA_IMAGES_BASE_URL}/search?{urlencode(params)}") as resp: data = await resp.json() + # Check is there any items returned + if len(data["collection"]["items"]) == 0: + await ctx.send(f"Can't find any items with search term `{search_term}`.") + return + # Get (random) item from result, that will be shown item = random.choice(data["collection"]["items"]) |