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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python3
|
2 |
+
# -*- coding: utf-8 -*-
|
3 |
+
# (c) Shrimadhav U K | X-Noid
|
4 |
+
|
5 |
+
# the logging things
|
6 |
+
import logging
|
7 |
+
logging.basicConfig(level=logging.DEBUG,
|
8 |
+
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
9 |
+
logger = logging.getLogger(__name__)
|
10 |
+
import lk21, urllib.parse, filetype, shutil, time, tldextract, asyncio, json, math, os, requests
|
11 |
+
from PIL import Image
|
12 |
+
# the secret configuration specific things
|
13 |
+
if bool(os.environ.get("WEBHOOK", False)):
|
14 |
+
from sample_config import Config
|
15 |
+
else:
|
16 |
+
from config import Config
|
17 |
+
|
18 |
+
# the Strings used for this "thing"
|
19 |
+
from translation import Translation
|
20 |
+
|
21 |
+
import pyrogram
|
22 |
+
logging.getLogger("pyrogram").setLevel(logging.WARNING)
|
23 |
+
from helper_funcs.display_progress import humanbytes
|
24 |
+
from helper_funcs.help_uploadbot import DownLoadFile
|
25 |
+
from helper_funcs.display_progress import progress_for_pyrogram
|
26 |
+
from hachoir.metadata import extractMetadata
|
27 |
+
from hachoir.parser import createParser
|
28 |
+
from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton
|
29 |
+
from pyrogram.errors import UserNotParticipant
|
30 |
+
|
31 |
+
|
32 |
+
@pyrogram.Client.on_message(pyrogram.filters.regex(pattern=".*http.*"))
|
33 |
+
async def echo(bot, update):
|
34 |
+
if update.from_user.id in Config.AUTH_USERS:
|
35 |
+
logger.info(update.from_user)
|
36 |
+
url = update.text
|
37 |
+
youtube_dl_username = None
|
38 |
+
youtube_dl_password = None
|
39 |
+
file_name = None
|
40 |
+
folder = f'./lk21/{update.from_user.id}/'
|
41 |
+
bypass = ['zippyshare', 'hxfile', 'mediafire', 'anonfiles', 'antfiles']
|
42 |
+
ext = tldextract.extract(url)
|
43 |
+
if ext.domain in bypass:
|
44 |
+
pablo = await update.reply_text('LK21 link detected')
|
45 |
+
time.sleep(2.5)
|
46 |
+
if os.path.isdir(folder):
|
47 |
+
await update.reply_text("Don't spam, wait till your previous task done.")
|
48 |
+
await pablo.delete()
|
49 |
+
return
|
50 |
+
os.makedirs(folder)
|
51 |
+
await pablo.edit_text('Downloading...')
|
52 |
+
bypasser = lk21.Bypass()
|
53 |
+
xurl = bypasser.bypass_url(url)
|
54 |
+
if ' | ' in url:
|
55 |
+
url_parts = url.split(' | ')
|
56 |
+
url = url_parts[0]
|
57 |
+
file_name = url_parts[1]
|
58 |
+
else:
|
59 |
+
if xurl.find('/'):
|
60 |
+
urlname = xurl.rsplit('/', 1)[1]
|
61 |
+
file_name = urllib.parse.unquote(urlname)
|
62 |
+
if '+' in file_name:
|
63 |
+
file_name = file_name.replace('+', ' ')
|
64 |
+
dldir = f'{folder}{file_name}'
|
65 |
+
r = requests.get(xurl, allow_redirects=True)
|
66 |
+
open(dldir, 'wb').write(r.content)
|
67 |
+
try:
|
68 |
+
file = filetype.guess(dldir)
|
69 |
+
xfiletype = file.mime
|
70 |
+
except AttributeError:
|
71 |
+
xfiletype = file_name
|
72 |
+
if xfiletype in ['video/mp4', 'video/x-matroska', 'video/webm', 'audio/mpeg']:
|
73 |
+
metadata = extractMetadata(createParser(dldir))
|
74 |
+
if metadata is not None:
|
75 |
+
if metadata.has("duration"):
|
76 |
+
duration = metadata.get('duration').seconds
|
77 |
+
await pablo.edit_text('Uploading...')
|
78 |
+
start_time = time.time()
|
79 |
+
if xfiletype in ['video/mp4', 'video/x-matroska', 'video/webm']:
|
80 |
+
await bot.send_video(
|
81 |
+
chat_id=update.chat.id,
|
82 |
+
video=dldir,
|
83 |
+
caption=file_name,
|
84 |
+
duration=duration,
|
85 |
+
reply_to_message_id=update.message_id,
|
86 |
+
progress=progress_for_pyrogram,
|
87 |
+
progress_args=(
|
88 |
+
Translation.UPLOAD_START,
|
89 |
+
pablo,
|
90 |
+
start_time
|
91 |
+
)
|
92 |
+
)
|
93 |
+
elif xfiletype == 'audio/mpeg':
|
94 |
+
await bot.send_audio(
|
95 |
+
chat_id=update.chat.id,
|
96 |
+
audio=dldir,
|
97 |
+
caption=file_name,
|
98 |
+
duration=duration,
|
99 |
+
reply_to_message_id=update.message_id,
|
100 |
+
progress=progress_for_pyrogram,
|
101 |
+
progress_args=(
|
102 |
+
Translation.UPLOAD_START,
|
103 |
+
pablo,
|
104 |
+
start_time
|
105 |
+
)
|
106 |
+
)
|
107 |
+
else:
|
108 |
+
await bot.send_document(
|
109 |
+
chat_id=update.chat.id,
|
110 |
+
document=dldir,
|
111 |
+
caption=file_name,
|
112 |
+
reply_to_message_id=update.message_id,
|
113 |
+
progress=progress_for_pyrogram,
|
114 |
+
progress_args=(
|
115 |
+
Translation.UPLOAD_START,
|
116 |
+
pablo,
|
117 |
+
start_time
|
118 |
+
)
|
119 |
+
)
|
120 |
+
await pablo.delete()
|
121 |
+
shutil.rmtree(folder)
|
122 |
+
return
|
123 |
+
if "|" in url:
|
124 |
+
url_parts = url.split("|")
|
125 |
+
if len(url_parts) == 2:
|
126 |
+
url = url_parts[0]
|
127 |
+
file_name = url_parts[1]
|
128 |
+
elif len(url_parts) == 4:
|
129 |
+
url = url_parts[0]
|
130 |
+
file_name = url_parts[1]
|
131 |
+
youtube_dl_username = url_parts[2]
|
132 |
+
youtube_dl_password = url_parts[3]
|
133 |
+
else:
|
134 |
+
for entity in update.entities:
|
135 |
+
if entity.type == "text_link":
|
136 |
+
url = entity.url
|
137 |
+
elif entity.type == "url":
|
138 |
+
o = entity.offset
|
139 |
+
l = entity.length
|
140 |
+
url = url[o:o + l]
|
141 |
+
if url is not None:
|
142 |
+
url = url.strip()
|
143 |
+
if file_name is not None:
|
144 |
+
file_name = file_name.strip()
|
145 |
+
# https://stackoverflow.com/a/761825/4723940
|
146 |
+
if youtube_dl_username is not None:
|
147 |
+
youtube_dl_username = youtube_dl_username.strip()
|
148 |
+
if youtube_dl_password is not None:
|
149 |
+
youtube_dl_password = youtube_dl_password.strip()
|
150 |
+
logger.info(url)
|
151 |
+
logger.info(file_name)
|
152 |
+
else:
|
153 |
+
for entity in update.entities:
|
154 |
+
if entity.type == "text_link":
|
155 |
+
url = entity.url
|
156 |
+
elif entity.type == "url":
|
157 |
+
o = entity.offset
|
158 |
+
l = entity.length
|
159 |
+
url = url[o:o + l]
|
160 |
+
if Config.HTTP_PROXY != "":
|
161 |
+
command_to_exec = [
|
162 |
+
"yt-dlp",
|
163 |
+
"--no-warnings",
|
164 |
+
"--youtube-skip-dash-manifest",
|
165 |
+
"-j",
|
166 |
+
url,
|
167 |
+
"--proxy", Config.HTTP_PROXY
|
168 |
+
]
|
169 |
+
else:
|
170 |
+
command_to_exec = [
|
171 |
+
"yt-dlp",
|
172 |
+
"--no-warnings",
|
173 |
+
"--youtube-skip-dash-manifest",
|
174 |
+
"-j",
|
175 |
+
url
|
176 |
+
]
|
177 |
+
if youtube_dl_username is not None:
|
178 |
+
command_to_exec.append("--username")
|
179 |
+
command_to_exec.append(youtube_dl_username)
|
180 |
+
if youtube_dl_password is not None:
|
181 |
+
command_to_exec.append("--password")
|
182 |
+
command_to_exec.append(youtube_dl_password)
|
183 |
+
# logger.info(command_to_exec)
|
184 |
+
process = await asyncio.create_subprocess_exec(
|
185 |
+
*command_to_exec,
|
186 |
+
# stdout must a pipe to be accessible as process.stdout
|
187 |
+
stdout=asyncio.subprocess.PIPE,
|
188 |
+
stderr=asyncio.subprocess.PIPE,
|
189 |
+
)
|
190 |
+
# Wait for the subprocess to finish
|
191 |
+
stdout, stderr = await process.communicate()
|
192 |
+
e_response = stderr.decode().strip()
|
193 |
+
# logger.info(e_response)
|
194 |
+
t_response = stdout.decode().strip()
|
195 |
+
# logger.info(t_response)
|
196 |
+
# https://github.com/rg3/youtube-dl/issues/2630#issuecomment-38635239
|
197 |
+
if e_response and "nonnumeric port" not in e_response:
|
198 |
+
# logger.warn("Status : FAIL", exc.returncode, exc.output)
|
199 |
+
error_message = e_response.replace("please report this issue on https://yt-dl.org/bug . Make sure you are using the latest version; see https://yt-dl.org/update on how to update. Be sure to call youtube-dl with the --verbose flag and include its complete output.", "")
|
200 |
+
if "This video is only available for registered users." in error_message:
|
201 |
+
error_message += Translation.SET_CUSTOM_USERNAME_PASSWORD
|
202 |
+
await bot.send_message(
|
203 |
+
chat_id=update.chat.id,
|
204 |
+
text=Translation.NO_VOID_FORMAT_FOUND.format(str(error_message)),
|
205 |
+
reply_to_message_id=update.message_id,
|
206 |
+
parse_mode="html",
|
207 |
+
disable_web_page_preview=True
|
208 |
+
)
|
209 |
+
return False
|
210 |
+
if t_response:
|
211 |
+
# logger.info(t_response)
|
212 |
+
x_reponse = t_response
|
213 |
+
if "\n" in x_reponse:
|
214 |
+
x_reponse, _ = x_reponse.split("\n")
|
215 |
+
response_json = json.loads(x_reponse)
|
216 |
+
save_ytdl_json_path = Config.DOWNLOAD_LOCATION + \
|
217 |
+
"/" + str(update.from_user.id) + ".json"
|
218 |
+
with open(save_ytdl_json_path, "w", encoding="utf8") as outfile:
|
219 |
+
json.dump(response_json, outfile, ensure_ascii=False)
|
220 |
+
# logger.info(response_json)
|
221 |
+
inline_keyboard = []
|
222 |
+
duration = None
|
223 |
+
if "duration" in response_json:
|
224 |
+
duration = response_json["duration"]
|
225 |
+
if "formats" in response_json:
|
226 |
+
for formats in response_json["formats"]:
|
227 |
+
format_id = formats.get("format_id")
|
228 |
+
format_string = formats.get("format_note")
|
229 |
+
if format_string is None:
|
230 |
+
format_string = formats.get("format")
|
231 |
+
format_ext = formats.get("ext")
|
232 |
+
approx_file_size = ""
|
233 |
+
if "filesize" in formats:
|
234 |
+
approx_file_size = humanbytes(formats["filesize"])
|
235 |
+
cb_string_video = "{}|{}|{}".format(
|
236 |
+
"video", format_id, format_ext)
|
237 |
+
cb_string_file = "{}|{}|{}".format(
|
238 |
+
"file", format_id, format_ext)
|
239 |
+
if format_string is not None and not "audio only" in format_string:
|
240 |
+
ikeyboard = [
|
241 |
+
InlineKeyboardButton(
|
242 |
+
"S " + format_string + " video " + approx_file_size + " ",
|
243 |
+
callback_data=(cb_string_video).encode("UTF-8")
|
244 |
+
),
|
245 |
+
InlineKeyboardButton(
|
246 |
+
"D " + format_ext + " " + approx_file_size + " ",
|
247 |
+
callback_data=(cb_string_file).encode("UTF-8")
|
248 |
+
)
|
249 |
+
]
|
250 |
+
"""if duration is not None:
|
251 |
+
cb_string_video_message = "{}|{}|{}".format(
|
252 |
+
"vm", format_id, format_ext)
|
253 |
+
ikeyboard.append(
|
254 |
+
InlineKeyboardButton(
|
255 |
+
"VM",
|
256 |
+
callback_data=(
|
257 |
+
cb_string_video_message).encode("UTF-8")
|
258 |
+
)
|
259 |
+
)"""
|
260 |
+
else:
|
261 |
+
# special weird case :\
|
262 |
+
ikeyboard = [
|
263 |
+
InlineKeyboardButton(
|
264 |
+
"SVideo [" +
|
265 |
+
"] ( " +
|
266 |
+
approx_file_size + " )",
|
267 |
+
callback_data=(cb_string_video).encode("UTF-8")
|
268 |
+
),
|
269 |
+
InlineKeyboardButton(
|
270 |
+
"DFile [" +
|
271 |
+
"] ( " +
|
272 |
+
approx_file_size + " )",
|
273 |
+
callback_data=(cb_string_file).encode("UTF-8")
|
274 |
+
)
|
275 |
+
]
|
276 |
+
inline_keyboard.append(ikeyboard)
|
277 |
+
if duration is not None:
|
278 |
+
cb_string_64 = "{}|{}|{}".format("audio", "64k", "mp3")
|
279 |
+
cb_string_128 = "{}|{}|{}".format("audio", "128k", "mp3")
|
280 |
+
cb_string = "{}|{}|{}".format("audio", "320k", "mp3")
|
281 |
+
inline_keyboard.append([
|
282 |
+
InlineKeyboardButton(
|
283 |
+
"MP3 " + "(" + "64 kbps" + ")", callback_data=cb_string_64.encode("UTF-8")),
|
284 |
+
InlineKeyboardButton(
|
285 |
+
"MP3 " + "(" + "128 kbps" + ")", callback_data=cb_string_128.encode("UTF-8"))
|
286 |
+
])
|
287 |
+
inline_keyboard.append([
|
288 |
+
InlineKeyboardButton(
|
289 |
+
"MP3 " + "(" + "320 kbps" + ")", callback_data=cb_string.encode("UTF-8"))
|
290 |
+
])
|
291 |
+
else:
|
292 |
+
format_id = response_json["format_id"]
|
293 |
+
format_ext = response_json["ext"]
|
294 |
+
cb_string_file = "{}|{}|{}".format(
|
295 |
+
"file", format_id, format_ext)
|
296 |
+
cb_string_video = "{}|{}|{}".format(
|
297 |
+
"video", format_id, format_ext)
|
298 |
+
inline_keyboard.append([
|
299 |
+
InlineKeyboardButton(
|
300 |
+
"SVideo",
|
301 |
+
callback_data=(cb_string_video).encode("UTF-8")
|
302 |
+
),
|
303 |
+
InlineKeyboardButton(
|
304 |
+
"DFile",
|
305 |
+
callback_data=(cb_string_file).encode("UTF-8")
|
306 |
+
)
|
307 |
+
])
|
308 |
+
cb_string_file = "{}={}={}".format(
|
309 |
+
"file", format_id, format_ext)
|
310 |
+
cb_string_video = "{}={}={}".format(
|
311 |
+
"video", format_id, format_ext)
|
312 |
+
inline_keyboard.append([
|
313 |
+
InlineKeyboardButton(
|
314 |
+
"video",
|
315 |
+
callback_data=(cb_string_video).encode("UTF-8")
|
316 |
+
),
|
317 |
+
InlineKeyboardButton(
|
318 |
+
"file",
|
319 |
+
callback_data=(cb_string_file).encode("UTF-8")
|
320 |
+
)
|
321 |
+
])
|
322 |
+
reply_markup = InlineKeyboardMarkup(inline_keyboard)
|
323 |
+
# logger.info(reply_markup)
|
324 |
+
thumbnail = Config.DEF_THUMB_NAIL_VID_S
|
325 |
+
thumbnail_image = Config.DEF_THUMB_NAIL_VID_S
|
326 |
+
if "thumbnail" in response_json:
|
327 |
+
if response_json["thumbnail"] is not None:
|
328 |
+
thumbnail = response_json["thumbnail"]
|
329 |
+
thumbnail_image = response_json["thumbnail"]
|
330 |
+
thumb_image_path = DownLoadFile(
|
331 |
+
thumbnail_image,
|
332 |
+
Config.DOWNLOAD_LOCATION + "/" +
|
333 |
+
str(update.from_user.id) + ".webp",
|
334 |
+
Config.CHUNK_SIZE,
|
335 |
+
None, # bot,
|
336 |
+
Translation.DOWNLOAD_START,
|
337 |
+
update.message_id,
|
338 |
+
update.chat.id
|
339 |
+
)
|
340 |
+
if os.path.exists(thumb_image_path):
|
341 |
+
im = Image.open(thumb_image_path).convert("RGB")
|
342 |
+
im.save(thumb_image_path.replace(".webp", ".jpg"), "jpeg")
|
343 |
+
else:
|
344 |
+
thumb_image_path = None
|
345 |
+
await bot.send_message(
|
346 |
+
chat_id=update.chat.id,
|
347 |
+
text=Translation.FORMAT_SELECTION.format(thumbnail) + "\n" + Translation.SET_CUSTOM_USERNAME_PASSWORD,
|
348 |
+
reply_markup=reply_markup,
|
349 |
+
parse_mode="html",
|
350 |
+
reply_to_message_id=update.message_id
|
351 |
+
)
|
352 |
+
else:
|
353 |
+
# fallback for nonnumeric port a.k.a seedbox.io
|
354 |
+
inline_keyboard = []
|
355 |
+
cb_string_file = "{}={}={}".format(
|
356 |
+
"file", "LFO", "NONE")
|
357 |
+
cb_string_video = "{}={}={}".format(
|
358 |
+
"video", "OFL", "ENON")
|
359 |
+
inline_keyboard.append([
|
360 |
+
InlineKeyboardButton(
|
361 |
+
"SVideo",
|
362 |
+
callback_data=(cb_string_video).encode("UTF-8")
|
363 |
+
),
|
364 |
+
InlineKeyboardButton(
|
365 |
+
"DFile",
|
366 |
+
callback_data=(cb_string_file).encode("UTF-8")
|
367 |
+
)
|
368 |
+
])
|
369 |
+
reply_markup = InlineKeyboardMarkup(inline_keyboard)
|
370 |
+
await bot.send_message(
|
371 |
+
chat_id=update.chat.id,
|
372 |
+
text=Translation.FORMAT_SELECTION.format(""),
|
373 |
+
reply_markup=reply_markup,
|
374 |
+
parse_mode="html",
|
375 |
+
reply_to_message_id=update.message_id
|
376 |
+
)
|