1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-27 05:24:20 +00:00
royalnet/royalpack/commands/dog.py

222 lines
4.9 KiB
Python
Raw Normal View History

2023-12-17 04:15:19 +00:00
import logging
2021-04-18 14:47:47 +00:00
import io
2023-12-17 04:15:19 +00:00
import pathlib
2021-04-18 14:47:47 +00:00
2021-04-19 17:39:01 +00:00
import aiohttp
import royalnet.engineer as engi
2021-04-30 13:22:58 +00:00
import royalpack.bolts as rb
2023-12-17 04:15:19 +00:00
import royalpack.config as cfg
log = logging.getLogger(__name__)
2021-04-19 17:39:01 +00:00
2021-04-18 14:47:47 +00:00
2021-04-30 13:22:58 +00:00
@rb.capture_errors
2021-04-18 14:47:47 +00:00
@engi.TeleportingConversation
async def dog_any(*, _msg: engi.Message, **__):
"""
Invia un doggo in chat! 🐶
"""
2023-12-17 04:15:19 +00:00
log.debug("Evaluating config...")
config = cfg.lazy_config.evaluate()
2021-04-18 14:47:47 +00:00
async with aiohttp.ClientSession() as session:
2023-12-17 04:15:19 +00:00
log.debug("Fetching dog (not the opposite. ironic, huh?)")
2021-04-18 14:47:47 +00:00
async with session.get("https://dog.ceo/api/breeds/image/random") as response:
result = await response.json()
url = result["message"]
2023-12-17 04:15:19 +00:00
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:
with path.open("wb") as img:
img.write(await response.content.read())
2023-12-17 05:19:26 +00:00
with path.open("rb") as img:
await _msg.reply(files=[img])
2023-12-17 04:15:19 +00:00
path.unlink()
2021-04-18 14:47:47 +00:00
_breeds = [
"affenpinscher",
"african",
"airedale",
"akita",
"appenzeller",
"australian/shepherd",
"basenji",
"beagle",
"bluetick",
"borzoi",
"bouvier",
"boxer",
"brabancon",
"briard",
"buhund/norwegian",
"bulldog/boston",
"bulldog/english",
"bulldog/french",
"bullterrier/staffordshire",
"cairn",
"cattledog/australian",
"chihuahua",
"chow",
"clumber",
"cockapoo",
"collie/border",
"coonhound",
"corgi/cardigan",
"cotondetulear",
"dachshund",
"dalmatian",
"dane/great",
"deerhound/scottish",
"dhole",
"dingo",
"doberman",
"elkhound/norwegian",
"entlebucher",
"eskimo",
"finnish/lapphund",
"frise/bichon",
"germanshepherd",
"greyhound/italian",
"groenendael",
"havanese",
"hound/afghan",
"hound/basset",
"hound/blood",
"hound/english",
"hound/ibizan",
"hound/plott",
"hound/walker",
"husky",
"keeshond",
"kelpie",
"komondor",
"kuvasz",
"labrador",
"leonberg",
"lhasa",
"malamute",
"malinois",
"maltese",
"mastiff/bull",
"mastiff/english",
"mastiff/tibetan",
"mexicanhairless",
"mix",
"mountain/bernese",
"mountain/swiss",
"newfoundland",
"otterhound",
"ovcharka/caucasian",
"papillon",
"pekinese",
"pembroke",
"pinscher/miniature",
"pitbull",
"pointer/german",
"pointer/germanlonghair",
"pomeranian",
"poodle/miniature",
"poodle/standard",
"poodle/toy",
"pug",
"puggle",
"pyrenees",
"redbone",
"retriever/chesapeake",
"retriever/curly",
"retriever/flatcoated",
"retriever/golden",
"ridgeback/rhodesian",
"rottweiler",
"saluki",
"samoyed",
"schipperke",
"schnauzer/giant",
"schnauzer/miniature",
"setter/english",
"setter/gordon",
"setter/irish",
"sheepdog/english",
"sheepdog/shetland",
"shiba",
"shihtzu",
"spaniel/blenheim",
"spaniel/brittany",
"spaniel/cocker",
"spaniel/irish",
"spaniel/japanese",
"spaniel/sussex",
"spaniel/welsh",
"springer/english",
"stbernard",
"terrier/american",
"terrier/australian",
"terrier/bedlington",
"terrier/border",
"terrier/dandie",
"terrier/fox",
"terrier/irish",
"terrier/kerryblue",
"terrier/lakeland",
"terrier/norfolk",
"terrier/norwich",
"terrier/patterdale",
"terrier/russell",
"terrier/scottish",
"terrier/sealyham",
"terrier/silky",
"terrier/tibetan",
"terrier/toy",
"terrier/westhighland",
"terrier/wheaten",
"terrier/yorkshire",
"vizsla",
"waterdog/spanish",
"weimaraner",
"whippet",
"wolfhound/irish",
]
@engi.TeleportingConversation
async def dog_breed(*, _msg: engi.Message, breed: str, **__):
"""
Invia un doggo di una razza specifica in chat! 🐶
"""
breed = breed.lower()
if breed not in _breeds:
await _msg.reply(text="⚠️ La razza che hai specificato non esiste nel database.")
async with aiohttp.ClientSession() as session:
async with session.get(f"https://dog.ceo/api/breed/{breed}/images/random") as response:
result = await response.json()
url = result["message"]
async with session.get(url) as response:
img = await response.content.read()
await _msg.reply(files=[io.BytesIO(img)])
@engi.TeleportingConversation
async def dog_breedlist(*, _msg: engi.Message, **__):
"""
Elenca tutte le razze di dogghi disponibili! 🐶
"""
msg = [
2021-04-20 00:43:48 +00:00
"🐶 \uE01BRazze disponibili:\uE00B",
2021-04-18 14:47:47 +00:00
", ".join(_breeds),
]
await _msg.reply(text="\n".join(msg))
__all__ = ("dog_any", "dog_breed", "dog_breedlist")