Skip to content

Commit

Permalink
[Animals] simplify api
Browse files Browse the repository at this point in the history
  • Loading branch information
japandotorg committed Oct 8, 2023
1 parent 6f1f7fe commit 8106290
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 243 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ To add the cogs to your instance please do: `[p]repo add Seina-Cogs https://gith
| BattleRoyale| 0.1.2 | <details><summary>Play battle royale</summary>Play battle royale with your friends or join automated matches. | inthedark.org |
| PersonalChannels| 0.1.1 | <details><summary>Personal channel for members</summary>Personal channel for members. | inthedark.org |
| FirstMessage| 0.1.0 | <details><summary>First Message</summary>A simple cog for jump to first message of a channel.</details> | inthedark.org |
| Animals | 0.1.0 | <details><summary>Random animals!</summary>Random animal images & facts</details> | inthedark.org |
| Animals | 0.1.2 | <details><summary>Random animals!</summary>Random animal images & facts</details> | inthedark.org |
| MassUnban | 0.1.0 | <details><summary>Mass unban users.</summary>Mass unban users by the ban reason used.</details> | inthedark.org |
| AntiLinks | 0.1.1 | <details><summary>No links allowed.</summary>Removes all links in specified channels, with the ability to whitelist roles.</details> | inthedark.org |
| StatusRole | 0.1.0 | <details><summary>Assign roles on custom status.</summary>Assign roles to users for the duration in which they have certain custom statuses <details> | inthedark.org |
Expand Down
20 changes: 10 additions & 10 deletions animals/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"""

import random
from typing import Dict, List, Optional
from typing import Dict, List, Optional, Tuple

import aiohttp
from redbot.core import commands
Expand All @@ -34,7 +34,7 @@


class AnimalAPI:
def __init__(self, session: aiohttp.ClientSession):
def __init__(self, session: aiohttp.ClientSession) -> None:
self.session: aiohttp.ClientSession = session
self.endpoints: Dict[str, Dict[str, List[Dict[str, str]]]] = {
"images": {
Expand Down Expand Up @@ -122,14 +122,14 @@ def __init__(self, session: aiohttp.ClientSession):
},
}

async def image(self, animal: str):
async def image(self, animal: str) -> Optional[str]:
endpoint = random.choice(self.endpoints["images"][animal])
async with self.session.get(url=endpoint["url"]) as response:
if response.status != 200:
return raise_for_status(response)
return (await response.json())[endpoint["key"]]

async def fact(self, animal: str):
async def fact(self, animal: str) -> Optional[str]:
endpoint = random.choice(self.endpoints["facts"][animal])
async with self.session.get(url=endpoint["url"]) as response:
if response.status != 200:
Expand All @@ -141,7 +141,7 @@ async def fact(self, animal: str):


class CatAPI:
def __init__(self, ctx: commands.Context, session: aiohttp.ClientSession):
def __init__(self, ctx: commands.Context, session: aiohttp.ClientSession) -> None:
self.ctx: commands.Context = ctx
self.bot: Red = self.ctx.bot
self.session: aiohttp.ClientSession = session
Expand Down Expand Up @@ -173,7 +173,7 @@ def __init__(self, ctx: commands.Context, session: aiohttp.ClientSession):
"Vocalisation",
]

async def image(self, breed: Optional[str] = None):
async def image(self, breed: Optional[str] = None) -> Optional[Tuple]:
url = self.base + "images/search?has_breeds=1"
keys = await self.bot.get_shared_api_tokens("thecatapi")
token = keys.get("api_key")
Expand Down Expand Up @@ -235,7 +235,7 @@ async def image(self, breed: Optional[str] = None):
details += f"**URLs:** {' • '.join([f'[{x}]({y})' for x, y in urls.items()])}"
return response["url"], response["breeds"][0]["name"], details

async def breeds(self):
async def breeds(self) -> Optional[Tuple]:
keys = await self.bot.get_shared_api_tokens("thecatapi")
token = keys.get("api_key")
async with self.session.get(
Expand All @@ -253,13 +253,13 @@ async def breeds(self):


class DogAPI:
def __init__(self, ctx: commands.Context, session: aiohttp.ClientSession):
def __init__(self, ctx: commands.Context, session: aiohttp.ClientSession) -> None:
self.ctx: commands.Context = ctx
self.bot: Red = self.ctx.bot
self.session: aiohttp.ClientSession = session
self.base: str = "https://api.thedogapi.com/v1/"

async def image(self, breed: Optional[str] = None):
async def image(self, breed: Optional[str] = None) -> Optional[Tuple]:
url = self.base + "images/search?has_breeds=1"
keys = await self.bot.get_shared_api_tokens("thedogapi")
token = keys.get("api_key")
Expand Down Expand Up @@ -294,7 +294,7 @@ async def image(self, breed: Optional[str] = None):
details += height + weight # type: ignore
return response["url"], response["breeds"][0]["name"], details

async def breeds(self):
async def breeds(self) -> Optional[Tuple]:
keys = await self.bot.get_shared_api_tokens("thedogapi")
token = keys.get("api_key")
async with self.session.get(
Expand Down
Loading

0 comments on commit 8106290

Please sign in to comment.