From 02049bf0c1e1f37195826dfe24c01849a93b92bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Mart=C3=ADnez=20Montan=C3=A9?= Date: Tue, 15 Oct 2019 11:24:47 +0200 Subject: [PATCH] Add files via upload --- app.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 app.py diff --git a/app.py b/app.py new file mode 100644 index 0000000..d0a6b63 --- /dev/null +++ b/app.py @@ -0,0 +1,42 @@ +import telebot +import time +import pafy +import os + +bot_token = '837420348:AAEY2WT04zBjpCHYvOzCdy4FHhZf8jX6udE' +bot = telebot.TeleBot(token=bot_token) + + +@bot.message_handler(commands=['start']) +def send_welcome(message): + bot.reply_to(message, 'Welcome! Send me a youtube video and i will send you an mp3 file!') + + +@bot.message_handler(func=lambda msg: msg.text is not None and ("youtube.com" in msg.text or "youtu.be" in msg.text)) +def send_song(message): + filename = download_song(message) + path = os.path.join(os.getcwd(), filename) + while not os.path.exists(path): + continue + bot.send_message(message.chat.id, "Song downloaded") + song = open(filename, 'rb') + bot.send_message(message.chat.id, "Sending...") + bot.send_audio(message.chat.id, song) + bot.send_message(message.chat.id, "Enjoy it!") + os.remove(filename) + + +def download_song(message): + youtube_url = message.text + bot.send_message(message.chat.id, "downloading...") + video = pafy.new(youtube_url) + bestaudio = video.getbestaudio() + filename = bestaudio.download() + return filename + + +while True: + try: + bot.polling() + except Exception: + time.sleep(15)