Trying fix ocr and conversrt

This commit is contained in:
yasirarism 2023-05-29 14:17:45 +00:00 committed by GitHub
parent 4a72e140f2
commit 375be9672c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 45 deletions

View file

@ -119,29 +119,30 @@ async def ceksub(self: Client, ctx: Message, strings):
@use_chat_lang()
async def convertsrt(self: Client, ctx: Message, strings):
reply = ctx.reply_to_message
if not reply or not reply.document and not (reply.document.file_name.endswith(".vtt") or reply.document.file_name.endswith(".ass")):
return await ctx.reply_msg(strings("conv_sub_help").format(cmd=ctx.command[0]), del_in=5)
msg = await ctx.reply_msg(strings("convert_str"), quote=True)
if not os.path.exists("downloads"):
os.makedirs("downloads")
dl = await reply.download(file_name="downloads/")
filename = dl.split("/", 3)[3]
LOGGER.info(f"ConvertSub: {filename} by {ctx.from_user.first_name if ctx.from_user else ctx.sender_chat.title} [{ctx.from_user.id if ctx.from_user else ctx.sender_chat.id}]")
(await shell_exec(f"{FF_MPEG_NAME} -i '{dl}' 'downloads/{filename}.srt'"))[0]
c_time = time()
await ctx.reply_document(
f"downloads/{filename}.srt",
caption=strings("capt_conv_sub").format(nf=filename, bot=self.me.username),
thumb="assets/thumb.jpg",
progress=progress_for_pyrogram,
progress_args=(strings("up_str"), msg, c_time, self.me.dc_id),
)
await msg.delete_msg()
try:
os.remove(dl)
os.remove(f"downloads/{filename}.srt")
except:
pass
if replied and replied.document and (replied.document.file_name and replied.document.file_name.endswith(".vtt") or replied.document.file_name.endswith(".ass")):
msg = await ctx.reply_msg(strings("convert_str"), quote=True)
if not os.path.exists("downloads"):
os.makedirs("downloads")
dl = await reply.download(file_name="downloads/")
filename = dl.split("/", 3)[3]
LOGGER.info(f"ConvertSub: {filename} by {ctx.from_user.first_name if ctx.from_user else ctx.sender_chat.title} [{ctx.from_user.id if ctx.from_user else ctx.sender_chat.id}]")
(await shell_exec(f"{FF_MPEG_NAME} -i '{dl}' 'downloads/{filename}.srt'"))[0]
c_time = time()
await ctx.reply_document(
f"downloads/{filename}.srt",
caption=strings("capt_conv_sub").format(nf=filename, bot=self.me.username),
thumb="assets/thumb.jpg",
progress=progress_for_pyrogram,
progress_args=(strings("up_str"), msg, c_time, self.me.dc_id),
)
await msg.delete_msg()
try:
os.remove(dl)
os.remove(f"downloads/{filename}.srt")
except:
pass
else:
return await ctx.reply_msg(strings("conv_sub_help").format(cmd=ctx.command[0]), del_in=6)
@app.on_callback_query(filters.regex(r"^streamextract#"))

View file

@ -30,25 +30,26 @@ __HELP__ = "/ocr [reply to photo] - Read Text From Image"
@use_chat_lang()
async def ocr(self: Client, ctx: Message, strings):
reply = ctx.reply_to_message
if not reply or not (reply.document and reply.document.mime_type.startswith("image")) or 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 if ctx.from_user else ctx.sender_chat.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"]))
if os.path.exists(file_path):
os.remove(file_path)
except Exception as e:
await msg.edit_msg(str(e))
if os.path.exists(file_path):
os.remove(file_path)
if reply and (reply.sticker or reply.photo or reply.document and reply.document.mime_type.startswith("image")):
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 if ctx.from_user else ctx.sender_chat.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"]))
if os.path.exists(file_path):
os.remove(file_path)
except Exception as e:
await msg.edit_msg(str(e))
if os.path.exists(file_path):
os.remove(file_path)
else:
return await ctx.reply_msg(strings("no_photo").format(cmd=ctx.command[0]), quote=True)