2021-05-03 11:26:47 +00:00
|
|
|
|
#!/usr/local/bin/python3
|
|
|
|
|
# coding: utf-8
|
|
|
|
|
|
2021-08-14 09:57:42 +00:00
|
|
|
|
# ytdlbot - new.py
|
|
|
|
|
# 8/14/21 14:37
|
2021-05-03 11:26:47 +00:00
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
__author__ = "Benny <benny.think@gmail.com>"
|
|
|
|
|
|
2021-07-11 15:01:32 +00:00
|
|
|
|
import logging
|
2021-05-03 11:26:47 +00:00
|
|
|
|
import os
|
2021-08-14 09:57:42 +00:00
|
|
|
|
|
2021-05-04 07:58:01 +00:00
|
|
|
|
import re
|
2021-07-11 15:01:32 +00:00
|
|
|
|
import tempfile
|
2021-08-14 09:57:42 +00:00
|
|
|
|
import typing
|
2021-05-09 12:12:58 +00:00
|
|
|
|
|
2021-08-14 09:57:42 +00:00
|
|
|
|
from pyrogram import Client, filters, types
|
|
|
|
|
from pyrogram.types import InlineKeyboardButton, InlineKeyboardMarkup
|
2021-05-03 11:26:47 +00:00
|
|
|
|
from tgbot_ping import get_runtime
|
|
|
|
|
|
2021-08-14 09:57:42 +00:00
|
|
|
|
from downloader import convert_flac, upload_hook, ytdl_download
|
2021-05-05 04:07:03 +00:00
|
|
|
|
|
2021-05-03 11:26:47 +00:00
|
|
|
|
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(filename)s [%(levelname)s]: %(message)s')
|
2021-08-14 09:57:42 +00:00
|
|
|
|
api_id = int(os.getenv("APP_ID", 0))
|
|
|
|
|
api_hash = os.getenv("APP_HASH")
|
|
|
|
|
token = os.getenv("TOKEN")
|
2021-08-15 07:14:41 +00:00
|
|
|
|
app = Client("ytdl", api_id, api_hash, bot_token=token, workers=100)
|
2021-05-05 06:56:47 +00:00
|
|
|
|
|
2021-05-04 03:30:22 +00:00
|
|
|
|
|
2021-08-14 09:57:42 +00:00
|
|
|
|
@app.on_message(filters.command(["start"]))
|
|
|
|
|
def start_handler(client: "Client", message: "types.Message"):
|
|
|
|
|
chat_id = message.chat.id
|
|
|
|
|
logging.info("Welcome to youtube-dl bot!")
|
|
|
|
|
client.send_chat_action(chat_id, "typing")
|
|
|
|
|
client.send_message(message.chat.id, "Wrapper for youtube-dl.")
|
2021-05-04 03:30:22 +00:00
|
|
|
|
|
2021-05-04 07:58:01 +00:00
|
|
|
|
|
2021-08-14 09:57:42 +00:00
|
|
|
|
@app.on_message(filters.command(["help"]))
|
|
|
|
|
def help_handler(client: "Client", message: "types.Message"):
|
|
|
|
|
chat_id = message.chat.id
|
|
|
|
|
client.send_chat_action(chat_id, "typing")
|
|
|
|
|
client.send_message(chat_id, "Stop working? "
|
|
|
|
|
"Wait a few seconds, send your link again or report bugs at "
|
|
|
|
|
"https://github.com/tgbot-collection/ytdl-bot/issues")
|
2021-05-04 07:58:01 +00:00
|
|
|
|
|
|
|
|
|
|
2021-08-14 09:57:42 +00:00
|
|
|
|
@app.on_message(filters.command(["ping"]))
|
|
|
|
|
def ping_handler(client: "Client", message: "types.Message"):
|
|
|
|
|
chat_id = message.chat.id
|
|
|
|
|
client.send_chat_action(chat_id, "typing")
|
|
|
|
|
bot_info = get_runtime("botsrunner_ytdl_1", "YouTube-dl")
|
|
|
|
|
client.send_message(chat_id, bot_info)
|
2021-05-10 09:19:45 +00:00
|
|
|
|
|
2021-05-03 11:26:47 +00:00
|
|
|
|
|
2021-08-14 09:57:42 +00:00
|
|
|
|
@app.on_message(filters.command(["about"]))
|
|
|
|
|
def help_handler(client: "Client", message: "types.Message"):
|
|
|
|
|
chat_id = message.chat.id
|
|
|
|
|
client.send_chat_action(chat_id, "typing")
|
|
|
|
|
client.send_message(chat_id, "YouTube-DL by @BennyThink\n"
|
|
|
|
|
"GitHub: https://github.com/tgbot-collection/ytdl-bot")
|
2021-05-03 11:26:47 +00:00
|
|
|
|
|
2021-05-13 11:14:01 +00:00
|
|
|
|
|
2021-08-14 09:57:42 +00:00
|
|
|
|
@app.on_message()
|
|
|
|
|
def download_handler(client: "Client", message: "types.Message"):
|
2021-08-15 07:14:41 +00:00
|
|
|
|
if message.chat.type != "private" and not message.text.lower().startswith("/ytdl"):
|
|
|
|
|
logging.warning("%s, it's annoying me...🙄️ ", message.text)
|
|
|
|
|
return
|
|
|
|
|
|
2021-08-14 09:57:42 +00:00
|
|
|
|
chat_id = message.chat.id
|
|
|
|
|
url = re.sub(r'/ytdl\s*', '', message.text)
|
|
|
|
|
logging.info("start %s", url)
|
2021-05-13 11:14:01 +00:00
|
|
|
|
|
2021-08-14 09:57:42 +00:00
|
|
|
|
if not re.findall(r"^https?://", url.lower()):
|
|
|
|
|
message.reply_text("I think you should send me a link.", quote=True)
|
|
|
|
|
return
|
2021-05-05 06:56:47 +00:00
|
|
|
|
|
2021-08-14 09:57:42 +00:00
|
|
|
|
bot_msg: typing.Union["types.Message", "typing.Any"] = message.reply_text("Processing", quote=True)
|
|
|
|
|
client.send_chat_action(chat_id, 'upload_video')
|
|
|
|
|
temp_dir = tempfile.TemporaryDirectory()
|
2021-05-05 06:56:47 +00:00
|
|
|
|
|
2021-08-14 09:57:42 +00:00
|
|
|
|
result = ytdl_download(url, temp_dir.name, bot_msg)
|
|
|
|
|
logging.info("Download complete.")
|
2021-05-05 06:56:47 +00:00
|
|
|
|
|
2021-08-14 09:57:42 +00:00
|
|
|
|
markup = InlineKeyboardMarkup(
|
|
|
|
|
[
|
|
|
|
|
[ # First row
|
|
|
|
|
InlineKeyboardButton( # Generates a callback query when pressed
|
|
|
|
|
"audio",
|
|
|
|
|
callback_data="audio"
|
|
|
|
|
)
|
|
|
|
|
]
|
|
|
|
|
]
|
|
|
|
|
)
|
2021-05-05 06:56:47 +00:00
|
|
|
|
|
2021-08-14 09:57:42 +00:00
|
|
|
|
if result["status"]:
|
|
|
|
|
client.send_chat_action(chat_id, 'upload_document')
|
|
|
|
|
video_path = result["filepath"]
|
|
|
|
|
bot_msg.edit_text('Download complete. Sending now...')
|
|
|
|
|
client.send_video(chat_id, video_path, supports_streaming=True, caption=url,
|
|
|
|
|
progress=upload_hook, progress_args=(bot_msg,), reply_markup=markup)
|
|
|
|
|
bot_msg.edit_text('Download success!✅')
|
|
|
|
|
else:
|
|
|
|
|
client.send_chat_action(chat_id, 'typing')
|
|
|
|
|
tb = result["error"][0:4000]
|
|
|
|
|
bot_msg.edit_text(f"{url} download failed❌:\n```{tb}```")
|
2021-05-03 11:26:47 +00:00
|
|
|
|
|
2021-08-14 09:57:42 +00:00
|
|
|
|
temp_dir.cleanup()
|
2021-05-03 11:26:47 +00:00
|
|
|
|
|
2021-07-11 15:29:12 +00:00
|
|
|
|
|
2021-08-14 09:57:42 +00:00
|
|
|
|
@app.on_callback_query()
|
|
|
|
|
def answer(client: "Client", callback_query: types.CallbackQuery):
|
|
|
|
|
callback_query.answer(f"Converting to audio...please wait patiently")
|
|
|
|
|
msg = callback_query.message
|
2021-07-11 15:29:12 +00:00
|
|
|
|
|
2021-08-14 09:57:42 +00:00
|
|
|
|
chat_id = msg.chat.id
|
|
|
|
|
mp4_name = msg.video.file_name # 'youtube-dl_test_video_a.mp4'
|
2021-07-11 16:07:26 +00:00
|
|
|
|
flac_name = mp4_name.replace("mp4", "m4a")
|
2021-07-11 15:01:32 +00:00
|
|
|
|
|
|
|
|
|
with tempfile.NamedTemporaryFile() as tmp:
|
2021-08-14 09:57:42 +00:00
|
|
|
|
logging.info("downloading to %s", tmp.name)
|
|
|
|
|
client.send_chat_action(chat_id, 'record_video_note')
|
|
|
|
|
client.download_media(msg, tmp.name)
|
|
|
|
|
logging.info("downloading complete %s", tmp.name)
|
2021-07-11 15:01:32 +00:00
|
|
|
|
# execute ffmpeg
|
2021-08-14 09:57:42 +00:00
|
|
|
|
client.send_chat_action(chat_id, 'record_audio')
|
|
|
|
|
flac_tmp = convert_flac(flac_name, tmp)
|
|
|
|
|
client.send_chat_action(chat_id, 'upload_audio')
|
|
|
|
|
client.send_audio(chat_id, flac_tmp)
|
2021-05-03 11:26:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2021-08-14 09:57:42 +00:00
|
|
|
|
app.run()
|