queue download

This commit is contained in:
levina 2022-02-01 16:31:45 +07:00 committed by GitHub
parent 8e3bc05fd8
commit c5044d2372
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -37,10 +37,18 @@ ydl_opts = {
'quite': True
}
is_downloading = False
@Client.on_message(command(["song", f"song@{bn}"]) & ~filters.edited)
def song(_, message):
global is_downloading
query = " ".join(message.command[1:])
if is_downloading:
return await message.reply(
"» Another download in progress, please try again after some time !"
)
is_downloading = True
m = message.reply("🔎 finding song...")
ydl_ops = {"format": "bestaudio[ext=m4a]"}
try:
@ -57,7 +65,7 @@ def song(_, message):
m.edit("❌ song not found.\n\nplease give a valid song name.")
print(str(e))
return
m.edit("📥 downloading file...")
m.edit("📥 downloading song...")
try:
with yt_dlp.YoutubeDL(ydl_ops) as ydl:
info_dict = ydl.extract_info(link, download=False)
@ -68,7 +76,7 @@ def song(_, message):
for i in range(len(dur_arr) - 1, -1, -1):
dur += int(float(dur_arr[i])) * secmul
secmul *= 60
m.edit("📤 uploading file...")
m.edit("📤 uploading song...")
message.reply_audio(
audio_file,
caption=rep,
@ -78,6 +86,7 @@ def song(_, message):
duration=dur,
)
m.delete()
is_downloading = False
except Exception as e:
m.edit("❌ error, wait for bot owner to fix")
print(e)
@ -93,6 +102,7 @@ def song(_, message):
command(["vsong", f"vsong@{bn}", "video", f"video@{bn}"]) & ~filters.edited
)
async def vsong(client, message):
global is_downloading
ydl_opts = {
"format": "best",
"keepvideo": True,
@ -102,6 +112,11 @@ async def vsong(client, message):
"quite": True,
}
query = " ".join(message.command[1:])
if is_downloading:
return await message.reply(
"» Another download in progress, please try again after some time !"
)
is_downloading = True
try:
results = YoutubeSearch(query, max_results=1).to_dict()
link = f"https://youtube.com{results[0]['url_suffix']}"
@ -117,20 +132,21 @@ async def vsong(client, message):
except Exception as e:
print(e)
try:
msg = await message.reply("📥 **downloading video...**")
msg = await message.reply("📥 downloading video...")
with YoutubeDL(ydl_opts) as ytdl:
ytdl_data = ytdl.extract_info(link, download=True)
file_name = ytdl.prepare_filename(ytdl_data)
except Exception as e:
return await msg.edit(f"🚫 **error:** {e}")
return await msg.edit(f"🚫 error: `{e}`")
preview = wget.download(thumbnail)
await msg.edit("📤 **uploading video...**")
await msg.edit("📤 uploading video...")
await message.reply_video(
file_name,
duration=int(ytdl_data["duration"]),
thumb=preview,
caption=ytdl_data["title"],
)
is_downloading = False
try:
os.remove(file_name)
await msg.delete()