57 lines
2.0 KiB
Python
57 lines
2.0 KiB
Python
|
""" QR Code related utilities. """
|
||
|
|
||
|
from os import remove
|
||
|
from pyqrcode import create
|
||
|
from pyzbar.pyzbar import decode
|
||
|
from PIL import Image
|
||
|
from pagermaid import log
|
||
|
from pagermaid.listener import listener
|
||
|
from pagermaid.utils import obtain_message, upload_attachment, lang, alias_command
|
||
|
|
||
|
|
||
|
@listener(is_plugin=False, incoming=True, command=alias_command("genqr"),
|
||
|
description=lang('genqr_des'),
|
||
|
parameters="<string>")
|
||
|
async def genqr(context):
|
||
|
""" Generate QR codes. """
|
||
|
reply_id = context.reply_to_msg_id
|
||
|
try:
|
||
|
message = await obtain_message(context)
|
||
|
except ValueError:
|
||
|
await context.reply(lang('error_prefix'))
|
||
|
return
|
||
|
msg = await context.reply(lang('genqr_process'))
|
||
|
try:
|
||
|
create(message, error='L', mode='binary').png('qr.webp', scale=6)
|
||
|
except UnicodeEncodeError:
|
||
|
await msg.edit(f"{lang('error_prefix')}{lang('genqr_e_encode')}")
|
||
|
return
|
||
|
await upload_attachment("qr.webp", context.chat_id, reply_id)
|
||
|
remove("qr.webp")
|
||
|
await msg.delete()
|
||
|
await log(f"`{message}` {lang('genqr_ok')}")
|
||
|
|
||
|
|
||
|
@listener(is_plugin=False, incoming=True, command=alias_command("parseqr"),
|
||
|
description=lang('parseqr_des'))
|
||
|
async def parseqr(context):
|
||
|
""" Parse attachment of replied message as a QR Code and output results. """
|
||
|
success = False
|
||
|
target_file_path = await context.client.download_media(
|
||
|
await context.get_reply_message()
|
||
|
)
|
||
|
if not target_file_path:
|
||
|
await context.edit(f"{lang('error_prefix')}{lang('parseqr_nofile')}")
|
||
|
return
|
||
|
try:
|
||
|
message = str(decode(Image.open(target_file_path))[0].data)[2:][:-1]
|
||
|
success = True
|
||
|
await context.reply(f"**{lang('parseqr_content')}: **\n"
|
||
|
f"`{message}`")
|
||
|
except IndexError:
|
||
|
await context.reply(f"{lang('error_prefix')}{lang('parseqr_e_noqr')}")
|
||
|
message = None
|
||
|
if success:
|
||
|
await log(f"{lang('parseqr_log')} `{message}`.")
|
||
|
remove(target_file_path)
|