Merge pull request #1 from JasonKhew96/fast

Add FastTelethon 🚀
This commit is contained in:
Benny 2021-05-05 11:12:20 +08:00 committed by GitHub
commit f4634d14e4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 2 deletions

3
.gitmodules vendored Normal file
View file

@ -0,0 +1,3 @@
[submodule "FastTelethon"]
path = FastTelethon
url = https://gist.github.com/painor/7e74de80ae0c819d3e9abcf9989a8dd6

1
FastTelethon Submodule

@ -0,0 +1 @@
Subproject commit a98abd5ff5cae3640e785611a38c0e213df56343

47
ytdl.py
View file

@ -18,7 +18,13 @@ import functools
import fakeredis
import youtube_dl
from FastTelethon.FastTelethon import upload_file
from hachoir.metadata import extractMetadata
from hachoir.parser import createParser
from telethon import TelegramClient, events
from telethon.tl.types import DocumentAttributeFilename, DocumentAttributeVideo
from telethon.utils import get_input_media
from hachoir.metadata.video import MkvMetadata
from tgbot_ping import get_runtime
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(filename)s [%(levelname)s]: %(message)s')
@ -150,8 +156,25 @@ async def echo_all(event):
async with bot.action(chat_id, 'document'):
video_path = result["filepath"]
await bot.edit_message(chat_id, message, 'Download complete. Sending now...')
await bot.send_file(chat_id, video_path,
progress_callback=lambda x, y: upload_callback(x, y, chat_id, message))
metadata, mime_type = get_metadata(video_path)
with open(video_path, 'rb') as f:
input_file = await upload_file(
bot,
f,
progress_callback=lambda x, y: upload_callback(
x, y, chat_id, message))
input_media = get_input_media(input_file)
input_media.attributes = [
DocumentAttributeVideo(
round_message=False,
supports_streaming=True,
**metadata
),
DocumentAttributeFilename(
os.path.basename(video_path)),
]
input_media.mime_type = mime_type
await bot.send_file(chat_id, input_media)
await bot.edit_message(chat_id, message, 'Download success!✅')
else:
async with bot.action(chat_id, 'typing'):
@ -162,6 +185,26 @@ async def echo_all(event):
temp_dir.cleanup()
def get_metadata(video_path):
try:
metadata = extractMetadata(createParser(video_path))
if isinstance(metadata, MkvMetadata):
return dict(
duration=metadata.get('duration').seconds,
w=metadata['video[1]'].get('width'),
h=metadata['video[1]'].get('height')
), metadata.get('mime_type')
else:
return dict(
duration=metadata.get('duration').seconds,
w=metadata.get('width'),
h=metadata.get('height')
), metadata.get('mime_type')
except Exception as e:
logging.error(e)
return dict(duration=0, w=0, h=0), 'application/octet-stream'
if __name__ == '__main__':
bot.start()
bot.run_until_disconnected()