Skip to content

Commit

Permalink
coroutine in yt
Browse files Browse the repository at this point in the history
  • Loading branch information
SantiiRepair committed Aug 10, 2023
1 parent 7d1e615 commit 342880d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 50 deletions.
5 changes: 4 additions & 1 deletion .gitpod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
# Learn more from ready-to-use templates: https://www.gitpod.io/docs/introduction/getting-started/quickstart

tasks:
- init: sudo apt update && sudo apt install cron curl expect ffmpeg git python3 python3-pip tree -y && make install
- name: Setup & Install
before: sudo apt update && sudo apt install cron curl expect ffmpeg git python3 python3-pip tree
init: make install



2 changes: 1 addition & 1 deletion bot/helpers/yt.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def download_lagu(url: str) -> str:
return os.path.join("downloads", f"{info['id']}.{info['ext']}")


def ytsearch(query: str):
async def ytsearch(query: str):
try:
search = VideosSearch(query, limit=1).result()
data = search["result"][0]
Expand Down
90 changes: 42 additions & 48 deletions bot/plugins/play_song.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import os
import re
import uuid
import logging
from asyncio import sleep
Expand Down Expand Up @@ -31,8 +30,6 @@ async def play_song(client, message):
QUEUE = await load_pkl(queues, "rb", "dict")
chat = message.chat
replied = message.reply_to_message
query = message.text.split(" ", 1)[1]
search = ytsearch(query)
data = await user_info(message.from_user)
download_as = os.path.join(
current_dir, f"../downloads/songs/{str(uuid.uuid4())}.mp3"
Expand Down Expand Up @@ -64,10 +61,10 @@ async def play_song(client, message):
),
)
msg = await message.reply("\u23F3 **__Processing...__**")
regex = r"^(https?\:\/\/)?(www\.youtube\.com|youtu\.?be)\/.+"
match = re.match(regex, query)
try:
if match or " " in message.text:
if " " in message.text:
query = message.text.split(maxsplit=1)[1]
search = await ytsearch(query)
name = search[0]
title = search[0]
ref = search[1]
Expand All @@ -78,55 +75,53 @@ async def play_song(client, message):
hm, url = await ytdl(fmt, ref)
if hm == 0:
await msg.edit(f"`{url}`")
if search == 0:
return await msg.edit(
"__Can't find song.\n\nTry searching with more specific title.__",
)
if chat.id in QUEUE:
pos = await add_to_queue(chat, name, url, ref, "audio")
return await msg.edit(
f"__Added to queue at {pos}\n\n Title: [{name}]({url})\nDuration: {duration} Minutes\n Requested by:__ [{data['first_name']}]({data['linked']})",
# file=thumb,
reply_markup=[
[InlineKeyboardButton("cʟᴏꜱᴇ", callback_data="cls")]
],
reply_markup=InlineKeyboardMarkup(
[[InlineKeyboardButton("cʟᴏꜱᴇ", callback_data="cls")]]
),
)
elif VOICE_CHATS.get(chat.id) is None:
await msg.edit("\U0001fa84 **__Joining the voice chat...__**")
await on_call.join(chat.id)
VOICE_CHATS[chat.id] = on_call
await sleep(1)

if search == 0:
await msg.edit(
"__Can't find song.\n\nTry searching with more specific title.__",
)
await on_call.start_audio(url, repeat=False)
await add_to_queue(chat, name, url, ref, "audio")
await sleep(2)
await msg.edit(
f"**__Started Streaming__**\n\n **Title**: [{name}]({url})\n **Duration:** {duration} **Minutes\n Requested by:** [{data['first_name']}]({data['linked']})",
# file=thumb,
reply_markup=InlineKeyboardMarkup(
await sleep(2)
await on_call.start_audio(url, repeat=False)
await add_to_queue(chat, name, url, ref, "audio")
return await msg.edit(
f"**__Started Streaming__**\n\n **Title**: [{name}]({url})\n **Duration:** {duration} **Minutes\n Requested by:** [{data['first_name']}]({data['linked']})",
# file=thumb,
reply_markup=InlineKeyboardMarkup(
[
[
[
InlineKeyboardButton(
"\u23EA", callback_data="back_callback"
),
InlineKeyboardButton(
"\u23F8\uFE0F",
callback_data="pause_or_resume_callback",
),
InlineKeyboardButton(
"\u23ED\uFE0F",
callback_data="next_callback",
),
],
[
InlineKeyboardButton(
"cʟᴏꜱᴇ", callback_data="end_callback"
)
],
]
),
)
if replied.audio:
InlineKeyboardButton(
"\u23EA", callback_data="back_callback"
),
InlineKeyboardButton(
"\u23F8\uFE0F",
callback_data="pause_or_resume_callback",
),
InlineKeyboardButton(
"\u23ED\uFE0F",
callback_data="next_callback",
),
],
[
InlineKeyboardButton(
"cʟᴏꜱᴇ", callback_data="end_callback"
)
],
]
),
)
if replied and replied.audio:
name = "Audio File"
await msg.edit("\U0001f4be **__Downloading...__**")
media = await client.download_media(
Expand All @@ -135,7 +130,7 @@ async def play_song(client, message):
progress=progress,
progress_args=(client, chat, msg),
)
elif replied.voice:
elif replied and replied.voice:
name = "Voice Note"
await msg.edit("\U0001f4be **__Downloading...__**")
media = await client.download_media(
Expand Down Expand Up @@ -179,11 +174,10 @@ async def play_song(client, message):
await msg.edit("\U0001fa84 **__Joining the voice chat...__**")
await on_call.join(chat.id)
VOICE_CHATS[chat.id] = on_call
await sleep(1)
await sleep(2)
await on_call.start_audio(media, repeat=False)
await msg.delete()
await kreacher.send_photo(
return await kreacher.send_photo(
chat.id,
caption=f"**__Started Streaming__**\n\n **Title:** [{name}]({msg_linked})\n **Requested by:** [{data['first_name']}]({data['linked']})",
photo=fotoplay,
Expand Down Expand Up @@ -212,7 +206,7 @@ async def play_song(client, message):
)
except Exception as e:
logging.error(e)
clear_queue(chat)
await clear_queue(chat)
await VOICE_CHATS[chat.id].stop()
await msg.edit(
f"**__Oops master, something wrong has happened.__** \n\n`Error: {e}`",
Expand Down

0 comments on commit 342880d

Please sign in to comment.