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 logging
|
||||||
|
import pathlib
|
||||||
|
|
||||||
import aiohttp
|
import aiohttp
|
||||||
import royalnet.engineer as engi
|
import royalnet.engineer as engi
|
||||||
|
|
||||||
import royalpack.bolts as rb
|
import royalpack.bolts as rb
|
||||||
|
import royalpack.config as cfg
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -15,6 +16,8 @@ async def cat(*, _msg: engi.Message, **__):
|
||||||
"""
|
"""
|
||||||
Invia un gatto in chat! 🐈
|
Invia un gatto in chat! 🐈
|
||||||
"""
|
"""
|
||||||
|
log.debug("Evaluating config...")
|
||||||
|
config = cfg.lazy_config.evaluate()
|
||||||
|
|
||||||
log.debug("Creating a new HTTP session")
|
log.debug("Creating a new HTTP session")
|
||||||
async with aiohttp.ClientSession() as session:
|
async with aiohttp.ClientSession() as session:
|
||||||
|
@ -75,14 +78,21 @@ async def cat(*, _msg: engi.Message, **__):
|
||||||
log.info("Making a GET request to retrieve a The Cat API image")
|
log.info("Making a GET request to retrieve a The Cat API image")
|
||||||
async with session.get(selected_cat["url"]) as response:
|
async with session.get(selected_cat["url"]) as response:
|
||||||
|
|
||||||
log.debug("Reading image bytes into memory")
|
filename = selected_cat["url"].split("/")[-1]
|
||||||
img = io.BytesIO()
|
path = pathlib.Path(config["files.cache.cat"]).joinpath(filename)
|
||||||
while img_data := await response.content.read(8192):
|
log.debug("Saving cat to: %s", path)
|
||||||
img.write(img_data)
|
|
||||||
img.seek(0)
|
|
||||||
|
|
||||||
log.debug("Sending image in the chat")
|
with path.open("wb") as img:
|
||||||
await _msg.reply(files=[img])
|
log.debug("Reading image bytes into memory")
|
||||||
|
while img_data := await response.content.read(8192):
|
||||||
|
img.write(img_data)
|
||||||
|
img.seek(0)
|
||||||
|
|
||||||
|
log.debug("Sending image in the chat")
|
||||||
|
await _msg.reply(files=[img])
|
||||||
|
|
||||||
|
log.debug("Deleting cat...")
|
||||||
|
path.unlink()
|
||||||
|
|
||||||
|
|
||||||
# Objects exported by this module
|
# Objects exported by this module
|
||||||
|
|
|
@ -1,9 +1,14 @@
|
||||||
|
import logging
|
||||||
import io
|
import io
|
||||||
|
import pathlib
|
||||||
|
|
||||||
import aiohttp
|
import aiohttp
|
||||||
import royalnet.engineer as engi
|
import royalnet.engineer as engi
|
||||||
|
|
||||||
import royalpack.bolts as rb
|
import royalpack.bolts as rb
|
||||||
|
import royalpack.config as cfg
|
||||||
|
|
||||||
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
@rb.capture_errors
|
@rb.capture_errors
|
||||||
|
@ -12,13 +17,28 @@ async def dog_any(*, _msg: engi.Message, **__):
|
||||||
"""
|
"""
|
||||||
Invia un doggo in chat! 🐶
|
Invia un doggo in chat! 🐶
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
log.debug("Evaluating config...")
|
||||||
|
config = cfg.lazy_config.evaluate()
|
||||||
|
|
||||||
async with aiohttp.ClientSession() as session:
|
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:
|
async with session.get("https://dog.ceo/api/breeds/image/random") as response:
|
||||||
result = await response.json()
|
result = await response.json()
|
||||||
url = result["message"]
|
url = result["message"]
|
||||||
async with session.get(url) as response:
|
|
||||||
img = await response.content.read()
|
filename = url.split("/")[-1]
|
||||||
await _msg.reply(files=[io.BytesIO(img)])
|
path = pathlib.Path(config["files.cache.dog"]).joinpath(filename)
|
||||||
|
log.debug("Saving dog to: %s", path)
|
||||||
|
|
||||||
|
async with session.get(url) as response:
|
||||||
|
with path.open("wb") as img:
|
||||||
|
img.write(await response.content.read())
|
||||||
|
|
||||||
|
await _msg.reply(files=[img])
|
||||||
|
|
||||||
|
path.unlink()
|
||||||
|
|
||||||
|
|
||||||
_breeds = [
|
_breeds = [
|
||||||
|
|
Loading…
Reference in a new issue