2020-05-19 16:46:49 +00:00
|
|
|
from typing import *
|
|
|
|
import royalnet.commands as rc
|
|
|
|
import aiohttp
|
|
|
|
import io
|
|
|
|
|
|
|
|
|
2020-07-18 14:29:43 +00:00
|
|
|
class DogCommand(rc.Command):
|
|
|
|
name: str = "dog"
|
2020-05-19 16:46:49 +00:00
|
|
|
|
2020-07-18 14:29:43 +00:00
|
|
|
description: str = "Invia un cane della razza specificata in chat."
|
2020-05-19 16:46:49 +00:00
|
|
|
|
2020-07-18 14:29:43 +00:00
|
|
|
syntax: str = "[razza]"
|
2020-05-19 16:46:49 +00:00
|
|
|
|
|
|
|
async def run(self, args: rc.CommandArgs, data: rc.CommandData) -> None:
|
2020-07-18 14:29:43 +00:00
|
|
|
breed = args.joined()
|
|
|
|
if breed:
|
|
|
|
url = f"https://dog.ceo/api/breed/{breed}/images/random"
|
|
|
|
else:
|
|
|
|
url = f"https://dog.ceo/api/breeds/image/random"
|
|
|
|
|
2020-05-19 16:46:49 +00:00
|
|
|
async with aiohttp.ClientSession() as session:
|
2020-07-18 14:29:43 +00:00
|
|
|
async with session.get(url) as response:
|
2020-05-28 14:57:10 +00:00
|
|
|
if response.status >= 400:
|
|
|
|
raise rc.ExternalError(f"Request returned {response.status}")
|
2020-05-19 16:46:49 +00:00
|
|
|
result = await response.json()
|
|
|
|
assert "status" in result
|
|
|
|
assert result["status"] == "success"
|
|
|
|
assert "message" in result
|
|
|
|
url = result["message"]
|
|
|
|
async with session.get(url) as response:
|
|
|
|
img = await response.content.read()
|
|
|
|
await data.reply_image(image=io.BytesIO(img))
|