mirror of
https://github.com/RYGhub/royalnet.git
synced 2024-11-26 21:14:19 +00:00
Fix /cat and /dog. Probably
This commit is contained in:
parent
a408683b46
commit
e0e6a334f1
2 changed files with 41 additions and 11 deletions
|
@ -1,10 +1,11 @@
|
|||
import io
|
||||
import logging
|
||||
import pathlib
|
||||
|
||||
import aiohttp
|
||||
import royalnet.engineer as engi
|
||||
|
||||
import royalpack.bolts as rb
|
||||
import royalpack.config as cfg
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
@ -15,6 +16,8 @@ async def cat(*, _msg: engi.Message, **__):
|
|||
"""
|
||||
Invia un gatto in chat! 🐈
|
||||
"""
|
||||
log.debug("Evaluating config...")
|
||||
config = cfg.lazy_config.evaluate()
|
||||
|
||||
log.debug("Creating a new HTTP session")
|
||||
async with aiohttp.ClientSession() as session:
|
||||
|
@ -75,8 +78,12 @@ async def cat(*, _msg: engi.Message, **__):
|
|||
log.info("Making a GET request to retrieve a The Cat API image")
|
||||
async with session.get(selected_cat["url"]) as response:
|
||||
|
||||
filename = selected_cat["url"].split("/")[-1]
|
||||
path = pathlib.Path(config["files.cache.cat"]).joinpath(filename)
|
||||
log.debug("Saving cat to: %s", path)
|
||||
|
||||
with path.open("wb") as img:
|
||||
log.debug("Reading image bytes into memory")
|
||||
img = io.BytesIO()
|
||||
while img_data := await response.content.read(8192):
|
||||
img.write(img_data)
|
||||
img.seek(0)
|
||||
|
@ -84,6 +91,9 @@ async def cat(*, _msg: engi.Message, **__):
|
|||
log.debug("Sending image in the chat")
|
||||
await _msg.reply(files=[img])
|
||||
|
||||
log.debug("Deleting cat...")
|
||||
path.unlink()
|
||||
|
||||
|
||||
# Objects exported by this module
|
||||
__all__ = (
|
||||
|
|
|
@ -1,9 +1,14 @@
|
|||
import logging
|
||||
import io
|
||||
import pathlib
|
||||
|
||||
import aiohttp
|
||||
import royalnet.engineer as engi
|
||||
|
||||
import royalpack.bolts as rb
|
||||
import royalpack.config as cfg
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@rb.capture_errors
|
||||
|
@ -12,13 +17,28 @@ async def dog_any(*, _msg: engi.Message, **__):
|
|||
"""
|
||||
Invia un doggo in chat! 🐶
|
||||
"""
|
||||
|
||||
log.debug("Evaluating config...")
|
||||
config = cfg.lazy_config.evaluate()
|
||||
|
||||
async with aiohttp.ClientSession() as session:
|
||||
|
||||
log.debug("Fetching dog (not the opposite. ironic, huh?)")
|
||||
async with session.get("https://dog.ceo/api/breeds/image/random") as response:
|
||||
result = await response.json()
|
||||
url = result["message"]
|
||||
|
||||
filename = url.split("/")[-1]
|
||||
path = pathlib.Path(config["files.cache.dog"]).joinpath(filename)
|
||||
log.debug("Saving dog to: %s", path)
|
||||
|
||||
async with session.get(url) as response:
|
||||
img = await response.content.read()
|
||||
await _msg.reply(files=[io.BytesIO(img)])
|
||||
with path.open("wb") as img:
|
||||
img.write(await response.content.read())
|
||||
|
||||
await _msg.reply(files=[img])
|
||||
|
||||
path.unlink()
|
||||
|
||||
|
||||
_breeds = [
|
||||
|
|
Loading…
Reference in a new issue