-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
189 lines (151 loc) · 7.02 KB
/
main.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/usr/bin/env python3
# coding: utf-8
import os
import shlex, logging, json
from subprocess import run, CalledProcessError
from functools import wraps
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import MessageHandler, CallbackQueryHandler, CallbackContext, Updater, CommandHandler, RegexHandler, Filters
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
logger = logging.getLogger(__name__)
with open('settings.json') as f:
settings = json.load(f)
TOKEN = settings['TOKEN']
AUTHORIZED_USERS = settings['AUTHORIZED_USERS']
YTDLP_PATH = settings['YTDLP_PATH']
DOWNLOAD_DIR = settings['DOWNLOAD_DIR']
def restricted(func):
@wraps(func)
def wrapped(update: Update, context: CallbackContext):
user_id = update.effective_user.id
username = update.effective_user.username
if username not in AUTHORIZED_USERS:
response = "User {} is not authorized to use this bot.".format(username)
logging.info(response)
update.message.reply_text(response)
return
return func(update, context)
return wrapped
def start(update: Update, context: CallbackContext) -> None:
"""Send a message when the command /start is issued."""
user = update.effective_user.first_name
response = "Hi {}. Type /help for more information about this bot.".format(user)
update.message.reply_text(response)
@restricted
def help_command(update: Update, context: CallbackContext) -> None:
"""Send a message when the command /help is issued."""
response = 'To use this bot, just send a link of the YouTube video you would like to download, and it will be sent as a file to you.\n\ne.g. just send "https://www.youtube.com/watch?v=E3Pv4c4Qz9w" (without quotes) and I will show you the options.'
update.message.reply_text(response)
def get_download_keyboard():
keyboard = []
options = ["audio", 360, 480, 720, 1080, 1440, "max"]
for i in options:
if type(i) is int:
description = "Up to " + str(i) + "p 📺"
data = "{} | {}".format(i, description)
elif i == "audio":
description = "Audio only 🎵"
data = "{} | {}".format(i, description)
else:
description = "Maximum resolution available 📺"
data = "{} | {}".format(i, description)
keyboard.append([InlineKeyboardButton(description, callback_data=data)])
return keyboard
def download(context) -> None:
job = context.job
link = job.context['link']
message_id = job.context['message_id']
user_id = job.context['user_id']
option = job.context['option']
logging.info('Job triggered')
context.bot.send_message(
user_id,
"Working on it...",
reply_to_message_id=message_id,
)
options = {
"audio": "-f 'ba*[ext=mp3] / ba' -S 'ext'",
"360": "-f 'bv*[ext=mp4]+ba[ext=m4a]/b[ext=mp4] / bv*+ba/b' -S 'res:360'",
"480": "-f 'bv*[ext=mp4]+ba[ext=m4a]/b[ext=mp4] / bv*+ba/b' -S 'res:480'",
"720": "-f 'bv*[ext=mp4]+ba[ext=m4a]/b[ext=mp4] / bv*+ba/b' -S 'res:720'",
"1080": "-f 'bv*[ext=mp4]+ba[ext=m4a]/b[ext=mp4] / bv*+ba/b' -S 'res:1080'",
"1440": "-f 'bv*[ext=mp4]+ba[ext=m4a]/b[ext=mp4] / bv*+ba/b' -S 'res:1440'",
"max": "-f 'bv*[ext=mp4]+ba[ext=m4a]/b[ext=mp4] / bv*+ba/b'"
}
arg = options.get(option)
# beware shell injections! shlex.quote should escape them
# and the regex of the regexhandler should exclude other stuff
command = "{} {} -o '%(title)s.%(resolution)s.%(ext)s' {} -P {} --restrict-filenames --exec echo".format(YTDLP_PATH, shlex.quote(link), arg, DOWNLOAD_DIR)
try:
result = run(command, shell=True, capture_output=True, check=True, cwd=DOWNLOAD_DIR)
context.bot.send_message(
user_id,
"Download completed. Sending the file to you...",
reply_to_message_id=message_id,
)
shell_output = result.stdout.decode('utf-8')
path = shell_output.splitlines()[-1]
if option == "audio":
context.bot.send_audio(
user_id,
audio=open(path, "rb"),
reply_to_message_id=message_id,
timeout=15
)
else:
context.bot.send_video(
user_id,
video=open(path, "rb"),
reply_to_message_id=message_id,
supports_streaming=True,
timeout=15
)
except CalledProcessError as err:
context.bot.send_message(
user_id,
"process exited with code {} ```{}```".format(err.returncode, err.output),
reply_to_message_id=message_id,
)
@restricted
def text_handler(update: Update, context: CallbackContext) -> None:
response = "I'm sorry, but I couldn't find a valid YouTube URL here."
update.message.reply_text(response, quote=True)
@restricted
def url_handler(update: Update, context: CallbackContext) -> None:
context.user_data["link"] = update.message.text
context.user_data["message_id"] = update.message.message_id
context.user_data["user_id"] = update.effective_user.id
keyboard = get_download_keyboard()
reply_markup = InlineKeyboardMarkup(keyboard)
update.message.reply_text('Please choose an option:', reply_markup=reply_markup)
def call_download_job(update: Update, context: CallbackContext) -> None:
link = context.user_data["link"]
message_id = context.user_data["message_id"]
user_id = context.user_data["user_id"]
option = context.user_data["option"]
context.job_queue.run_once(download, 0, context={'link': link, 'message_id': message_id, 'user_id': user_id, 'option': option})
def button(update: Update, context: CallbackContext) -> None:
query = update.callback_query
query.answer()
option, description = query.data.split(" | ")
query.edit_message_text(text="Selected option: {}".format(description))
context.user_data["option"] = option
call_download_job(update, context)
def get_logs_command() -> None:
#cat log file, maybe last 10 lines or by date
a = 1
def main() -> None:
"""Start the bot."""
updater = Updater(TOKEN)
dispatcher = updater.dispatcher
dispatcher.add_handler(CommandHandler("start", start))
dispatcher.add_handler(CommandHandler("help", help_command))
dispatcher.add_handler(CommandHandler("getLogs", get_logs_command))
regex = 'https?://(www\.)?youtu(\.)?be(\.com)?/(?(3)watch\?v=|)?(?!playlist)[a-zA-Z0-9\-_]{4,15}'
dispatcher.add_handler(MessageHandler(Filters.regex(regex), url_handler))
dispatcher.add_handler(MessageHandler(Filters.text & ~Filters.regex(regex), text_handler))
dispatcher.add_handler(CallbackQueryHandler(button))
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()