-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
67 lines (49 loc) · 2.09 KB
/
bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import re
import telebot
from telebot.types import InlineKeyboardMarkup, InlineKeyboardButton
TOKEN = 'ADD_YOUR_BOT_TOKEN_HERE'
bot = telebot.TeleBot(TOKEN)
@bot.message_handler(commands=['start'])
def send_welcome(message):
bot.reply_to(message, "Give me a YouTube video URL & i will send the thumbnail.")
@bot.message_handler(func=lambda message: True)
def select_quality(message):
URLpattern = r'(https?://)?(www\.)?(youtube\.com/watch\?v=|youtu\.be/)([\w-]+)'
YTurls = re.search(URLpattern, message.text)
if YTurls:
vidID = YTurls.group(4)
# vidURL = YTurls.group(0)
def gen_markup():
markup = InlineKeyboardMarkup()
markup.add(InlineKeyboardButton(text=f"High ✅", callback_data=f"high#{vidID}"))
markup.add(
InlineKeyboardButton(text=f"Medium",callback_data=f"medium#{vidID}"),
InlineKeyboardButton(text=f"Low", callback_data=f"low#{vidID}")
)
return markup
bot.reply_to(message=message, text="Choose a size:", reply_markup=gen_markup())
else:
bot.reply_to(message=message, text="Please send me a youtube link.")
@bot.callback_query_handler(func=lambda call: True)
def download_thumbnail(call):
data = call.data.split("#")
receivedData = data[0]
vidID = data[1]
chatID = call.message.chat.id
bot.delete_message(chat_id=call.message.chat.id, message_id=call.message.message_id)
highThumb = f"https://img.youtube.com/vi/{vidID}/maxresdefault.jpg"
mediumThumb = f"https://img.youtube.com/vi/{vidID}/hqdefault.jpg"
lowThumb = f"https://img.youtube.com/vi/{vidID}/mqdefault.jpg"
try:
match receivedData:
case "high":
bot.send_photo(chatID, highThumb)
case "medium":
bot.send_photo(chatID, mediumThumb)
case "low":
bot.send_photo(chatID, lowThumb)
except Exception as e:
bot.send_message(chatID, f"Error: {e}.")
print("\n - Error:", e)
print("YTThumbDownloader bot running..")
bot.infinity_polling()