forked from GreyMatter658/TG-YouTube-Uploader
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.py
335 lines (299 loc) · 12.9 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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import os
import asyncio
from urllib.parse import urlparse
from pyrogram import Client, filters
from pyrogram.types import Message, InlineKeyboardMarkup, InlineKeyboardButton
from youtube_dl import YoutubeDL
from opencc import OpenCC
from config import Config
import wget
Jebot = Client(
"YT Downloader",
api_id=Config.APP_ID,
api_hash=Config.API_HASH,
bot_token=Config.TG_BOT_TOKEN,
)
YTDL_REGEX = (r"^((?:https?:)?\/\/)"
r"?((?:www|m)\.)"
r"?((?:youtube\.com|youtu\.be|xvideos\.com|pornhub\.com"
r"|xhamster\.com|xnxx\.com))"
r"(\/)([-a-zA-Z0-9()@:%_\+.~#?&//=]*)([\w\-]+)(\S+)?$")
s2tw = OpenCC('s2tw.json').convert
@Jebot.on_message(filters.command("start"))
async def start(client, message):
if message.chat.type == 'private':
await Jebot.send_message(
chat_id=message.chat.id,
text="""<b>Hello! This is a YouTube Uploader Bot
I can download video or audio from Youtube. Made by @Moksh_b658 🇮🇳
Hit help button to find out more about how to use me</b>""",
reply_markup=InlineKeyboardMarkup(
[
[
InlineKeyboardButton('🆘 Help', callback_data="help"),
InlineKeyboardButton('👥 Support', url='https://telegram.me/greymatters_bots_discussion')
],[
InlineKeyboardButton('👮 Owner', url='https://t.me/Moksh_b658'),
InlineKeyboardButton('🤖 BotsList', url='https://t.me/+IezzVbvzlKxlODY1'),
InlineKeyboardButton('Channel 📢', url='https://t.me/GreyMatter_bots')
],[
InlineKeyboardButton('⭕ Source-GitHub ⭕', url='https://github.com/GreyMatter658/TG-YouTube-Uploader'),
]]
),
disable_web_page_preview=True,
parse_mode="html")
@Jebot.on_message(filters.command("help"))
async def help(client, message):
if message.chat.type == 'private':
await Jebot.send_message(
chat_id=message.chat.id,
text="""<b>YouTube Bot Help!
Just send a Youtube url to download it in video or audio format!
~ @GreyMatter_bots</b>""",
reply_markup=InlineKeyboardMarkup(
[[
InlineKeyboardButton(
"🔙 Back", callback_data="start"),
InlineKeyboardButton(
"👥 About ", callback_data="about"),
],[
InlineKeyboardButton("👮 Owner", url="https://t.me/Moksh_b658"),
InlineKeyboardButton("🤖 Bot List", url="https://t.me/+IezzVbvzlKxlODY1"),
InlineKeyboardButton('Channel 📢', url="https://t.me/GreyMatter_bots")
],[
InlineKeyboardButton("⭕Source Code -GitHub⭕", url="https://github.com/GreyMatter658/TG-YouTube-Uploader"),
]]
),
disable_web_page_preview=True,
parse_mode="html")
@Jebot.on_message(filters.command("about"))
async def about(client, message):
if message.chat.type == 'private':
await Jebot.send_message(
chat_id=message.chat.id,
text="""<b>About GreyMatter's YouTube Bot!</b>
<b>👨💻 Developer:</b> <a href="https://t.me/PredatorHackerzZ_bot">Predator 🇮🇳</a>
<b>💁♂️ Support:</b> <a href="https://telegram.me/greymatters_bots_discussion">GreyMatter's Support</a>
<b>😇 Channel :</b> <a href="https://t.me/GreyMatter_bots">GreyMatter's Bot </a>
<b>📚 Library:</b> <a href="https://github.com/pyrogram/pyrogram">Pyrogram</a>
<b>🤖 BotList :</b> <a href="https://t.me/+IezzVbvzlKxlODY1"> Telegram Bots </a>
<b>👤 Source : </b> <a href="https://github.com/GreyMatter658/TG-YouTube-Uploader"> Click Here </a>
<b>~ @GreyMatter_bots</b>""",
reply_markup=InlineKeyboardMarkup(
[[
InlineKeyboardButton(
"⭕ Join Our Channel ⭕", url="https://t.me/GreyMatter_bots"),
]]
),
disable_web_page_preview=True,
parse_mode="html")
# https://docs.pyrogram.org/start/examples/bot_keyboards
# Reply with inline keyboard
@Jebot.on_message(filters.private
& filters.text
& ~filters.edited
& filters.regex(YTDL_REGEX))
async def ytdl_with_button(_, message: Message):
await message.reply_text(
"**Choose Download type👇**",
reply_markup=InlineKeyboardMarkup(
[
[
InlineKeyboardButton(
"Audio 🎵",
callback_data="ytdl_audio"
),
InlineKeyboardButton(
"Video 🎬",
callback_data="ytdl_video"
)
]
]
),
quote=True
)
@Jebot.on_callback_query(filters.regex("^ytdl_audio$"))
async def callback_query_ytdl_audio(_, callback_query):
try:
url = callback_query.message.reply_to_message.text
ydl_opts = {
'format': 'bestaudio',
'outtmpl': '%(title)s - %(extractor)s-%(id)s.%(ext)s',
'writethumbnail': True
}
with YoutubeDL(ydl_opts) as ydl:
message = callback_query.message
await message.reply_chat_action("typing")
info_dict = ydl.extract_info(url, download=False)
# download
await callback_query.edit_message_text("**Downloading audio...**")
ydl.process_info(info_dict)
# upload
audio_file = ydl.prepare_filename(info_dict)
task = asyncio.create_task(send_audio(message, info_dict,
audio_file))
while not task.done():
await asyncio.sleep(3)
await message.reply_chat_action("upload_document")
await message.reply_chat_action("cancel")
await message.delete()
except Exception as e:
await message.reply_text(e)
await callback_query.message.reply_to_message.delete()
await callback_query.message.delete()
if Config.AUDIO_THUMBNAIL == "No":
async def send_audio(message: Message, info_dict, audio_file):
basename = audio_file.rsplit(".", 1)[-2]
# .webm -> .weba
if info_dict['ext'] == 'webm':
audio_file_weba = basename + ".weba"
os.rename(audio_file, audio_file_weba)
audio_file = audio_file_weba
# thumbnail
thumbnail_url = info_dict['thumbnail']
thumbnail_file = basename + "." + \
get_file_extension_from_url(thumbnail_url)
# info (s2tw)
webpage_url = info_dict['webpage_url']
title = s2tw(info_dict['title'])
caption = f"<b><a href=\"{webpage_url}\">{title}</a></b>"
duration = int(float(info_dict['duration']))
performer = s2tw(info_dict['uploader'])
await message.reply_audio(audio_file, caption=caption, duration=duration,
performer=performer, title=title,
parse_mode='HTML', thumb=thumbnail_file)
os.remove(audio_file)
os.remove(thumbnail_file)
else:
async def send_audio(message: Message, info_dict, audio_file):
basename = audio_file.rsplit(".", 1)[-2]
# .webm -> .weba
if info_dict['ext'] == 'webm':
audio_file_weba = basename + ".weba"
os.rename(audio_file, audio_file_weba)
audio_file = audio_file_weba
# thumbnail
lol = Config.AUDIO_THUMBNAIL
thumbnail_file = wget.download(lol)
# info (s2tw)
webpage_url = info_dict['webpage_url']
title = s2tw(info_dict['title'])
caption = f"<b><a href=\"{webpage_url}\">{title}</a></b>"
duration = int(float(info_dict['duration']))
performer = s2tw(info_dict['uploader'])
await message.reply_audio(audio_file, caption=caption, duration=duration,
performer=performer, title=title,
parse_mode='HTML', thumb=thumbnail_file)
os.remove(audio_file)
os.remove(thumbnail_file)
@Jebot.on_callback_query(filters.regex("^ytdl_video$"))
async def callback_query_ytdl_video(_, callback_query):
try:
# url = callback_query.message.text
url = callback_query.message.reply_to_message.text
ydl_opts = {
'format': 'best[ext=mp4]',
'outtmpl': '%(title)s - %(extractor)s-%(id)s.%(ext)s',
'writethumbnail': True
}
with YoutubeDL(ydl_opts) as ydl:
message = callback_query.message
await message.reply_chat_action("typing")
info_dict = ydl.extract_info(url, download=False)
# download
await callback_query.edit_message_text("**Downloading video...**")
ydl.process_info(info_dict)
# upload
video_file = ydl.prepare_filename(info_dict)
task = asyncio.create_task(send_video(message, info_dict,
video_file))
while not task.done():
await asyncio.sleep(3)
await message.reply_chat_action("upload_document")
await message.reply_chat_action("cancel")
await message.delete()
except Exception as e:
await message.reply_text(e)
await callback_query.message.reply_to_message.delete()
await callback_query.message.delete()
if Config.VIDEO_THUMBNAIL == "No":
async def send_video(message: Message, info_dict, video_file):
basename = video_file.rsplit(".", 1)[-2]
# thumbnail
thumbnail_url = info_dict['thumbnail']
thumbnail_file = basename + "." + \
get_file_extension_from_url(thumbnail_url)
# info (s2tw)
webpage_url = info_dict['webpage_url']
title = s2tw(info_dict['title'])
caption = f"<b><a href=\"{webpage_url}\">{title}</a></b>"
duration = int(float(info_dict['duration']))
width, height = get_resolution(info_dict)
await message.reply_video(
video_file, caption=caption, duration=duration,
width=width, height=height, parse_mode='HTML',
thumb=thumbnail_file)
os.remove(video_file)
os.remove(thumbnail_file)
else:
async def send_video(message: Message, info_dict, video_file):
basename = video_file.rsplit(".", 1)[-2]
# thumbnail
lel = Config.VIDEO_THUMBNAIL
thumbnail_file = wget.download(lel)
# info (s2tw)
webpage_url = info_dict['webpage_url']
title = s2tw(info_dict['title'])
caption = f"<b><a href=\"{webpage_url}\">{title}</a></b>"
duration = int(float(info_dict['duration']))
width, height = get_resolution(info_dict)
await message.reply_video(
video_file, caption=caption, duration=duration,
width=width, height=height, parse_mode='HTML',
thumb=thumbnail_file)
os.remove(video_file)
os.remove(thumbnail_file)
def get_file_extension_from_url(url):
url_path = urlparse(url).path
basename = os.path.basename(url_path)
return basename.split(".")[-1]
def get_resolution(info_dict):
if {"width", "height"} <= info_dict.keys():
width = int(info_dict['width'])
height = int(info_dict['height'])
# https://support.google.com/youtube/answer/6375112
elif info_dict['height'] == 1080:
width = 1920
height = 1080
elif info_dict['height'] == 720:
width = 1280
height = 720
elif info_dict['height'] == 480:
width = 854
height = 480
elif info_dict['height'] == 360:
width = 640
height = 360
elif info_dict['height'] == 240:
width = 426
height = 240
return (width, height)
@Jebot.on_callback_query()
async def button(bot, update):
cb_data = update.data
if "help" in cb_data:
await update.message.delete()
await help(bot, update.message)
elif "about" in cb_data:
await update.message.delete()
await about(bot, update.message)
elif "start" in cb_data:
await update.message.delete()
await start(bot, update.message)
print(
"""
Bot Started!
Join **@GreyMatter_bots**
"""
)
Jebot.run()