2022-07-24 16:39:27 +02:00
|
|
|
from http import HTTPStatus
|
|
|
|
|
2022-07-13 22:54:16 +02:00
|
|
|
from aiohttp import ClientSession
|
|
|
|
|
|
|
|
from didier.data.embeds.urban_dictionary import Definition
|
|
|
|
|
2022-07-14 20:28:45 +02:00
|
|
|
__all__ = ["lookup", "PER_PAGE"]
|
|
|
|
|
|
|
|
|
|
|
|
PER_PAGE = 10
|
2022-07-13 22:54:16 +02:00
|
|
|
|
|
|
|
|
2022-07-24 16:39:27 +02:00
|
|
|
async def lookup(http_session: ClientSession, query: str) -> tuple[int, list[Definition]]:
|
2022-07-13 22:54:16 +02:00
|
|
|
"""Fetch the Urban Dictionary definitions for a given word"""
|
|
|
|
url = "https://api.urbandictionary.com/v0/define"
|
|
|
|
|
|
|
|
async with http_session.get(url, params={"term": query}) as response:
|
2022-07-24 16:39:27 +02:00
|
|
|
if response.status != HTTPStatus.OK:
|
|
|
|
return response.status, []
|
|
|
|
|
2022-07-13 22:54:16 +02:00
|
|
|
response_json = await response.json()
|
2022-07-24 16:39:27 +02:00
|
|
|
return 200, list(map(Definition.parse_obj, response_json["list"]))
|