MissKatyPyro/misskaty/plugins/ocr.py
yasirarism 924ba30a8d
Some Major Change (#50)
* Big Update Coming

* reformating: code

* 'Refactored by Sourcery' (#51)

Co-authored-by: Sourcery AI <>

---------

Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
Co-authored-by: methneviebyvaet <77743895+meth1337@users.noreply.github.com>
2023-04-16 11:38:34 +07:00

52 lines
1.9 KiB
Python

"""
* @author yasir <yasiramunandar@gmail.com>
* @date 2022-12-01 09:12:27
* @lastModified 2022-12-01 09:32:31
* @projectName MissKatyPyro
* Copyright @YasirPedia All rights reserved
"""
import os
from pyrogram import filters, Client
from pyrogram.types import Message
from telegraph.aio import Telegraph
from misskaty import app
from misskaty.core.decorator.ratelimiter import ratelimiter
from misskaty.core.decorator.errors import capture_err
from misskaty.helper.localization import use_chat_lang
from misskaty.helper.http import http
from misskaty.vars import COMMAND_HANDLER
__MODULE__ = "OCR"
__HELP__ = "/ocr [reply to photo] - Read Text From Image"
@app.on_message(filters.command(["ocr"], COMMAND_HANDLER))
@capture_err
@ratelimiter
@use_chat_lang()
async def ocr(self: Client, ctx: Message, strings):
reply = ctx.reply_to_message
if not reply or not reply.photo and (reply.document and not reply.document.mime_type.startswith("image")) and not reply.sticker:
return await ctx.reply_msg(strings("no_photo").format(cmd=ctx.command[0]), quote=True, del_in=6)
msg = await ctx.reply_msg(strings("read_ocr"), quote=True)
try:
file_path = await reply.download()
if reply.sticker:
file_path = await reply.download(f"ocr_{ctx.from_user.id}.jpg")
response = await Telegraph().upload_file(file_path)
url = f"https://telegra.ph{response[0]['src']}"
req = (
await http.get(
f"https://script.google.com/macros/s/AKfycbwURISN0wjazeJTMHTPAtxkrZTWTpsWIef5kxqVGoXqnrzdLdIQIfLO7jsR5OQ5GO16/exec?url={url}",
follow_redirects=True,
)
).json()
await msg.edit_msg(strings("result_ocr").format(result=req["text"]))
os.remove(file_path)
except Exception as e:
await msg.edit_msg(str(e))
os.remove(file_path)