From 79c2b27178aec60749cdbdfb4ff194fb767b0abc Mon Sep 17 00:00:00 2001 From: xtaodada Date: Mon, 9 Jan 2023 21:42:40 +0800 Subject: [PATCH] =?UTF-8?q?qr=20=E4=BA=8C=E7=BB=B4=E7=A0=81=E7=9B=B8?= =?UTF-8?q?=E5=85=B3=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- qr/main.py | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 qr/main.py diff --git a/qr/main.py b/qr/main.py new file mode 100644 index 0000000..1c24e3e --- /dev/null +++ b/qr/main.py @@ -0,0 +1,75 @@ +""" QR Code related utilities. """ + +from PIL import Image +from pagermaid import log, Config +from pagermaid.enums import Client, Message +from pagermaid.listener import listener +from pagermaid.single_utils import safe_remove +from pagermaid.utils import lang, pip_install + +pip_install("pyqrcode") +pip_install("pypng") +pip_install("pyzbar") + +from pyqrcode import create + + +@listener(is_plugin=False, outgoing=True, command="genqr", + description=lang('genqr_des'), + parameters="") +async def gen_qr(client: Client, message: Message): + """ Generate QR codes. """ + text = message.obtain_message() + if not text: + await message.edit(lang('error_prefix')) + return + if not Config.SILENT: + await message.edit(lang('genqr_process')) + try: + create(text, error="L", mode="binary").png("qr.webp", scale=6) + await client.send_document( + message.chat.id, + document="qr.webp", + reply_to_message_id=message.reply_to_message_id or message.reply_to_top_message_id, + ) + except UnicodeEncodeError: + await message.edit(f"{lang('error_prefix')}{lang('genqr_e_encode')}") + return + except Exception as e: + await message.edit(f"{lang('error_prefix')}\n{e}") + return + finally: + safe_remove("qr.webp") + await message.safe_delete() + await log(f"`{text}` {lang('genqr_ok')}") + + +@listener(is_plugin=False, outgoing=True, command="parseqr", + description=lang('parseqr_des')) +async def parse_qr(message: Message): + """ Parse attachment of replied message as a QR Code and output results. """ + try: + from pyzbar.pyzbar import decode as pyzbar_decode + except ImportError: + await message.edit(f"{lang('error_prefix')}请先在系统中安装 zbar 库。") + return + success = False + reply = message.reply_to_message + if message.document or message.photo or message.sticker: + target_file_path = await message.download() + elif reply and (reply.document or reply.photo or reply.sticker): + target_file_path = await reply.download() + else: + return await message.edit(f"{lang('error_prefix')}{lang('parseqr_nofile')}") + try: + text = str(pyzbar_decode(Image.open(target_file_path))[0].data)[2:][:-1] + success = True + await message.edit(f"**{lang('parseqr_content')}: **\n" + f"`{text}`") + except Exception: + await message.edit(f"{lang('error_prefix')}{lang('parseqr_e_noqr')}") + text = None + finally: + safe_remove(target_file_path) + if success: + await log(f"{lang('parseqr_log')} `{text}`.")