From 1575b79fbf9c27fc0d48b5cad2fa8131b74504cb Mon Sep 17 00:00:00 2001 From: "Karlis. S" Date: Sat, 29 Feb 2020 13:23:26 +0200 Subject: (Space Cog): Created get_random_nasa_item function what will be used in (upcoming) .nasa command. --- bot/seasons/evergreen/space.py | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'bot') diff --git a/bot/seasons/evergreen/space.py b/bot/seasons/evergreen/space.py index 2caeb6df..2dd17ecf 100644 --- a/bot/seasons/evergreen/space.py +++ b/bot/seasons/evergreen/space.py @@ -1,5 +1,6 @@ import logging from datetime import datetime +from random import randint from typing import Any, Dict, Optional from urllib.parse import urlencode @@ -58,6 +59,16 @@ class Space(Cog): async with self.http_session.get(url=f"{BASE_URL}{endpoint}?{urlencode(params)}") as resp: return await resp.json() + async def get_random_nasa_item(self, data: Dict[str, Any]) -> Dict[str, Any]: + """Get random item from Dictionary that is fetched from NASA API in .nasa command.""" + # Get how much items is available + length = len(data["collection"]["items"]) + + # Get position of item that will be returned + pos = randint(0, length - 1) + + return data["collection"]["items"][pos] + def setup(bot: SeasonalBot) -> None: """Load Space Cog.""" -- cgit v1.2.3