Add support for local files in .cat
This commit is contained in:
parent
eebaeef2f5
commit
5727eaea20
|
@ -1,3 +1,4 @@
|
|||
import os
|
||||
import html
|
||||
import tempfile
|
||||
from pyrogram import Client, filters
|
||||
|
@ -7,26 +8,40 @@ from .. import config, help_dict, log_errors, session, progress_callback, public
|
|||
@log_errors
|
||||
@public_log_errors
|
||||
async def cat(client, message):
|
||||
media = message.document
|
||||
if not media and not getattr(message.reply_to_message, 'empty', True):
|
||||
media = message.reply_to_message.document
|
||||
if not media:
|
||||
await message.reply_text('Document required')
|
||||
return
|
||||
media = (message.text or message.caption).markdown.split(' ', 1)[1:]
|
||||
if media:
|
||||
media = os.path.expanduser(media[0])
|
||||
else:
|
||||
media = message.document
|
||||
if not media and not getattr(message.reply_to_message, 'empty', True):
|
||||
media = message.reply_to_message.document
|
||||
if not media:
|
||||
await message.reply_text('Document or local file path required')
|
||||
return
|
||||
done = False
|
||||
with tempfile.NamedTemporaryFile() as file:
|
||||
reply = await message.reply_text('Downloading...')
|
||||
await client.download_media(media, file_name=file.name, progress=progress_callback, progress_args=(reply, 'Downloading...', False))
|
||||
with open(file.name) as nfile:
|
||||
reply = rfile = None
|
||||
try:
|
||||
if not isinstance(media, str):
|
||||
rfile = tempfile.NamedTemporaryFile()
|
||||
reply = await message.reply_text('Downloading...')
|
||||
await client.download_media(media, file_name=rfile.name, progress=progress_callback, progress_args=(reply, 'Downloading...', False))
|
||||
media = rfile.name
|
||||
with open(media, 'rb') as file:
|
||||
while True:
|
||||
chunk = nfile.read(4096)
|
||||
chunk = file.read(4096)
|
||||
if not chunk:
|
||||
break
|
||||
chunk = f'<code>{html.escape(chunk)}</code>'
|
||||
if not chunk.strip():
|
||||
continue
|
||||
chunk = f'<code>{html.escape(chunk.decode())}</code>'
|
||||
if done:
|
||||
await message.reply_text(chunk, quote=False)
|
||||
else:
|
||||
await reply.edit_text(chunk)
|
||||
await getattr(reply, 'edit_text', message.reply_text)(chunk)
|
||||
done = True
|
||||
finally:
|
||||
if rfile:
|
||||
rfile.close()
|
||||
|
||||
help_dict['cat'] = ('cat', '{prefix}cat <i>(as caption of text file or reply)</i> - Outputs file\'s text to Telegram')
|
||||
help_dict['cat'] = ('cat', '''{prefix}cat <i>(as caption of text file or reply)</i> - Outputs file's text to Telegram
|
||||
{prefix}cat <i><path to local file></i> - Outputs file's text to Telegram''')
|
||||
|
|
Loading…
Reference in New Issue