Use FastTelethon 🚀

This commit is contained in:
jasonkhew96 2021-05-04 20:42:13 +08:00
parent a783709355
commit 1ded70780e
No known key found for this signature in database
GPG key ID: DACCAB1D3735A97D
2 changed files with 48 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

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,27 @@ 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))
d, w, h, 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(
duration=d,
w=w,
h=h,
round_message=False,
supports_streaming=True
),
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 +187,24 @@ async def echo_all(event):
temp_dir.cleanup()
def get_metadata(video_path):
try:
metadata = extractMetadata(createParser(video_path))
if isinstance(metadata, MkvMetadata):
return (metadata.get('duration').seconds,
metadata['video[1]'].get('width'),
metadata['video[1]'].get('height'),
metadata.get('mime_type'))
else:
return (metadata.get('duration').seconds,
metadata.get('width'),
metadata.get('height'),
metadata.get('mime_type'))
except Exception as e:
logging.error(e)
return (0, 0, 0, 'application/octet-stream')
if __name__ == '__main__':
bot.start()
bot.run_until_disconnected()