|
1 | | -import os |
2 | | -import telebot |
| 1 | +import os, re |
3 | 2 | import threading |
| 3 | +import telebot |
4 | 4 |
|
5 | | -from modules import checker, myqueues |
| 5 | +from dotenv import load_dotenv |
6 | 6 |
|
7 | | -from dotenv import load_dotenv |
8 | 7 | load_dotenv() |
9 | 8 |
|
| 9 | +from modules.checker import youtube_regex |
| 10 | +from modules.downloader import download_video |
| 11 | + |
10 | 12 | TOKEN = os.getenv("BOT_API_KEY") |
11 | 13 |
|
12 | 14 | bot = telebot.TeleBot(TOKEN, parse_mode="HTML") |
13 | | - |
14 | | -@bot.message_handler(commands=['start']) |
| 15 | + |
| 16 | + |
| 17 | +# '/start' command reply |
| 18 | +@bot.message_handler(commands=["start"]) |
15 | 19 | def send_welcome(message): |
16 | 20 | bot.reply_to( |
17 | | - message, "Hello, I'm a <b>Simple Youtube Downloader!👋</b>\n\nTo get started, just type the /help command.") |
| 21 | + message, |
| 22 | + "Hello, I'm a <b>Simple Youtube Downloader!👋</b>\n\nTo get started, just type the /help command.", |
| 23 | + ) |
| 24 | + |
18 | 25 |
|
19 | | -@bot.message_handler(commands=['help']) |
| 26 | +# '/help' command reply |
| 27 | +@bot.message_handler(commands=["help"]) |
20 | 28 | def send_help(message): |
21 | 29 | bot.reply_to( |
22 | 30 | message, |
23 | 31 | """ |
24 | | - <b>Just send your youtube link and select the video quality.</b> 😉 |
25 | | - <i> |
26 | | - Developer: @dev00111 |
27 | | - Source: <a href="https://github.com/hansanaD/TelegramYTDLBot">TelegramYTDLBot</a></i> |
28 | | - """, disable_web_page_preview=True,) |
29 | | - |
30 | | - |
| 32 | + <b>Just send me your video link.</b> ▶️ |
| 33 | +
|
| 34 | +<i>Source: <a href="https://github.com/hansanaD/TelegramYTDLBot">TelegramYTDLBot</a> by <a href="https://github.com/DevHanza/">DevHanza</a></i> |
| 35 | + """, |
| 36 | + disable_web_page_preview=True, |
| 37 | + ) |
31 | 38 |
|
| 39 | + |
| 40 | +# Youtube Link Listener |
32 | 41 | @bot.message_handler(func=lambda m: True) |
33 | | -def link_check(message): |
34 | | - checker.linkCheck(bot=bot, message=message) |
35 | | - # print(checker.videoURL) |
36 | | - |
37 | | -# Callback handler for # getVidInfo() |
38 | | -@bot.callback_query_handler(func=lambda call: [call.data == item for item in checker.showList]) |
39 | | -def callback_query(call): |
40 | | - |
41 | | - data = call.data.split("#") |
42 | | - receivedData = data[0] |
43 | | - videoURL = data[1] |
44 | | - # print(receivedData) |
45 | | - |
46 | | - bot.answer_callback_query(call.id, f"Selected {receivedData} to download.") |
47 | | - bot.delete_message(chat_id=call.message.chat.id, message_id=call.message.message_id) |
48 | | - |
49 | | - myqueues.download_queue.put((call.message, videoURL, receivedData)) |
50 | | - queue_position = myqueues.download_queue.qsize() |
51 | | - |
52 | | - |
53 | | - if queue_position == 0 & 1: |
54 | | - bot.send_message(call.message.chat.id, f"Download has been added to the queue.") |
55 | | - else: |
56 | | - bot.send_message(call.message.chat.id, f"Download has been added to the queue at #{queue_position}.") |
57 | | - |
58 | | - |
59 | | - # downloader.download(bot=bot, message=call.message, userInput=receivedData, videoURL=checker.videoURL) |
60 | | - # bot.send_message(call.message.chat.id, f"{videoURL} \n{receivedData} : Download Triggered!") |
61 | | - |
62 | | -# message, videoURL, receivedData |
63 | | - |
64 | | -download_thread = threading.Thread(target=myqueues.download_worker, args=(bot, myqueues.download_queue)) |
65 | | -download_thread.daemon = True |
66 | | -download_thread.start() |
| 42 | +def on_yt_link(message): |
| 43 | + # run download in background so bot stays responsive |
| 44 | + threading.Thread(target=yt_link_handler, args=(message,)).start() |
| 45 | + |
| 46 | + |
| 47 | +def yt_link_handler(message): |
| 48 | + |
| 49 | + matches = re.findall(youtube_regex, message.text) |
| 50 | + |
| 51 | + if matches: |
| 52 | + |
| 53 | + url = matches[0] |
| 54 | + status_msg = bot.reply_to(message, f"Starting to download..") |
| 55 | + |
| 56 | + try: |
| 57 | + file_path = download_video(url, bot, message.chat.id, status_msg.message_id) |
| 58 | + |
| 59 | + with open(file_path, "rb") as file: |
| 60 | + bot.send_video(message.chat.id, file) |
| 61 | + |
| 62 | + os.remove(file_path) |
| 63 | + |
| 64 | + except Exception as e: |
| 65 | + bot.reply_to(message, f"❌ Error: {e}") |
| 66 | + |
67 | 67 |
|
68 | 68 | print("TelegramYTDLBot is running..\n") |
69 | 69 | bot.infinity_polling() |
0 commit comments