Covid api requests

pull/133/head
stijndcl 2022-09-22 02:04:34 +02:00
parent abbb3026eb
commit df884f55f1
5 changed files with 96 additions and 17 deletions

View File

@ -7,7 +7,7 @@ from discord.ext import commands
from database.crud.links import get_link_by_name
from database.schemas import Link
from didier import Didier
from didier.data.apis import inspirobot, urban_dictionary
from didier.data.apis import disease_sh, inspirobot, urban_dictionary
from didier.data.embeds.google import GoogleSearch
from didier.data.scrapers import google
@ -20,16 +20,29 @@ class Other(commands.Cog):
def __init__(self, client: Didier):
self.client = client
@commands.hybrid_command(name="corona", aliases=["covid", "rona"])
async def covid(self, ctx: commands.Context, country: str = "Belgium"):
"""Show Covid-19 info for a specific country.
By default, this will fetch the numbers for Belgium.
To get worldwide stats, use `all`, `global`, `world`, or `worldwide`.
"""
async with ctx.typing():
if country.lower() in ["all", "global", "world", "worldwide"]:
data = await disease_sh.get_global_info(self.client.http_session)
else:
data = await disease_sh.get_country_info(self.client.http_session, country)
await ctx.reply(str(data), mention_author=False)
@commands.hybrid_command(
name="define", aliases=["ud", "urban"], description="Look up the definition of a word on the Urban Dictionary"
)
async def define(self, ctx: commands.Context, *, query: str):
"""Look up the definition of `query` on the Urban Dictionary."""
async with ctx.typing():
status_code, definitions = await urban_dictionary.lookup(self.client.http_session, query)
if not definitions:
return await ctx.reply(f"Something went wrong (status {status_code})")
definitions = await urban_dictionary.lookup(self.client.http_session, query)
await ctx.reply(embed=definitions[0].to_embed(), mention_author=False)
@commands.hybrid_command(name="google", description="Google search")

View File

@ -0,0 +1,38 @@
from aiohttp import ClientSession
from didier.data.embeds.disease_sh import CovidData
from didier.utils.http.requests import ensure_get
__all__ = ["get_country_info", "get_global_info"]
async def get_country_info(http_session: ClientSession, country: str) -> CovidData:
"""Fetch the info for a given country for today and yesterday"""
endpoint = f"https://disease.sh/v3/covid-19/countries/{country}"
params = {"yesterday": 0, "strict": 1, "allowNull": 0}
async with ensure_get(http_session, endpoint, params=params) as response:
today = response
params["yesterday"] = 1
async with ensure_get(http_session, endpoint, params=params) as response:
yesterday = response
data = {"today": today, "yesterday": yesterday}
return CovidData.parse_obj(data)
async def get_global_info(http_session: ClientSession) -> CovidData:
"""Fetch the global info for today and yesterday"""
endpoint = "https://disease.sh/v3/covid-19/all"
params = {"yesterday": 0, "allowNull": 0}
async with ensure_get(http_session, endpoint, params=params) as response:
today = response
params["yesterday"] = 1
async with ensure_get(http_session, endpoint, params=params) as response:
yesterday = response
data = {"today": today, "yesterday": yesterday}
return CovidData.parse_obj(data)

View File

@ -1,8 +1,7 @@
from http import HTTPStatus
from aiohttp import ClientSession
from didier.data.embeds.urban_dictionary import Definition
from didier.utils.http.requests import ensure_get
__all__ = ["lookup", "PER_PAGE"]
@ -10,13 +9,9 @@ __all__ = ["lookup", "PER_PAGE"]
PER_PAGE = 10
async def lookup(http_session: ClientSession, query: str) -> tuple[int, list[Definition]]:
async def lookup(http_session: ClientSession, query: str) -> list[Definition]:
"""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:
if response.status != HTTPStatus.OK:
return response.status, []
response_json = await response.json()
return 200, list(map(Definition.parse_obj, response_json["list"]))
async with ensure_get(http_session, url, params={"term": query}) as response:
return list(map(Definition.parse_obj, response["list"]))

View File

@ -0,0 +1,33 @@
import discord
from overrides import overrides
from pydantic import BaseModel, Field
from didier.data.embeds.base import EmbedPydantic
__all__ = ["CovidData"]
class _CovidNumbers(BaseModel):
"""Covid information for a country"""
updated: int
country: str = "Worldwide"
cases: int
today_cases: int = Field(alias="todayCases")
deaths: int
today_deaths: int = Field(alias="todayDeaths")
recovered: int
todayRecovered: int = Field(alias="todayRecovered")
active: int
tests: int
class CovidData(EmbedPydantic):
"""Covid information from two days combined into one model"""
today: _CovidNumbers
yesterday: _CovidNumbers
@overrides
def to_embed(self, **kwargs) -> discord.Embed:
pass

View File

@ -1,6 +1,6 @@
import logging
from contextlib import asynccontextmanager
from typing import AsyncGenerator
from typing import AsyncGenerator, Optional
from aiohttp import ClientResponse, ClientSession, ContentTypeError
@ -19,10 +19,10 @@ def request_successful(response: ClientResponse) -> bool:
@asynccontextmanager
async def ensure_get(
http_session: ClientSession, endpoint: str, *, log_exceptions: bool = True
http_session: ClientSession, endpoint: str, *, params: Optional[dict] = None, log_exceptions: bool = True
) -> AsyncGenerator[dict, None]:
"""Context manager that automatically raises an exception if a GET-request fails"""
async with http_session.get(endpoint) as response:
async with http_session.get(endpoint, params=params) as response:
try:
content = await response.json()
except ContentTypeError: